Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #228378

Re: portable way to get highest bit set?

Path csiph.com!news.mixmin.net!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From Ian C <deathstation9000@gmail.com>
Newsgroups comp.lang.c, alt.comp.lang.c
Subject Re: portable way to get highest bit set?
Date Wed, 11 Oct 2023 18:03:38 +0100
Organization A noiseless patient Spider
Lines 28
Message-ID <b2e341ea-a49d-c0cf-d8ee-96235db44038@gmail.com> (permalink)
References <ug5gvh$1jkar$3@dont-email.me> <20231011102714.44a870af4dfe68f756974953@g{oogle}mail.com> <ug6huc$1rvp1$1@dont-email.me>
MIME-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 8bit
Injection-Info dont-email.me; posting-host="c45550e91e867ea6aa4e8876e5f5907d"; logging-data="2030851"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18BIOt83aePdsD4JKd4EjJNGV0QXzuuE3M="
User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Thunderbird/102.15.1
Cancel-Lock sha1:Y4arXblZtfpEYEwvq97lYx81e+Y=
In-Reply-To <ug6huc$1rvp1$1@dont-email.me>
Xref csiph.com comp.lang.c:228378 alt.comp.lang.c:606

Cross-posted to 2 groups.

Show key headers only | View raw


On 11/10/2023 17:19, candycanearter07 wrote:
> On 10/11/23 02:27, Anton Shepelev wrote:
>> candycanearter07:
>>
>>> What is the best/most portable way to get the highest bit
>>> set?
>>>
>>> ie. 011010001
>>> to  010000000
>>
>> What is your best attempt, even if unsuccessful?
>>
> 
> int out;
> for(out = 0x100000000; out; out >> 1)
>      if(out & input) break;
> 
> I'm trying to find something size independent, though.
> Or at least able to be swapped out with a #define

If you want the highest bit, 0x800000000 may be a better start :-)

If it was me I'd do it in reverse.  Assuming it's unsigned, shift your 
input to the right until it hits zero, counting the number of shifts 
which will be your bit number.  Assuming my head is currently working.


Ian C

Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Re: portable way to get highest bit set? candycanearter07 <no@thanks.net> - 2023-10-11 11:19 -0500
  Re: portable way to get highest bit set? David Brown <david.brown@hesbynett.no> - 2023-10-11 18:45 +0200
  Re: portable way to get highest bit set? Ian C <deathstation9000@gmail.com> - 2023-10-11 18:03 +0100
    Re: portable way to get highest bit set? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2023-10-11 10:17 -0700
  Re: portable way to get highest bit set? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2023-10-11 10:34 -0700
  Re: portable way to get highest bit set? Richard Harnden <richard.nospam@gmail.invalid> - 2023-10-11 19:02 +0100

csiph-web