Re: portable way to get highest bit set?

Kaz Kylheku <[email protected]> Sun, 15 Oct 2023 05:26:44 -0000 (UTC)
Newsgroups comp.lang.c,alt.comp.lang.c
Organization A noiseless patient Spider
Message-ID <[email protected]>
On 2023-10-11, Kaz Kylheku <[email protected]> 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?

-- 
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @[email protected]
NOTE: If you use Google Groups, I don't see you, unless you're whitelisted.