Re: portable way to get highest bit set?
Kaz Kylheku <[email protected]> Wed, 11 Oct 2023 17:23:36 -0000 (UTC)
| Newsgroups | comp.lang.c,alt.comp.lang.c |
|---|---|
| Organization | A noiseless patient Spider |
| Message-ID | <[email protected]> |
On 2023-10-11, candycanearter07 <[email protected]> wrote: > Hi, > > What is the best/most portable way to get the highest bit set? > > ie. 011010001 > to 010000000 I don't know about best or portable, but in the run-time of the TXR language, I solved it like this (see highest_bit function): https://www.kylheku.com/cgit/txr/tree/arith.c If we have GNU C builtin primitives for this, we use them. Otherwise do a statically coded binary search (no loop). The build configuration system provides constants like SIZEOF_PTR. That is not essential because you can just use regular conditionals. -- TXR Programming Language: http://nongnu.org/txr Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal Mastodon: @[email protected] NOTE: If you use Google Groups, I don't see you, unless you're whitelisted.