Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #273045
| Path | csiph.com!news.mixmin.net!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail |
|---|---|
| From | Tim Rentsch <tr.17687@z991.linuxsc.com> |
| Newsgroups | comp.lang.c |
| Subject | Re: portable way to get highest bit set? |
| Date | Sun, 15 Oct 2023 02:52:37 -0700 |
| Organization | A noiseless patient Spider |
| Lines | 57 |
| Message-ID | <86h6ms8b6i.fsf@linuxsc.com> (permalink) |
| References | <ug5gvh$1jkar$3@dont-email.me> <20231011143809.748@kylheku.com> <20231014222046.551@kylheku.com> <20231015113545.0000150f@yahoo.com> |
| MIME-Version | 1.0 |
| Content-Type | text/plain; charset=us-ascii |
| Injection-Info | dont-email.me; posting-host="eb478ed0db5b06d993c3ccd040701c7e"; logging-data="475715"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX187y2Zh55kHb5paj3ABF9Ybkz7s9x3XdzI=" |
| User-Agent | Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) |
| Cancel-Lock | sha1:fNKyUkP/Ll3CQycJp//foFPffB0= sha1:FhSoMpcMsgddYFC3tI76lFsOY54= |
| Xref | csiph.com comp.lang.c:273045 |
Show key headers only | View raw
Michael S <already5chosen@yahoo.com> writes:
> On Sun, 15 Oct 2023 05:26:44 -0000 (UTC)
> Kaz Kylheku <864-117-4973@kylheku.com> wrote:
>
>> On 2023-10-11, Kaz Kylheku <864-117-4973@kylheku.com> wrote:
>>
>>> E.g. 32 bit code:
>>>
>>> uint32_t fill_mask_down(uint32_t x)
>>> {
>>> x |= x >> 1; // e.g. 1000...0000 -> 1100...0000
>>> x |= x >> 2; // e.g. 1100...0000 -> 1111...0000
>>> x |= x >> 4; // e.g. 11110000... -> 11111111...
>>> x |= x >> 8;
>>> x |= x >> 16;
>>>
>>> return x;
>>> }
>>>
>>> Thus:
>>>
>>> uint32_t isolate_highest_bit(uint32_t x)
>>> {
>>> uint32_t m = fill_mask_down(x);
>>> return m ^ (m >> 1);
>>> }
>>
>> I guess this went over people's heads?
>
> Not over my head.
> My O(log(N)) variant is inspired by your solution.
> It is essentially the same like yours, with only difference that I
> tried to meet requirement of Tim Rentsch to code it in a manner
> independent of number of bits in x.
> Sorry for not giving you the credit.
Two short comments on the above. One, the requirement to make
the algorithm size independent came from the OP, not me; I just
expressed that in a way that is apparent in the C code. Two, I
had assumed that most people would already be familiar with the
expanding-shift-and-oring technique; it was one of several I
had already coded before the humorous post with my "favorite"
version.
> When coded as below, it become more clear that idea is the same
> like yours:
>
> T
> highest_bit_set( T u ){
> for (int rshift = 1; ((u+1) & u) != 0; rshift += rshift)
> u |= (u >> rshift);
> return u ^ (u>>1);
> }
I expect to have comments on this and your earlier posted
version soon.
Back to comp.lang.c | Previous | Next — Previous in thread | Find similar
Re: portable way to get highest bit set? Michael S <already5chosen@yahoo.com> - 2023-10-15 11:35 +0300 Re: portable way to get highest bit set? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2023-10-15 02:52 -0700
csiph-web