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


Groups > comp.lang.c > #237840

Re: portable way to get highest bit set?

Path csiph.com!weretis.net!feeder8.news.weretis.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 Thu, 12 Oct 2023 07:58:18 -0700
Organization A noiseless patient Spider
Lines 62
Message-ID <86zg0nanw5.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> <86cyxkb2ka.fsf@linuxsc.com> <20231012141719.99f5a10ec921db3ee6f7d948@g{oogle}mail.com> <864jiwaqic.fsf@linuxsc.com> <ffTVM.60121$8fO.5299@fx15.iad>
MIME-Version 1.0
Content-Type text/plain; charset=us-ascii
Injection-Info dont-email.me; posting-host="8a29b169449756014084ae3fe2a46ca6"; logging-data="2694084"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19S6C2P7tnGTFzIf3UeM/ctPYvgp0zvMl8="
User-Agent Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock sha1:o6SAcG+GFr1tFXWf1yi/lkTsnw8= sha1:Z7GMdWlbOx2JFChAExSyQGWx1yQ=
Xref csiph.com comp.lang.c:237840

Show key headers only | View raw


scott@slp53.sl.home (Scott Lurndal) writes:

> Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
>
>> Anton Shepelev <anton.txt@g{oogle}mail.com> writes:
>>
>>> Tim Rentsch:
>>>
>>>> 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.
>>>
>>> Well, I could use macros, or the largest integer type, or:
>>
>> Neither necessary nor sufficient.  The code should work for
>> the type T being __uint128_t, which is larger than uintmax_t.
>>
>>> #include <stddef.h>
>>> #include <limits.h>
>>>
>>> [..examine value character-by-character, including
>>>    determining if little-endian encoding is used..]
>>
>> You're making things more complicated than they need to be.
>> Looking at character values rather than just values of the type
>> involved is almost guaranteed to have potential representation
>> issues.  For example, little endian and big endian are not the
>> only choices possible for endianness.
>>
>> Below is a proposed solution that mostly works.  What problems
>> does it have, and how can they be fixed?  And can we find
>> something better?
>>
>>    /* The type T has been supplied somewhere upstream.  It */
>>    /* is an unsigned type, and possibly outside the realm  */
>>    /* of 'integer types' as defined by the C standard, as  */
>>    /* for example __uint128_t.                             */
>>
>>    T
>>    highest_bit_set( T u ){
>>        T r = 1;
>>        while(  u>>1 > r  )  r <<= 1;
>>        return  r;
>>    }
>
> Given a reasonable compiler (gcc or clang), I'd use
>
> /**
>  * Return the 1-relative bit number of the
>  * highest set bit.   If no bits are set,
>  * return zero.
>  */
> size_t
> highest_bit_set(unsigned long long u)
> {
>     return u ? 64ul - __builtin_clzll(u) : 0ul;
> }

That gives a solution to a different problem.

When you have code that solves the problem being
discussed, I'm interested to see it.

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


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