Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #272722
| From | Tim Rentsch <tr.17687@z991.linuxsc.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: portable way to get highest bit set? |
| Date | 2023-10-15 01:44 -0700 |
| Organization | A noiseless patient Spider |
| Message-ID | <86lec48ebs.fsf@linuxsc.com> (permalink) |
| References | <ug5gvh$1jkar$3@dont-email.me> <20231011143809.748@kylheku.com> <20231014222046.551@kylheku.com> |
Kaz Kylheku <864-117-4973@kylheku.com> writes:
> On 2023-10-11, Kaz Kylheku <864-117-4973@kylheku.com> wrote:
>
>> E.g. 32 bit code:
>>
>> uint32_t fill_mask_down(uint32_t x)
>> {
>> x |= x >> 1; // e.g. 1000...0000 -> 1100...0000
>> x |= x >> 2; // e.g. 1100...0000 -> 1111...0000
>> x |= x >> 4; // e.g. 11110000... -> 11111111...
>> x |= x >> 8;
>> x |= x >> 16;
>>
>> return x;
>> }
>>
>> Thus:
>>
>> uint32_t isolate_highest_bit(uint32_t x)
>> {
>> uint32_t m = fill_mask_down(x);
>> return m ^ (m >> 1);
>> }
>
> I guess this went over people's heads?
Not at all. It's a well-known technique. But it doesn't
address the central ask for an approach that works no
matter how wide the value type is.
Back to comp.lang.c | Previous | Next — Previous in thread | Find similar
Re: portable way to get highest bit set? Kaz Kylheku <864-117-4973@kylheku.com> - 2023-10-15 05:26 +0000
Re: portable way to get highest bit set? Michael S <already5chosen@yahoo.com> - 2023-10-15 00:28 -0700
Re: portable way to get highest bit set? Michael S <already5chosen@yahoo.com> - 2023-10-15 11:35 +0300
Re: portable way to get highest bit set? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2023-10-15 02:52 -0700
Re: portable way to get highest bit set? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2023-10-15 01:44 -0700
csiph-web