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


Groups > comp.lang.c > #271378 > unrolled thread

Re: portable way to get highest bit set?

Started byTim Rentsch <tr.17687@z991.linuxsc.com>
First post2023-10-14 19:05 -0700
Last post2023-10-15 18:05 +0200
Articles 5 — 2 participants

Back to article view | Back to comp.lang.c

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: portable way to get highest bit set? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2023-10-14 19:05 -0700
    Re: portable way to get highest bit set? jak <nospam@please.ty> - 2023-10-15 06:00 +0200
      Re: portable way to get highest bit set? jak <nospam@please.ty> - 2023-10-15 07:03 +0200
        Re: portable way to get highest bit set? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2023-10-15 01:35 -0700
          Re: portable way to get highest bit set? jak <nospam@please.ty> - 2023-10-15 18:05 +0200

#271378 — Re: portable way to get highest bit set?

FromTim Rentsch <tr.17687@z991.linuxsc.com>
Date2023-10-14 19:05 -0700
SubjectRe: portable way to get highest bit set?
Message-ID<86cyxgabe6.fsf@linuxsc.com>
jak <nospam@please.ty> writes:

> unsigned long h_bit(unsigned long val)
> {
>     int i;
>     for(i = 0; val > 1; val /= 2, i++);
>     return val << i;
> }

A nice compact solution.  I like the way it
naturally deals with the case when val is zero.

[toc] | [next] | [standalone]


#272267

Fromjak <nospam@please.ty>
Date2023-10-15 06:00 +0200
Message-ID<ugfo4p$ameh$1@dont-email.me>
In reply to#271378
Tim Rentsch ha scritto:
> jak <nospam@please.ty> writes:
> 
>> unsigned long h_bit(unsigned long val)
>> {
>>      int i;
>>      for(i = 0; val > 1; val /= 2, i++);
>>      return val << i;
>> }
> 
> A nice compact solution.  I like the way it
> naturally deals with the case when val is zero.
> 

This is not true because if Val is 0 also the bit shifted to left is,
so also its result is 0. This one of the reason why the exit from the
loop is greater than 1.

[toc] | [prev] | [next] | [standalone]


#272324

Fromjak <nospam@please.ty>
Date2023-10-15 07:03 +0200
Message-ID<ugfrqs$ba5k$1@dont-email.me>
In reply to#272267
jak ha scritto:
> Tim Rentsch ha scritto:
>> jak <nospam@please.ty> writes:
>>
>>> unsigned long h_bit(unsigned long val)
>>> {
>>>      int i;
>>>      for(i = 0; val > 1; val /= 2, i++);
>>>      return val << i;
>>> }
>>
>> A nice compact solution.  I like the way it
>> naturally deals with the case when val is zero.
>>
> 
> This is not true because if Val is 0 also the bit shifted to left is,
> so also its result is 0. This one of the reason why the exit from the
> loop is greater than 1.
> 

Sorry about the answer. I have translated your comment badly.

[toc] | [prev] | [next] | [standalone]


#272711

FromTim Rentsch <tr.17687@z991.linuxsc.com>
Date2023-10-15 01:35 -0700
Message-ID<86r0lw8erq.fsf@linuxsc.com>
In reply to#272324
jak <nospam@please.ty> writes:

> jak ha scritto:
>
>> Tim Rentsch ha scritto:
>>
>>> jak <nospam@please.ty> writes:
>>>
>>>> unsigned long h_bit(unsigned long val)
>>>> {
>>>>  int i;
>>>>  for(i = 0; val > 1; val /= 2, i++);
>>>>  return val << i;
>>>> }
>>>
>>> A nice compact solution.  I like the way it
>>> naturally deals with the case when val is zero.
>>
>> This is not true because if Val is 0 also the bit shifted to left
>> is, so also its result is 0.  This one of the reason why the exit
>> from the loop is greater than 1.
>
> Sorry about the answer.  I have translated your comment badly.

No worries.  I see how it works, and it's cool.

[toc] | [prev] | [next] | [standalone]


#276444

Fromjak <nospam@please.ty>
Date2023-10-15 18:05 +0200
Message-ID<ugh2jp$jant$1@dont-email.me>
In reply to#272711
Tim Rentsch ha scritto:
> jak <nospam@please.ty> writes:
> 
>> jak ha scritto:
>>
>>> Tim Rentsch ha scritto:
>>>
>>>> jak <nospam@please.ty> writes:
>>>>
>>>>> unsigned long h_bit(unsigned long val)
>>>>> {
>>>>>   int i;
>>>>>   for(i = 0; val > 1; val /= 2, i++);
>>>>>   return val << i;
>>>>> }
>>>>
>>>> A nice compact solution.  I like the way it
>>>> naturally deals with the case when val is zero.
>>>
>>> This is not true because if Val is 0 also the bit shifted to left
>>> is, so also its result is 0.  This one of the reason why the exit
>>> from the loop is greater than 1.
>>
>> Sorry about the answer.  I have translated your comment badly.
> 
> No worries.  I see how it works, and it's cool.
> 

In addition, GCC translates this code into a really simple way:

h_bit:
     mov rax, rcx
     cmp rcx, 1
     jbe .L1
     xor ecx, ecx
.L3:
     shr rax
     add ecx, 1
     cmp rax, 1
     jne .L3
     sal rax, cl
.L1:
     ret

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c


csiph-web