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


Groups > comp.lang.c > #231216

Re: portable way to get highest bit set?

From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.c, alt.comp.lang.c
Subject Re: portable way to get highest bit set?
Date 2023-10-11 23:58 +0100
Organization A noiseless patient Spider
Message-ID <878r884vi5.fsf@bsb.me.uk> (permalink)
References <ug5gvh$1jkar$3@dont-email.me> <ug70cu$219ov$1@dont-email.me>

Cross-posted to 2 groups.

Show all headers | View raw


jak <nospam@please.ty> writes:

> candycanearter07 ha scritto:
>> Hi,
>> What is the best/most portable way to get the highest bit set?
>> ie. 011010001
>> to  010000000
>
> Hi,
> I don't think it's the best but the most portable could be the
> mathematical approach:
>
> #include <stdio.h>
> #include <math.h>
>
> int main()
> {
>     unsigned long val = 3000, ret;

Test case: val = 0xFFFFFFFFFFFFFFFF

>     ret = (unsigned long)pow(2., (double)(unsigned long)(log((double)val) /
>    log(2.)));
>
>     printf("%lu -> %lu", val, ret);
>
>     return 0;
> }

-- 
Ben.

Back to comp.lang.c | Previous | Next | Find similar


Thread

Re: portable way to get highest bit set? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-10-11 23:58 +0100

csiph-web