Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #236095
| From | Tim Rentsch <tr.17687@z991.linuxsc.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: portable way to get highest bit set? |
| Date | 2023-10-12 02:41 -0700 |
| Organization | A noiseless patient Spider |
| Message-ID | <86cyxkb2ka.fsf@linuxsc.com> (permalink) |
| References | <ug5gvh$1jkar$3@dont-email.me> <20231011102714.44a870af4dfe68f756974953@g{oogle}mail.com> <ug6huc$1rvp1$1@dont-email.me> <86h6mxawqq.fsf@linuxsc.com> <20231012111100.272c96b3209baad26a150e55@g{oogle}mail.com> |
Anton Shepelev <anton.txt@g{oogle}mail.com> writes:
> Tim Rentsch:
>
>> I've coded up a dozen[*] different solutions.
>> Here is my current avorite[**]:
>>
>> typedef union {
>> long double d;
>> struct { unsigned long long s; unsigned e:15;} b;
>> } X;
>>
>> unsigned long long
>> high_bit_set( unsigned long long u ){
>> return 1ULL << (X){ .d = u }.b.e - 16383 & u;
>> }
>
> And mine is quite anticlimactic. It accepts a byte, because
> I wrote it yesterday, before learning that the OP needs a
> more generic solution:
>
> unsigned char highest_bit( unsigned char byte )
> { int i ;
> unsigned char mask = 1 << 7;
> for( i = 0; i <= 8; i += 1 )
> { if( byte & mask )
> { break; }
> mask >>= 1;
> }
> return mask;
> }
>
> It is the most straight-forward method that I could think
> of, and extensible to larger sizes of the variable.
But the approach you are using requires knowing the size
of the type involved. The challenge is to write code
that works without having to know the size in advance.
So something like the following, with type T being an
unknown unsigned integer type:
T
highest_bit_set( T u ){
T r;
/* set r to the highest bit set in u */
return r;
}
I have written code for this form of the problem, but
won't be posting it before the weekend. Try it!
P.S. My earlier "solution" was just for fun, not a serious
attempt to solve the problem.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Re: portable way to get highest bit set? Anton Shepelev <anton.txt@g{oogle}mail.com> - 2023-10-12 11:11 +0300
Re: portable way to get highest bit set? Michael S <already5chosen@yahoo.com> - 2023-10-12 02:27 -0700
Re: portable way to get highest bit set? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2023-10-12 02:41 -0700
Re: portable way to get highest bit set? Anton Shepelev <anton.txt@g{oogle}mail.com> - 2023-10-12 14:17 +0300
Re: portable way to get highest bit set? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2023-10-12 07:01 -0700
Re: portable way to get highest bit set? scott@slp53.sl.home (Scott Lurndal) - 2023-10-12 14:18 +0000
Re: portable way to get highest bit set? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2023-10-12 07:58 -0700
Re: portable way to get highest bit set? Michael S <already5chosen@yahoo.com> - 2023-10-12 17:30 +0300
Re: portable way to get highest bit set? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2023-10-12 08:05 -0700
Re: portable way to get highest bit set? Michael S <already5chosen@yahoo.com> - 2023-10-12 19:16 +0300
Re: portable way to get highest bit set? Anton Shepelev <anton.txt@g{oogle}mail.com> - 2023-10-12 18:31 +0300
csiph-web