Re: portable way to get highest bit set?

Blue-Maned_Hawk <[email protected]> Wed, 11 Oct 2023 21:16:48 -0000 (UTC)
Newsgroups comp.lang.c,alt.comp.lang.c
Organization A noiseless patient Spider
Message-ID <[email protected]>
candycanearter07 wrote:

> Hi,
> 
> What is the best/most portable way to get the highest bit set?
> 
> ie. 011010001 to  010000000

#include <stdbit.h>

unsigned char highest_bit_setc(unsigned char x)
{
	return stdc_first_leading_one(x) ? (1 << stdc_first_leading_one(x) 
- 1) : 0;
}

/* Repeat for all appropriate types and then probably make a generic macro 
for all the subroutines. */

-- 
Blue-Maned_Hawk│shortens to 
Hawk│/
blu.mɛin.dʰak/
│he/him/his/himself/Mr. 
bluemanedhawk.github.io
Minor vehicular events!