Re: portable way to get highest bit set?
Keith Thompson <[email protected]> Wed, 11 Oct 2023 14:30:55 -0700
| Newsgroups | comp.lang.c,alt.comp.lang.c |
|---|---|
| Organization | None to speak of |
| Message-ID | <[email protected]> |
Blue-Maned_Hawk <[email protected]> writes: > candycanearter07 wrote: >> 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. */ A solution that depends on a header that's introduced in C23, a standard that hasn't been published yet, is hardly portable. BTW, here's what your signature looks like in my newsreader (Gnus): Blue-Maned_HawkÃ\203\203\203\203Ã\203\203\202Ã\203\202Ã\202¢\224\202shortens to HawkÃ\203\203\203\203Ã\203\203\202Ã\203\202Ã\202¢\224\202/ blu.mÃ\203\203\203\203\211\233in.dÃ\203\203\203\203\212Ã\203\203\203\202Ã\203\203\202Ã\203\202Ã\202°ak/ Ã\203\203\203\203Ã\203\203\202Ã\203\202Ã\202¢\224\202he/him/his/himself/Mr. bluemanedhawk.github.io Minor vehicular events! And in Thunderbird: Blue-Maned_HawkÃÃÃâshortens to HawkÃÃÃâ/ blu.mÃin.dÃÃÃÃðak/ ÃÃÃâhe/him/his/himself/Mr. bluemanedhawk.github.io Minor vehicular events! -- Keith Thompson (The_Other_Keith) [email protected] Will write code for food. void Void(void) { Void(); } /* The recursive call of the void */