Re: portable way to get highest bit set?

David Brown <[email protected]> Wed, 11 Oct 2023 11:31:32 +0200
Newsgroups comp.lang.c,alt.comp.lang.c
Organization A noiseless patient Spider
Message-ID <[email protected]>
On 11/10/2023 09: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?
> 

And what is your definition of "best" and "most portable" ?

"Best" can mean smallest, neatest, fastest, and that can depend highly 
on the compiler, target and flags.

"Portable" can mean relying only on pure standard C (and if so, which 
standard version?), or one compiler but many targets, or one target and 
many compilers, or a group of common targets and compilers excluding 
"unusual" ones.

And what types do you want to support?  All integer types, or just some 
specific ones?

What is the specification for the function you want?  In particular, 
what should it do for 0?  Do you really want a result with one bit set, 
or do you actually want the bit number?