Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #230937

Re: portable way to get highest bit set?

From Keith Thompson <Keith.S.Thompson+u@gmail.com>
Newsgroups comp.lang.c, alt.comp.lang.c
Subject Re: portable way to get highest bit set?
Date 2023-10-11 14:30 -0700
Organization None to speak of
Message-ID <87h6mwu9rk.fsf@nosuchdomain.example.com> (permalink)
References <ug5gvh$1jkar$3@dont-email.me> <pan$e0805$faa9a91a$a60476dc$4a90cfa8@invalid.invalid>

Cross-posted to 2 groups.

Show all headers | View raw


Blue-Maned_Hawk <bluemanedhawk@invalid.invalid> 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) Keith.S.Thompson+u@gmail.com
Will write code for food.
void Void(void) { Void(); } /* The recursive call of the void */

Back to comp.lang.c | Previous | NextPrevious in thread | Find similar


Thread

Re: portable way to get highest bit set? Blue-Maned_Hawk <bluemanedhawk@invalid.invalid> - 2023-10-11 21:16 +0000
  Re: portable way to get highest bit set? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-10-11 14:30 -0700

csiph-web