Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

Always try to do things in chronological order; it's less confusing that way.


devel / comp.lang.c / Re: portable way to get highest bit set?

SubjectAuthor
* portable way to get highest bit set?candycanearter07
+* Re: portable way to get highest bit set?Anton Shepelev
|+* Re: portable way to get highest bit set?David Brown
||`* Re: portable way to get highest bit set?Anton Shepelev
|| `* Re: portable way to get highest bit set?David Brown
||  `* Re: portable way to get highest bit set?Anton Shepelev
||   `* Re: portable way to get highest bit set?David Brown
||    +- Re: portable way to get highest bit set?Anton Shepelev
||    `* Re: portable way to get highest bit set?Ben Bacarisse
||     `- Re: portable way to get highest bit set?David Brown
|`* Re: portable way to get highest bit set?candycanearter07
| +- Re: portable way to get highest bit set?David Brown
| +* Re: portable way to get highest bit set?Ian C
| |+- Re: portable way to get highest bit set?Tim Rentsch
| |`- Re: portable way to get highest bit set?candycanearter07
| +* Re: portable way to get highest bit set?Tim Rentsch
| |+* Re: portable way to get highest bit set?David Brown
| ||+* Re: portable way to get highest bit set?Ben Bacarisse
| |||+* Re: portable way to get highest bit set?David Brown
| ||||`* Re: portable way to get highest bit set?Ben Bacarisse
| |||| `- Re: portable way to get highest bit set?David Brown
| |||`- Re: portable way to get highest bit set?Tim Rentsch
| ||+* Re: portable way to get highest bit set?Michael S
| |||`* Re: portable way to get highest bit set?Michael S
| ||| `- Re: portable way to get highest bit set?Anton Shepelev
| ||+- Re: portable way to get highest bit set?Michael S
| ||+- Re: portable way to get highest bit set?Michael S
| ||`- Re: portable way to get highest bit set?Michael S
| |`* Re: portable way to get highest bit set?Anton Shepelev
| | +- Re: portable way to get highest bit set?Michael S
| | `* Re: portable way to get highest bit set?Tim Rentsch
| |  `* Re: portable way to get highest bit set?Anton Shepelev
| |   `* Re: portable way to get highest bit set?Tim Rentsch
| |    +* Re: portable way to get highest bit set?Scott Lurndal
| |    |`- Re: portable way to get highest bit set?Tim Rentsch
| |    +* Re: portable way to get highest bit set?Michael S
| |    |`* Re: portable way to get highest bit set?Tim Rentsch
| |    | +* Re: portable way to get highest bit set?Michael S
| |    | |`* Re: portable way to get highest bit set?Tim Rentsch
| |    | | +* Re: portable way to get highest bit set?Michael S
| |    | | |`* Re: portable way to get highest bit set?Tim Rentsch
| |    | | | +- Re: portable way to get highest bit set?Branimir Maksimovic
| |    | | | `* Re: portable way to get highest bit set?Michael S
| |    | | |  `* Re: portable way to get highest bit set?Michael S
| |    | | |   `* Re: portable way to get highest bit set?Tim Rentsch
| |    | | |    `* Re: portable way to get highest bit set?Michael S
| |    | | |     +- Re: portable way to get highest bit set?Chris M. Thomasson
| |    | | |     `* Re: portable way to get highest bit set?Tim Rentsch
| |    | | |      `* Re: portable way to get highest bit set?Keith Thompson
| |    | | |       `- Re: portable way to get highest bit set?Tim Rentsch
| |    | | `* Re: portable way to get highest bit set?jak
| |    | |  `* Re: portable way to get highest bit set?Tim Rentsch
| |    | |   `* Re: portable way to get highest bit set?jak
| |    | |    `* Re: portable way to get highest bit set?Tim Rentsch
| |    | |     `* Re: portable way to get highest bit set?jak
| |    | |      +* Re: portable way to get highest bit set?Tim Rentsch
| |    | |      |`* Re: portable way to get highest bit set?jak
| |    | |      | `- Re: portable way to get highest bit set?Tim Rentsch
| |    | |      `* Re: portable way to get highest bit set?Kaz Kylheku
| |    | |       `* Re: portable way to get highest bit set?Bart
| |    | |        `* Re: portable way to get highest bit set?Michael S
| |    | |         `* Re: portable way to get highest bit set?Ben Bacarisse
| |    | |          `- Re: portable way to get highest bit set?Michael S
| |    | +* Re: portable way to get highest bit set?Anton Shepelev
| |    | |+* Re: portable way to get highest bit set?Tim Rentsch
| |    | ||`* Re: portable way to get highest bit set?Anton Shepelev
| |    | || +- Re: portable way to get highest bit set?Tim Rentsch
| |    | || `* Re: portable way to get highest bit set?Anton Shepelev
| |    | ||  +- Re: portable way to get highest bit set?Bart
| |    | ||  `* Re: portable way to get highest bit set?Tim Rentsch
| |    | ||   `* Re: portable way to get highest bit set?Anton Shepelev
| |    | ||    `- Re: portable way to get highest bit set?Tim Rentsch
| |    | |`* Re: portable way to get highest bit set?Tim Rentsch
| |    | | +* Re: portable way to get highest bit set?Michael S
| |    | | |+* Re: portable way to get highest bit set?Tim Rentsch
| |    | | ||`- Re: portable way to get highest bit set?Michael S
| |    | | |`* Re: portable way to get highest bit set?Anton Shepelev
| |    | | | `- Re: portable way to get highest bit set?Tim Rentsch
| |    | | `- Re: portable way to get highest bit set?David Brown
| |    | `* Re: portable way to get highest bit set?Michael S
| |    |  +- Re: portable way to get highest bit set?Michael S
| |    |  +- False positives in spam filter? (was: Re: portable way to getRay Banana
| |    |  +- Re: portable way to get highest bit set?Anton Shepelev
| |    |  `- Re: portable way to get highest bit set?Ben Bacarisse
| |    `* Re: portable way to get highest bit set?Anton Shepelev
| |     `* Re: portable way to get highest bit set?Tim Rentsch
| |      `* Re: portable way to get highest bit set?Anton Shepelev
| |       `- Re: portable way to get highest bit set?Tim Rentsch
| +* Re: portable way to get highest bit set?Richard Harnden
| |`* Re: portable way to get highest bit set?candycanearter07
| | `* Re: portable way to get highest bit set?Bart
| |  +- Re: portable way to get highest bit set?Keith Thompson
| |  `- Re: portable way to get highest bit set?candycanearter07
| `- Re: portable way to get highest bit set?Tim Rentsch
+- Re: portable way to get highest bit set?Ben Bacarisse
+* Re: portable way to get highest bit set?Lew Pitcher
|`* Re: portable way to get highest bit set?Lew Pitcher
| `* Re: portable way to get highest bit set?Lew Pitcher
|  `* Re: portable way to get highest bit set?Tim Rentsch
|   `* Re: portable way to get highest bit set?Bart
|    `* Re: portable way to get highest bit set?Tim Rentsch
+- Re: portable way to get highest bit set?Kaz Kylheku
+* Re: portable way to get highest bit set?jak
+* Re: portable way to get highest bit set?Blue-Maned_Hawk
`* Re: portable way to get highest bit set?Kaz Kylheku

Pages:12345678
Re: portable way to get highest bit set?

<ug8r5q$2gqj4$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29783&group=comp.lang.c#29783

  copy link   Newsgroups: comp.lang.c alt.comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: nos...@please.ty (jak)
Newsgroups: comp.lang.c,alt.comp.lang.c
Subject: Re: portable way to get highest bit set?
Date: Thu, 12 Oct 2023 15:09:13 +0200
Organization: A noiseless patient Spider
Lines: 55
Message-ID: <ug8r5q$2gqj4$1@dont-email.me>
References: <ug5gvh$1jkar$3@dont-email.me> <ug70cu$219ov$1@dont-email.me>
<878r884vi5.fsf@bsb.me.uk> <ug86d3$2c9mp$1@dont-email.me>
<87edi0qide.fsf@bsb.me.uk>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 12 Oct 2023 13:09:14 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="e61172f60fef4771e29be2ec94b64ca0";
logging-data="2648676"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/hJFbCTlCV6J5gAS5uTaD1"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Firefox/91.0 SeaMonkey/2.53.17.1
Cancel-Lock: sha1:53WM/jCT3JWy6X9+7GUKFKzllqo=
In-Reply-To: <87edi0qide.fsf@bsb.me.uk>
 by: jak - Thu, 12 Oct 2023 13:09 UTC

Ben Bacarisse ha scritto:
> jak <nospam@please.ty> writes:
>
>> Ben Bacarisse ha scritto:
>>> jak <nospam@please.ty> writes:
>>>
>>>> candycanearter07 ha scritto:
>>>>> Hi,
>>>>> What is the best/most portable way to get the highest bit set?
>>>>> ie. 011010001
>>>>> to  010000000
>>>>
>>>> Hi,
>>>> I don't think it's the best but the most portable could be the
>>>> mathematical approach:
>>>>
>>>> #include <stdio.h>
>>>> #include <math.h>
>>>>
>>>> int main()
>>>> {
>>>> unsigned long val = 3000, ret;
>>> Test case: val = 0xFFFFFFFFFFFFFFFF
>>
>> This is due to the approximation of the floating point:
>
> I know the reason. I was just pointing out that you need to at least
> test the boundary cases!
>
You were really kind to answer. I was also reading about 'feivatround'
and like you I found the 'short blanket'. I think there are less
complicated and, however, easily portable methods. Such as this:

#include <stdio.h>
#include <math.h>

unsigned long h_bit(unsigned long val)
{ int i;
for(i = -1; val; val /= 2, i++);
return (unsigned long)pow(2., (double)i);
}

int main()
{ unsigned long val = ~0UL, ret;

ret = h_bit(val);
printf("\nv: %lX r: %lX", val, ret);

return 0;
}

Finally, the simplest things seem to be the best.
Thanks again.

Re: portable way to get highest bit set?

<ug8rmd$2gufn$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29784&group=comp.lang.c#29784

  copy link   Newsgroups: comp.lang.c alt.comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: nos...@please.ty (jak)
Newsgroups: comp.lang.c,alt.comp.lang.c
Subject: Re: portable way to get highest bit set?
Date: Thu, 12 Oct 2023 15:18:04 +0200
Organization: A noiseless patient Spider
Lines: 89
Message-ID: <ug8rmd$2gufn$1@dont-email.me>
References: <ug5gvh$1jkar$3@dont-email.me> <ug70cu$219ov$1@dont-email.me>
<878r884vi5.fsf@bsb.me.uk> <ug86d3$2c9mp$1@dont-email.me>
<87edi0qide.fsf@bsb.me.uk> <ug8ll6$2fjms$1@dont-email.me>
<20231012153657.000056bc@yahoo.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 12 Oct 2023 13:18:05 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="e61172f60fef4771e29be2ec94b64ca0";
logging-data="2652663"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+gutGAUYZifY/NJKDC0GGx"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Firefox/91.0 SeaMonkey/2.53.17.1
Cancel-Lock: sha1:NkaNBBGktjhkSH0+TQEybuFAf5A=
In-Reply-To: <20231012153657.000056bc@yahoo.com>
 by: jak - Thu, 12 Oct 2023 13:18 UTC

Michael S ha scritto:
> On Thu, 12 Oct 2023 13:35:01 +0200
> jak <nospam@please.ty> wrote:
>
>> Ben Bacarisse ha scritto:
>>> jak <nospam@please.ty> writes:
>>>
>>>> Ben Bacarisse ha scritto:
>>>>> jak <nospam@please.ty> writes:
>>>>>
>>>>>> candycanearter07 ha scritto:
>>>>>>> Hi,
>>>>>>> What is the best/most portable way to get the highest bit set?
>>>>>>> ie. 011010001
>>>>>>> to  010000000
>>>>>>
>>>>>> Hi,
>>>>>> I don't think it's the best but the most portable could be the
>>>>>> mathematical approach:
>>>>>>
>>>>>> #include <stdio.h>
>>>>>> #include <math.h>
>>>>>>
>>>>>> int main()
>>>>>> {
>>>>>> unsigned long val = 3000, ret;
>>>>> Test case: val = 0xFFFFFFFFFFFFFFFF
>>>>
>>>> This is due to the approximation of the floating point:
>>>
>>> I know the reason. I was just pointing out that you need to at
>>> least test the boundary cases!
>>>
>>
>> I am sure you knew and I have verified that the problem persists up to
>> "~0UL - 10000" but I don't find an elegant way, type independent, to
>> work around... just adding an 'if' or a ternary operator.
>
>
> I don't know what do you consider elegant, but that code solves
> problem of rounding:
> int save = fegetround();
> fesetround(FE_TOWARDZERO);
> volatile double dval = val;
> fesetround(save);
> Pay attention that without 'volatile' it is not guaranteed to work which
> I personally consider as a bug in the Standard.
>
> Also frexp()/ldexp() would be a little less slow than yours log()/pow().
> int e;
> frexp(dval, &e);
> ldexp(0.5, e);
> But it still would not work at val=0.
>
> I don't know what do you consider elegant, but that code solves
> problem of rounding:
> int save = fegetround();
> fesetround(FE_TOWARDZERO);
> volatile double dval = val;
> fesetround(save);
> Pay attention that without 'volatile' it is not guaranteed to work which
> I personally consider as a bug in the Standard.
>
> Also frexp()/ldexp() would be a little less slow than yours log()/pow().
> int e;
> frexp(dval, &e);
> ldexp(0.5, e);
> But it still would not work at val=0.
>
> I don't know what do you consider elegant, but that code solves
> problem of rounding:
> int save = fegetround();
> fesetround(FE_TOWARDZERO);
> volatile double dval = val;
> fesetround(save);
> Pay attention that without 'volatile' it is not guaranteed to work which
> I personally consider as a bug in the Standard.
>
> Also frexp()/ldexp() would be a little less slow than yours log()/pow().
> int e;
> frexp(dval, &e);
> ldexp(0.5, e);
> But it still would not work at val=0.
>
>
Oops ... I was wrong branch. I hung the answer to the previous branch.
Excuse me. (jak: Thu, 12 Oct 2023 13:09:14 UTC)

Re: portable way to get highest bit set?

<pan$325f3$70a576a8$3150c04a$26db7b31@invalid.invalid>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29787&group=comp.lang.c#29787

  copy link   Newsgroups: comp.lang.c alt.comp.lang.c
Path: i2pn2.org!i2pn.org!news.hispagatos.org!eternal-september.org!news.eternal-september.org!bluemanedhawk.eternal-september.org!.POSTED!not-for-mail
From: bluemane...@invalid.invalid (Blue-Maned_Hawk)
Newsgroups: comp.lang.c,alt.comp.lang.c
Subject: Re: portable way to get highest bit set?
Date: Thu, 12 Oct 2023 13:42:03 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 38
Message-ID: <pan$325f3$70a576a8$3150c04a$26db7b31@invalid.invalid>
References: <ug5gvh$1jkar$3@dont-email.me>
<pan$e0805$faa9a91a$a60476dc$4a90cfa8@invalid.invalid>
<87h6mwu9rk.fsf@nosuchdomain.example.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 12 Oct 2023 13:42:03 -0000 (UTC)
Injection-Info: bluemanedhawk.eternal-september.org; posting-host="f9e9af72c89db586b7218acc30cacee0";
logging-data="2632412"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/3+jRST5jIKr00S1+ctFdo8eEGxn53fco="
User-Agent: Pan/0.154 (Izium; 517acf4)
Cancel-Lock: sha1:+JqAqn3lF49yAWEPCkNV5wqCC7s=
Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAIAAADYYG7QAAACh0lEQVRYw71Z21bD
MAzzevbfkr4cHjrSXJyL044+MDa6WLEl2SkvkrZ1AbAvXO+bUGSCPYnsuIVGMpm
ZLnjX718GhAKNsp8lON2F9VrhELwIgJlBepkZjA78rVK+FkmNhEJK76UsJlz8+E
rJsjrpYouhLo/SC6qPHgakFOR8wV9+8rCfO/I/oVnmUZUp42/LW2XkLj9TCFNM9
jp5g2EmHZgpYZjCOkYU7sXVogRylJqpdggoFLG1g09Flah/7kErCxzR9HgXPYsq
0glb9cxjIz2Vsk9AmAoCSxECpD713joMKjQqLAtmMqJmXjdVvlMnMQCVITotJd1
z+fh1f1NNo+vuc1KnhWUmY7t03vydTud9BbXCtN3L2PL3bK7JCNG0GHzuZxafyB
fxevCxpm1vrwZltqw6SILCcdoCE6PGQC8wZWDA9Or7Qp5s3lAZezys0nDazs9S9
R0TjwEiksRxLkNPC1NMMWPs1bj0Ei0Yuo+JVtFLuzP1NRJ16qXWN8DhhtmS4PDg
O6mqRxs4bEJrYt087mSIow/1VzW2oFlMQuiuIy/KsUagvhdw6hSjJGlIavbLF8x
j3X47bccLcUSi0dkWh1nUZNhANT1tHKUXrNxNLbd9KPb9wDDVrKwmPQMOPQ1oy6
k5I1DwzDeRJd3jVIhDAUxq3ngzJG4CCkNXZxZVMcjefoK2J0gUY2S3rxz/RuTFx
2zHd9U+obimJXMG4edsk/2j5pTU5G1MmzbRLxkfq5EiT1GGsidvMGzi+1goGb2l
GCrN+nGnV8xj3q3JLRDVPL96vUc7Z4aJ3TN1mVqWAMJMfG+Jxh6TQqP+92iZkCU
xtglds1AB6r0aiSHKcnFck+p/c/0CbacFLQcajGcAAAAASUVORK5CYII=
X-Face: Llanfair­pwllgwyng
yll
­gogeryÂÃ
? ?­chwyrnÂÂÂÂÂÂ
­drobwll­llanÃ
‚Ã
ƒƒƒ‚­tysilioÂÂÂÃ
‚­gogo­goch
 by: Blue-Maned_Hawk - Thu, 12 Oct 2023 13:42 UTC

Keith Thompson wrote:

> A solution that depends on a header that's introduced in C23, a standard
> that hasn't been published yet, is hardly portable.

Well, it will be in 2024.

> BTW, here's what your signature looks like in my newsreader (Gnus):
>
> Blue-Maned_HawkÃ\203\203\203\203Ã\203\203\202Ã\203\202Ã\202¢
\224\202shortens
> to HawkÃ\203\203\203\203Ã\203\203\202Ã\203\202Ã\202¢\224\202/
>
blu.mÃ\203\203\203\203\211\233in.dÃ\203\203\203\203\212Ã\203\203\203\202Ã\203\203\202Ã\203\202Ã\202°ak/
> Ã\203\203\203\203Ã\203\203\202Ã\203\202Ã\202¢\224\202he/him/his/
himself/Mr.
> bluemanedhawk.github.io Minor vehicular events!
>
> And in Thunderbird:
>
> Blue-Maned_HawkÃÃÃâshortens to HawkÃÃÃâ/
> blu.mÃin.dÃÃÃÃðak/
> ÃÃÃâhe/him/his/himself/Mr.
> bluemanedhawk.github.io Minor vehicular events!

Blame Pan for corrupting it. That was meant to have its fields separated
by the │ character, including a pronunciation explanation field that has
IPA characters in it.

--
Blue-Maned_Hawk│shortens to
Hawk│/
blu.mɛin.dÊ°ak/
│he/him/his/himself/Mr.
bluemanedhawk.github.io
In eggshells and mayo!

Re: portable way to get highest bit set?

<864jiwaqic.fsf@linuxsc.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29788&group=comp.lang.c#29788

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: tr.17...@z991.linuxsc.com (Tim Rentsch)
Newsgroups: comp.lang.c
Subject: Re: portable way to get highest bit set?
Date: Thu, 12 Oct 2023 07:01:47 -0700
Organization: A noiseless patient Spider
Lines: 40
Message-ID: <864jiwaqic.fsf@linuxsc.com>
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>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: dont-email.me; posting-host="8a29b169449756014084ae3fe2a46ca6";
logging-data="2670830"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18KBYwh5Dku9+qsigd5jFosHCAtfl0v4SA="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:fGvueG9WkT8FnaErwJZmAJsJHug=
sha1:pypdNzu9uECwqyhH2zVCcdB/Gv0=
 by: Tim Rentsch - Thu, 12 Oct 2023 14:01 UTC

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;
}

Re: portable way to get highest bit set?

<ffTVM.60121$8fO.5299@fx15.iad>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29790&group=comp.lang.c#29790

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!paganini.bofh.team!3.us.feeder.erje.net!feeder.erje.net!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx15.iad.POSTED!not-for-mail
X-newsreader: xrn 9.03-beta-14-64bit
Sender: scott@dragon.sl.home (Scott Lurndal)
From: sco...@slp53.sl.home (Scott Lurndal)
Reply-To: slp53@pacbell.net
Subject: Re: portable way to get highest bit set?
Newsgroups: comp.lang.c
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>
Lines: 54
Message-ID: <ffTVM.60121$8fO.5299@fx15.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Thu, 12 Oct 2023 14:18:51 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Thu, 12 Oct 2023 14:18:51 GMT
X-Received-Bytes: 2644
 by: Scott Lurndal - Thu, 12 Oct 2023 14:18 UTC

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;
}

Re: portable way to get highest bit set?

<20231012173021.0000149c@yahoo.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29791&group=comp.lang.c#29791

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.chmurka.net!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: already5...@yahoo.com (Michael S)
Newsgroups: comp.lang.c
Subject: Re: portable way to get highest bit set?
Date: Thu, 12 Oct 2023 17:30:21 +0300
Organization: A noiseless patient Spider
Lines: 61
Message-ID: <20231012173021.0000149c@yahoo.com>
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>
MIME-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Injection-Info: dont-email.me; posting-host="df0da282e3f59609704589ea08055b8e";
logging-data="2631684"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18MZX4VGQ/pYsMPC3r0pZcqN+WJOynKcJw="
Cancel-Lock: sha1:ywNyOwbOVDbV2VRVYjqLkl964AY=
X-Newsreader: Claws Mail 3.19.1 (GTK+ 2.24.33; x86_64-w64-mingw32)
 by: Michael S - Thu, 12 Oct 2023 14:30 UTC

On Thu, 12 Oct 2023 07:01:47 -0700
Tim Rentsch <tr.17687@z991.linuxsc.com> wrote:

> 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;
> }

That code does not look like working.
That is:

T
highest_bit_set1( T u ){
T r = 0;
while (u > r) r = r*2+1;
return r ^ (r>>1);
}

T
highest_bit_set1( T u ){
T r = 0;
while( u > r) r = r*2+1;
return r ^ (r>>1);
}

Re: portable way to get highest bit set?

<86zg0nanw5.fsf@linuxsc.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29793&group=comp.lang.c#29793

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: tr.17...@z991.linuxsc.com (Tim Rentsch)
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>
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=
 by: Tim Rentsch - Thu, 12 Oct 2023 14:58 UTC

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.

Re: portable way to get highest bit set?

<86v8bbanjv.fsf@linuxsc.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29794&group=comp.lang.c#29794

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: tr.17...@z991.linuxsc.com (Tim Rentsch)
Newsgroups: comp.lang.c
Subject: Re: portable way to get highest bit set?
Date: Thu, 12 Oct 2023 08:05:40 -0700
Organization: A noiseless patient Spider
Lines: 65
Message-ID: <86v8bbanjv.fsf@linuxsc.com>
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> <20231012173021.0000149c@yahoo.com>
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="U2FsdGVkX18+jr3vh7P7jlfvQHrGAI1IFnlYE4hqnHw="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:Ub2bNXzxAtVFV+EHu0kx3eJrTvk=
sha1:S9AYgHMT8jCmmy25dqBltRqddJM=
 by: Tim Rentsch - Thu, 12 Oct 2023 15:05 UTC

Michael S <already5chosen@yahoo.com> writes:

> On Thu, 12 Oct 2023 07:01:47 -0700
> Tim Rentsch <tr.17687@z991.linuxsc.com> wrote:
>
>> 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;
>> }
>
> That code does not look like working.

Yes, deliberately so. I didn't want to give working
code yet, so other people could work on the problem.

> That is:
>
> T
> highest_bit_set1( T u ){
> T r = 0;
> while (u > r) r = r*2+1;
> return r ^ (r>>1);
> }

As expected, this works. I like the way it naturally
accommodates a zero input.

Can you find a different solution that works in
logarithmic time rather than linear time?

Re: portable way to get highest bit set?

<20231012183135.a6faa3027ccac0820b24ad96@g{oogle}mail.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29796&group=comp.lang.c#29796

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: anton....@g{oogle}mail.com (Anton Shepelev)
Newsgroups: comp.lang.c
Subject: Re: portable way to get highest bit set?
Date: Thu, 12 Oct 2023 18:31:35 +0300
Organization: A noiseless patient Spider
Lines: 72
Message-ID: <20231012183135.a6faa3027ccac0820b24ad96@g{oogle}mail.com>
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>
MIME-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Injection-Info: dont-email.me; posting-host="fb1b492396d4ccc86f11222f164fb6bd";
logging-data="2708462"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19vCSWDNckYMVZqQZjEaqAV9V7Q+Uzy0hU="
Cancel-Lock: sha1:YqLMmTTd63Baio+6FW66D251HYs=
X-Newsreader: Sylpheed 3.7.0 (GTK+ 2.24.30; i686-pc-mingw32)
 by: Anton Shepelev - Thu, 12 Oct 2023 15:31 UTC

Tim Rentsch:

> The code should work for the type T being __uint128_t,
> which is larger than uintmax_t.

I was in my C89 mode:

#include <stdio.h>
#include <stddef.h>
#include <limits.h>

char is_le()
{ const int v = 1;
const char* p = (unsigned char*)&v;
return *p == 0;
}

unsigned char highest_bit_1( unsigned char byte )
{ int i ;
unsigned char mask = 1 << (CHAR_BIT-1);
for( i = 0; i <= CHAR_BIT; i += 1 )
{ if( byte & mask )
{ break; }
mask >>= 1;
}
return mask;
}

/* sz must be the size of the value /and/ of the buffer: */
void highest_bit_2( void* val, void* buffer, size_t sz )
{ int i,inc;
unsigned char *pval = val;
unsigned char *pbuf = buffer;
unsigned char c ;
char le = is_le();
char set;

if( le )
{ inc = 1; } else
{ inc = -1; pval += sz-1; pbuf += sz-1; }

set = 0;
for( i = 0; i < sz; i += 1 )
{ c = *pval ;
if( !set && c != 0 )
{ *pbuf = highest_bit_1( c );
set = 1;
}
else *pbuf = 0;
pval += inc; pbuf += inc;
}
}

void test( long int val )
{ char buf[sizeof(long int)];

highest_bit_2(&val, &buf, sizeof( long int ));
printf("Value: %9x, High-bit mask: %9x\n", val, *(long unsigned int *)&buf);
}

int main(void)
{ test(0x0); test(0x1); test(0x2); test(0x3); test(0x4); test(0x5);

test((1<<17)-1); test((1<<17)); test((1<<17)+1);
}

....printf("%x"), anybody?

--
() ascii ribbon campaign -- against html e-mail
/\ www.asciiribbon.org -- against proprietary attachments

Re: portable way to get highest bit set?

<20231012191641.00007493@yahoo.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29797&group=comp.lang.c#29797

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: already5...@yahoo.com (Michael S)
Newsgroups: comp.lang.c
Subject: Re: portable way to get highest bit set?
Date: Thu, 12 Oct 2023 19:16:41 +0300
Organization: A noiseless patient Spider
Lines: 82
Message-ID: <20231012191641.00007493@yahoo.com>
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>
<20231012173021.0000149c@yahoo.com>
<86v8bbanjv.fsf@linuxsc.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Injection-Info: dont-email.me; posting-host="df0da282e3f59609704589ea08055b8e";
logging-data="2631684"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/T0CimXpEkKn5RK8vpec/kPxyPzXz9g3g="
Cancel-Lock: sha1:Buv7LI0pR+eJLlzL5TH4Yo5SujM=
X-Newsreader: Claws Mail 3.19.1 (GTK+ 2.24.33; x86_64-w64-mingw32)
 by: Michael S - Thu, 12 Oct 2023 16:16 UTC

On Thu, 12 Oct 2023 08:05:40 -0700
Tim Rentsch <tr.17687@z991.linuxsc.com> wrote:

> Michael S <already5chosen@yahoo.com> writes:
>
> > On Thu, 12 Oct 2023 07:01:47 -0700
> > Tim Rentsch <tr.17687@z991.linuxsc.com> wrote:
> >
> >> 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;
> >> }
> >
> > That code does not look like working.
>
> Yes, deliberately so. I didn't want to give working
> code yet, so other people could work on the problem.
>
> > That is:
> >
> > T
> > highest_bit_set1( T u ){
> > T r = 0;
> > while (u > r) r = r*2+1;
> > return r ^ (r>>1);
> > }
>
> As expected, this works. I like the way it naturally
> accommodates a zero input.
>
> Can you find a different solution that works in
> logarithmic time rather than linear time?

Naturally, I can. But the code is not as easy to comprehend as I would
like. And I am not sure that for typical distributions with prevalence
of small numbers it is faster than above variant.

T
highest_bit_set2( T u ){
T v;
for (int rshift = 0; (v = (u >> rshift) >> 1) != 0; rshift = rshift*2+1)
u |= v;
return u ^ (u>>1);
}

Re: portable way to get highest bit set?

<874jivttp9.fsf@nosuchdomain.example.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29801&group=comp.lang.c#29801

  copy link   Newsgroups: comp.lang.c alt.comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c,alt.comp.lang.c
Subject: Re: portable way to get highest bit set?
Date: Thu, 12 Oct 2023 14:30:10 -0700
Organization: None to speak of
Lines: 24
Message-ID: <874jivttp9.fsf@nosuchdomain.example.com>
References: <ug5gvh$1jkar$3@dont-email.me>
<pan$e0805$faa9a91a$a60476dc$4a90cfa8@invalid.invalid>
<87h6mwu9rk.fsf@nosuchdomain.example.com>
<pan$325f3$70a576a8$3150c04a$26db7b31@invalid.invalid>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Info: dont-email.me; posting-host="0d09a1fd828b856f1a82ca58c0c8ac6b";
logging-data="2867779"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+WfHYVcX4cdq1/1Gi3DxU+"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:3wAuCpdeV+k1FsUTthSNbyKMfUA=
sha1:oauk4SGCagyEsE5+cv3QuBW0uME=
 by: Keith Thompson - Thu, 12 Oct 2023 21:30 UTC

Blue-Maned_Hawk <bluemanedhawk@invalid.invalid> writes:
> Keith Thompson wrote:
[...]
>> BTW, here's what your signature looks like in my newsreader (Gnus):
>>
>> Blue-Maned_HawkÃ\203\203\203\203Ã\203\203\202Ã\203\202Ã\202¢ \224\202shortens
[SNIP]
>
> Blame Pan for corrupting it. That was meant to have its fields separated
> by the │ character, including a pronunciation explanation field that has
> IPA characters in it.

OK, I'll take your word for it that Pan is at fault. But I also blame
you for continuing to use a signature that's corrupted before it leaves
your computer. Consider either changing it or figuring out how to get
Pan to handle it correctly (you can use alt.test to test solutions).

I would have contacted you by email if I could. If you want to discuss
this further, email me. I won't continue this discussion here.

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Will write code for food.
void Void(void) { Void(); } /* The recursive call of the void */

Re: portable way to get highest bit set?

<ug9p1o$2nihu$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29802&group=comp.lang.c#29802

  copy link   Newsgroups: comp.lang.c alt.comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: nos...@please.ty (jak)
Newsgroups: comp.lang.c,alt.comp.lang.c
Subject: Re: portable way to get highest bit set?
Date: Thu, 12 Oct 2023 23:39:06 +0200
Organization: A noiseless patient Spider
Lines: 6
Message-ID: <ug9p1o$2nihu$1@dont-email.me>
References: <ug5gvh$1jkar$3@dont-email.me> <ug70cu$219ov$1@dont-email.me>
<878r884vi5.fsf@bsb.me.uk> <ug86d3$2c9mp$1@dont-email.me>
<87edi0qide.fsf@bsb.me.uk> <ug8r5q$2gqj4$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 12 Oct 2023 21:39:04 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="68c4c23a55b8f97ed1f0210d9c005723";
logging-data="2869822"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+8/rRuyoVZ0mIVfv1B5EMj"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Firefox/91.0 SeaMonkey/2.53.17.1
Cancel-Lock: sha1:IYbto6kS9TkJDT2FZZPLSFYLEco=
In-Reply-To: <ug8r5q$2gqj4$1@dont-email.me>
 by: jak - Thu, 12 Oct 2023 21:39 UTC

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

Re: portable way to get highest bit set?

<20231013005031.c7f5a3e09343c73093007b84@gmail.moc>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29803&group=comp.lang.c#29803

  copy link   Newsgroups: comp.lang.c alt.comp.lang.c
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!feeder8.news.weretis.net!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: anton....@gmail.moc (Anton Shepelev)
Newsgroups: comp.lang.c,alt.comp.lang.c
Subject: Re: portable way to get highest bit set?
Date: Fri, 13 Oct 2023 00:50:31 +0300
Organization: A noiseless patient Spider
Lines: 11
Message-ID: <20231013005031.c7f5a3e09343c73093007b84@gmail.moc>
References: <ug5gvh$1jkar$3@dont-email.me>
<pan$e0805$faa9a91a$a60476dc$4a90cfa8@invalid.invalid>
<87h6mwu9rk.fsf@nosuchdomain.example.com>
<pan$325f3$70a576a8$3150c04a$26db7b31@invalid.invalid>
<874jivttp9.fsf@nosuchdomain.example.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Injection-Info: dont-email.me; posting-host="689840a5a7f13bbeb53b380fbd730f87";
logging-data="2871346"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+Ze+/Lfjd/nIxM1rXsulWKsb5gOPLMKG8="
Cancel-Lock: sha1:ibwNPYMumjHJ1yuT+U7Oy4mAiik=
X-Newsreader: Sylpheed 3.7.0 (GTK+ 2.24.30; i686-pc-mingw32)
 by: Anton Shepelev - Thu, 12 Oct 2023 21:50 UTC

Keith Thompson to Blue-Maned_Hawk:

> OK, I'll take your word for it that Pan is at fault. But
> I also blame you for continuing to use a signature that's
> corrupted before it leaves your computer.

I am with Keith.

--
() ascii ribbon campaign -- against html e-mail
/\ www.asciiribbon.org -- against proprietary attachments

Re: portable way to get highest bit set?

<20231013021504.12623444fc7d7fdaab87f1e0@gmail.moc>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29804&group=comp.lang.c#29804

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: anton....@gmail.moc (Anton Shepelev)
Newsgroups: comp.lang.c
Subject: Re: portable way to get highest bit set?
Date: Fri, 13 Oct 2023 02:15:04 +0300
Organization: A noiseless patient Spider
Lines: 30
Message-ID: <20231013021504.12623444fc7d7fdaab87f1e0@gmail.moc>
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>
<20231012173021.0000149c@yahoo.com>
<86v8bbanjv.fsf@linuxsc.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Injection-Info: dont-email.me; posting-host="b7aacdabc5d68bf7abd376108d4d2dca";
logging-data="2909496"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/GxVFLsObq1oZlQWD4Pup/LGmv4qQ10uQ="
Cancel-Lock: sha1:mlHtgzkLhwHKyTeJi/yjI/1OYrw=
X-Newsreader: Sylpheed 3.7.0 (GTK+ 2.24.30; i686-pc-mingw32)
 by: Anton Shepelev - Thu, 12 Oct 2023 23:15 UTC

Tim Rentsch:

> Can you find a different solution that works in
> logarithmic time rather than linear time?

Direct linear search comes to mind. Instead of long long
int below, use the widest integer type that your C
environment supports:

#define LLBM CHAR_BIT * sizeof( long long ) - 1

unsigned long long int
high_bit_mask( unsigned long long n )
{ unsigned long long m = ULLONG_MAX;
int l = 0, r = LLBM+1;
while( 1 )
{ const int ofs = (l + r) / 2;
const unsigned long long mn = n & (m << LLBM - ofs);

if( mn > 0 ) r = ofs;
else if( mn == 0 ) l = ofs + 1;
if( l == r ) break;
}
/* How can I avoid this condition? */
return l == LLBM + 1 ? 0 : 1 << LLBM-l;
}

--
() ascii ribbon campaign -- against html e-mail
/\ www.asciiribbon.org -- against proprietary attachments

Re: portable way to get highest bit set?

<ug9uqs$2opp0$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29805&group=comp.lang.c#29805

  copy link   Newsgroups: comp.lang.c alt.comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: chris.m....@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c,alt.comp.lang.c
Subject: Re: portable way to get highest bit set?
Date: Thu, 12 Oct 2023 16:17:49 -0700
Organization: A noiseless patient Spider
Lines: 11
Message-ID: <ug9uqs$2opp0$1@dont-email.me>
References: <ug5gvh$1jkar$3@dont-email.me>
<pan$e0805$faa9a91a$a60476dc$4a90cfa8@invalid.invalid>
<87h6mwu9rk.fsf@nosuchdomain.example.com>
<pan$325f3$70a576a8$3150c04a$26db7b31@invalid.invalid>
<874jivttp9.fsf@nosuchdomain.example.com>
<20231013005031.c7f5a3e09343c73093007b84@gmail.moc>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 12 Oct 2023 23:17:48 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="2f6cae8585e78fbcab118262ba2efe09";
logging-data="2909984"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19iBc/Ycarmxq5pqHO641vsAQpv0ioK4FM="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:JfUckk7r3slUQVut8QxwAPuCnSg=
Content-Language: en-US
In-Reply-To: <20231013005031.c7f5a3e09343c73093007b84@gmail.moc>
 by: Chris M. Thomasson - Thu, 12 Oct 2023 23:17 UTC

On 10/12/2023 2:50 PM, Anton Shepelev wrote:
> Keith Thompson to Blue-Maned_Hawk:
>
>> OK, I'll take your word for it that Pan is at fault. But
>> I also blame you for continuing to use a signature that's
>> corrupted before it leaves your computer.
>
> I am with Keith.
>

I second that.

Re: portable way to get highest bit set?

<ug9vrc$2ov0t$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29806&group=comp.lang.c#29806

  copy link   Newsgroups: comp.lang.c alt.comp.lang.c
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!1.us.feeder.erje.net!3.eu.feeder.erje.net!feeder.erje.net!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: lew.pitc...@digitalfreehold.ca (Lew Pitcher)
Newsgroups: comp.lang.c,alt.comp.lang.c
Subject: Re: portable way to get highest bit set?
Date: Thu, 12 Oct 2023 23:35:08 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 31
Message-ID: <ug9vrc$2ov0t$1@dont-email.me>
References: <ug5gvh$1jkar$3@dont-email.me> <ug6cv8$1r3g4$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 12 Oct 2023 23:35:08 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="e04ae411ff81b78795b8050073c4aa25";
logging-data="2915357"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18VVVaOXyylQNK+5XfdUNCuuUUBfi25Aq4="
User-Agent: Pan/0.139 (Sexual Chocolate; GIT bf56508
git://git.gnome.org/pan2)
Cancel-Lock: sha1:Mma/nZwXilyz9wWCZSNXTfq62zo=
 by: Lew Pitcher - Thu, 12 Oct 2023 23:35 UTC

On Wed, 11 Oct 2023 14:54:32 +0000, Lew Pitcher wrote:

> On Wed, 11 Oct 2023 01:56:49 -0500, candycanearter07 wrote:
>
>> Hi,
>>
>> What is the best/most portable way to get the highest bit set?
>>
>> ie. 011010001 to 010000000
>
> What have you tried?
>
> I can think of one way, but it may not be the "best" or "most portable"
> way of setting the highest bit of a value, and it /does/ have some
> limitations. Show your work, and I might show mine.

#include <limits.h>
unsigned long long int msb_mask(unsigned long long int valu)
{ unsigned long long int mask;

for (mask = ((ULLONG_MAX) ^ (ULLONG_MAX >> 1));
mask && !(mask&valu);
mask >>= 1) continue;
return mask&valu;
}

--
Lew Pitcher
"In Skills We Trust"

Re: portable way to get highest bit set?

<uga0in$2ov0t$2@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29807&group=comp.lang.c#29807

  copy link   Newsgroups: comp.lang.c alt.comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: lew.pitc...@digitalfreehold.ca (Lew Pitcher)
Newsgroups: comp.lang.c,alt.comp.lang.c
Subject: Re: portable way to get highest bit set?
Date: Thu, 12 Oct 2023 23:47:35 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 46
Message-ID: <uga0in$2ov0t$2@dont-email.me>
References: <ug5gvh$1jkar$3@dont-email.me> <ug6cv8$1r3g4$1@dont-email.me>
<ug9vrc$2ov0t$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 12 Oct 2023 23:47:35 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="e04ae411ff81b78795b8050073c4aa25";
logging-data="2915357"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18Mwmxl724H50vhScFqc9tp7Eg1GKQiJpg="
User-Agent: Pan/0.139 (Sexual Chocolate; GIT bf56508
git://git.gnome.org/pan2)
Cancel-Lock: sha1:f3HlWLWBRK0ZFQIAAM88lWUbQp4=
 by: Lew Pitcher - Thu, 12 Oct 2023 23:47 UTC

On Thu, 12 Oct 2023 23:35:08 +0000, Lew Pitcher wrote:

> On Wed, 11 Oct 2023 14:54:32 +0000, Lew Pitcher wrote:
>
>> On Wed, 11 Oct 2023 01:56:49 -0500, candycanearter07 wrote:
>>
>>> Hi,
>>>
>>> What is the best/most portable way to get the highest bit set?
>>>
>>> ie. 011010001 to 010000000
>>
>> What have you tried?
>>
>> I can think of one way, but it may not be the "best" or "most portable"
>> way of setting the highest bit of a value, and it /does/ have some
>> limitations. Show your work, and I might show mine.
>
> #include <limits.h>
> unsigned long long int msb_mask(unsigned long long int valu)
> {
> unsigned long long int mask;
>
> for (mask = ((ULLONG_MAX) ^ (ULLONG_MAX >> 1));
> mask && !(mask&valu);
> mask >>= 1) continue;
> return mask&valu;
> }

Well, I'll be go to COBOL. A minor tweak just occurred to me.

unsigned long long int msb_mask(unsigned long long int valu)
{ unsigned long long int mask;

for (mask = ((ULLONG_MAX) ^ (ULLONG_MAX >> 1));
mask && !(mask&valu);
mask >>= 1) continue;
return mask; /* don't need to AND with valu now */
}

--
Lew Pitcher
"In Skills We Trust"

Re: portable way to get highest bit set?

<ugagv6$2vs64$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29813&group=comp.lang.c#29813

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!usenet.goja.nl.eu.org!weretis.net!feeder8.news.weretis.net!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: jameskuy...@alumni.caltech.edu (James Kuyper)
Newsgroups: comp.lang.c
Subject: Re: portable way to get highest bit set?
Date: Fri, 13 Oct 2023 00:27:18 -0400
Organization: A noiseless patient Spider
Lines: 10
Message-ID: <ugagv6$2vs64$1@dont-email.me>
References: <ug5gvh$1jkar$3@dont-email.me>
<pan$e0805$faa9a91a$a60476dc$4a90cfa8@invalid.invalid>
<87h6mwu9rk.fsf@nosuchdomain.example.com>
<pan$325f3$70a576a8$3150c04a$26db7b31@invalid.invalid>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 13 Oct 2023 04:27:18 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="a70e9bcd23d7197e11bc5ed8d1f54e7f";
logging-data="3141828"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19XCHDTERSeRnEG+BBDKspNdjXl4A7Y39s="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:UFl0htvl90TE8sRsGp1H0LQvpl0=
Content-Language: en-US
In-Reply-To: <pan$325f3$70a576a8$3150c04a$26db7b31@invalid.invalid>
 by: James Kuyper - Fri, 13 Oct 2023 04:27 UTC

On 10/12/23 09:42, Blue-Maned_Hawk wrote:
> Keith Thompson wrote:
>
>> A solution that depends on a header that's introduced in C23, a standard
>> that hasn't been published yet, is hardly portable.
>
> Well, it will be in 2024.

You expect the new standard to be widely adopted so much quicker than
any previous version? Why?

Re: portable way to get highest bit set?

<ugc8bs$3bvcj$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29824&group=comp.lang.c#29824

  copy link   Newsgroups: comp.lang.c alt.comp.lang.c news.software.readers
Followup: news.software.readers
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: val...@cultnix.org (vallor)
Newsgroups: comp.lang.c,alt.comp.lang.c,news.software.readers
Subject: Re: portable way to get highest bit set?
Followup-To: news.software.readers
Date: Fri, 13 Oct 2023 20:12:45 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 60
Message-ID: <ugc8bs$3bvcj$1@dont-email.me>
References: <ug5gvh$1jkar$3@dont-email.me>
<pan$e0805$faa9a91a$a60476dc$4a90cfa8@invalid.invalid>
<87h6mwu9rk.fsf@nosuchdomain.example.com>
<pan$325f3$70a576a8$3150c04a$26db7b31@invalid.invalid>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 13 Oct 2023 20:12:45 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="904bf301999d48bd8602b0c136aec429";
logging-data="3538323"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/UHXLfb7b35OH0z8zJSiuP"
Cancel-Lock: sha1:jgUPE7PYBM3V+bcWb49lvtHkbyE=
X-Face: \}2`P"_@pS86<'EM:'b.Ml}8IuMK"pV"?FReF$'c.S%u9<Q#U*4QO)$l81M`{Q/n
XL'`91kd%N::LG:=*\35JS0prp\VJN^<s"b#bff@fA7]5lJA.jn,x_d%Md$,{.EZ
 by: vallor - Fri, 13 Oct 2023 20:12 UTC

On Thu, 12 Oct 2023 13:42:03 -0000 (UTC), Blue-Maned_Hawk
<bluemanedhawk@invalid.invalid> wrote in
<pan$325f3$70a576a8$3150c04a$26db7b31@invalid.invalid>:

> Keith Thompson wrote:
>
>> A solution that depends on a header that's introduced in C23, a
>> standard that hasn't been published yet, is hardly portable.
>
> Well, it will be in 2024.
>
>> BTW, here's what your signature looks like in my newsreader (Gnus):
>>
>> Blue-Maned_HawkÃ\203\203\203\203Ã\203\203\202Ã\203\202Ã\202¢
> \224\202shortens
>> to HawkÃ\203\203\203\203Ã\203\203\202Ã\203\202Ã\202¢\224\202/
>>
>
blu.mÃ\203\203\203\203\211\233in.dÃ\203\203\203\203\212Ã\203\203\203\202Ã\203\203\202Ã\203\202Ã\202°ak/
>> Ã\203\203\203\203Ã\203\203\202Ã\203\202Ã\202¢\224\202he/him/his/
> himself/Mr.
>> bluemanedhawk.github.io Minor vehicular events!
>>
>> And in Thunderbird:
>>
>> Blue-Maned_HawkÃÃÃâshortens to HawkÃÃÃâ/ blu.mÃin.dÃÃÃÃðak/
>> ÃÃÃâhe/him/his/himself/Mr.
>> bluemanedhawk.github.io Minor vehicular events!
>
> Blame Pan for corrupting it. That was meant to have its fields
> separated by the │ character, including a pronunciation explanation
> field that has IPA characters in it.

(followup-to: news.software.readers)

I doubt Pan is to blame, but if it is,
let's get a patch in to the maintainers.

Some folks have offered to help with your sig.
I'm willing to help as time allows, but you have
to be willing to put in the time to stop posting
mojibakes. :/

If you're shy about email, please come
visit news.software.readers, where you
will find the thread "Usenet characters".

The article where I post the relevant
RFC quotes, cites, and maybe a little shade on
non-compliant newsreaders; can be
found in article:

<zDOJM.1001200$TPw2.839294@fx17.iad>

I've set the fu2 in this article
to news.software.readers.

--
-Scott
p.s. I use pan and track their git repo

Re: portable way to get highest bit set?

<pan$21acb$49c8a2a6$a1bbf038$1e40c8f0@invalid.invalid>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29829&group=comp.lang.c#29829

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!bluemanedhawk.eternal-september.org!.POSTED!not-for-mail
From: bluemane...@invalid.invalid (Blue-Maned_Hawk)
Newsgroups: comp.lang.c
Subject: Re: portable way to get highest bit set?
Date: Fri, 13 Oct 2023 22:02:55 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 25
Message-ID: <pan$21acb$49c8a2a6$a1bbf038$1e40c8f0@invalid.invalid>
References: <ug5gvh$1jkar$3@dont-email.me>
<pan$e0805$faa9a91a$a60476dc$4a90cfa8@invalid.invalid>
<87h6mwu9rk.fsf@nosuchdomain.example.com>
<pan$325f3$70a576a8$3150c04a$26db7b31@invalid.invalid>
<ugagv6$2vs64$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 13 Oct 2023 22:02:55 -0000 (UTC)
Injection-Info: bluemanedhawk.eternal-september.org; posting-host="046bb5a101b582feafc06d5d6b6b79b7";
logging-data="3604562"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/3tRR13QXt2/E0ItkKnV7jETxH3BMWBpQ="
User-Agent: Pan/0.154 (Izium; 517acf4)
Cancel-Lock: sha1:m3nmcQmQZnygEVOSHDnlLcs/7Pk=
X-Face: Llanfair­pwllgwyng
yll
­gogeryÂÃ
? ?­chwyrnÂÂÂÂÂÂ
­drobwll­llanÃ
‚Ã
ƒƒƒ‚­tysilioÂÂÂÃ
‚­gogo­goch
Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAIAAADYYG7QAAACh0lEQVRYw71Z21bD
MAzzevbfkr4cHjrSXJyL044+MDa6WLEl2SkvkrZ1AbAvXO+bUGSCPYnsuIVGMpm
ZLnjX718GhAKNsp8lON2F9VrhELwIgJlBepkZjA78rVK+FkmNhEJK76UsJlz8+E
rJsjrpYouhLo/SC6qPHgakFOR8wV9+8rCfO/I/oVnmUZUp42/LW2XkLj9TCFNM9
jp5g2EmHZgpYZjCOkYU7sXVogRylJqpdggoFLG1g09Flah/7kErCxzR9HgXPYsq
0glb9cxjIz2Vsk9AmAoCSxECpD713joMKjQqLAtmMqJmXjdVvlMnMQCVITotJd1
z+fh1f1NNo+vuc1KnhWUmY7t03vydTud9BbXCtN3L2PL3bK7JCNG0GHzuZxafyB
fxevCxpm1vrwZltqw6SILCcdoCE6PGQC8wZWDA9Or7Qp5s3lAZezys0nDazs9S9
R0TjwEiksRxLkNPC1NMMWPs1bj0Ei0Yuo+JVtFLuzP1NRJ16qXWN8DhhtmS4PDg
O6mqRxs4bEJrYt087mSIow/1VzW2oFlMQuiuIy/KsUagvhdw6hSjJGlIavbLF8x
j3X47bccLcUSi0dkWh1nUZNhANT1tHKUXrNxNLbd9KPb9wDDVrKwmPQMOPQ1oy6
k5I1DwzDeRJd3jVIhDAUxq3ngzJG4CCkNXZxZVMcjefoK2J0gUY2S3rxz/RuTFx
2zHd9U+obimJXMG4edsk/2j5pTU5G1MmzbRLxkfq5EiT1GGsidvMGzi+1goGb2l
GCrN+nGnV8xj3q3JLRDVPL96vUc7Z4aJ3TN1mVqWAMJMfG+Jxh6TQqP+92iZkCU
xtglds1AB6r0aiSHKcnFck+p/c/0CbacFLQcajGcAAAAASUVORK5CYII=
 by: Blue-Maned_Hawk - Fri, 13 Oct 2023 22:02 UTC

James Kuyper wrote:

> On 10/12/23 09:42, Blue-Maned_Hawk wrote:
>> Keith Thompson wrote:
>>
>>> A solution that depends on a header that's introduced in C23, a
>>> standard that hasn't been published yet, is hardly portable.
>>
>> Well, it will be in 2024.
>
> You expect the new standard to be widely adopted so much quicker than
> any previous version? Why?

What?

--
Blue-Maned_Hawk│shortens to
Hawk│/
blu.mɛin.dÊ°ak/
│he/him/his/himself/Mr.
bluemanedhawk.github.io
A typical acre of Florida land produces a revenue of three cents per year
in falling cocaine bales.

Re: portable way to get highest bit set?

<86r0ly9n77.fsf@linuxsc.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29831&group=comp.lang.c#29831

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: tr.17...@z991.linuxsc.com (Tim Rentsch)
Newsgroups: comp.lang.c
Subject: Re: portable way to get highest bit set?
Date: Fri, 13 Oct 2023 15:23:08 -0700
Organization: A noiseless patient Spider
Lines: 71
Message-ID: <86r0ly9n77.fsf@linuxsc.com>
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> <20231012183135.a6faa3027ccac0820b24ad96@g{oogle}mail.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: dont-email.me; posting-host="a03fa788dc159b4dec27c6d302b8dace";
logging-data="3620535"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19ewloUa7WCPxXoj8Nj67K3O4MxP4d0mbU="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:87K4vrFAVmTXYUOeF475ezwSsP4=
sha1:mTLd0McUjhbtOVJt+VcI2F1FVQE=
 by: Tim Rentsch - Fri, 13 Oct 2023 22:23 UTC

Anton Shepelev <anton.txt@g{oogle}mail.com> writes:

> Tim Rentsch:
>
>> The code should work for the type T being __uint128_t,
>> which is larger than uintmax_t.
>
> I was in my C89 mode:
>
> #include <stdio.h>
> #include <stddef.h>
> #include <limits.h>
>
> char is_le()
> { const int v = 1;
> const char* p = (unsigned char*)&v;
> return *p == 0;
> }
>
> unsigned char highest_bit_1( unsigned char byte )
> { int i ;
> unsigned char mask = 1 << (CHAR_BIT-1);
> for( i = 0; i <= CHAR_BIT; i += 1 )
> { if( byte & mask )
> { break; }
> mask >>= 1;
> }
> return mask;
> }
>
> /* sz must be the size of the value /and/ of the buffer: */
> void highest_bit_2( void* val, void* buffer, size_t sz )
> { int i,inc;
> unsigned char *pval = val;
> unsigned char *pbuf = buffer;
> unsigned char c ;
> char le = is_le();
> char set;
>
> if( le )
> { inc = 1; } else
> { inc = -1; pval += sz-1; pbuf += sz-1; }
>
> set = 0;
> for( i = 0; i < sz; i += 1 )
> { c = *pval ;
> if( !set && c != 0 )
> { *pbuf = highest_bit_1( c );
> set = 1;
> }
> else *pbuf = 0;
> pval += inc; pbuf += inc;
> }
> }
>
> void test( long int val )
> { char buf[sizeof(long int)];
>
> highest_bit_2(&val, &buf, sizeof( long int ));
> printf("Value: %9x, High-bit mask: %9x\n",
> val, *(long unsigned int *)&buf);
> }
>
>
> int main(void)
> { test(0x0); test(0x1); test(0x2); test(0x3); test(0x4); test(0x5);
>
> test((1<<17)-1); test((1<<17)); test((1<<17)+1);
> }

I'm at a loss for words.

Re: portable way to get highest bit set?

<20231014012644.e479ae5d69d5826e0e6ba474@gmail.moc>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29832&group=comp.lang.c#29832

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!rocksolid2!news.neodome.net!news.mixmin.net!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: anton....@gmail.moc (Anton Shepelev)
Newsgroups: comp.lang.c
Subject: Re: portable way to get highest bit set?
Date: Sat, 14 Oct 2023 01:26:44 +0300
Organization: A noiseless patient Spider
Lines: 9
Message-ID: <20231014012644.e479ae5d69d5826e0e6ba474@gmail.moc>
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>
<20231012183135.a6faa3027ccac0820b24ad96@g{oogle}mail.com>
<86r0ly9n77.fsf@linuxsc.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Injection-Info: dont-email.me; posting-host="19ffa8ae0585e499a41ecb7f9e25ce90";
logging-data="3622154"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18z8D9wy6wHF2kiyDMowNVaXHq6l3WnZqU="
Cancel-Lock: sha1:ZYcHLUurKPzMSlKFl7u36cH3fdA=
X-Newsreader: Sylpheed 3.7.0 (GTK+ 2.24.30; i686-pc-mingw32)
 by: Anton Shepelev - Fri, 13 Oct 2023 22:26 UTC

Tim Rentsch about my code:

> I'm at a loss for words.

What, no one did worse?

--
() ascii ribbon campaign -- against html e-mail
/\ www.asciiribbon.org -- against proprietary attachments

Re: portable way to get highest bit set?

<86mswm9mwk.fsf@linuxsc.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29834&group=comp.lang.c#29834

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.nntp4.net!news.gegeweb.eu!gegeweb.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: tr.17...@z991.linuxsc.com (Tim Rentsch)
Newsgroups: comp.lang.c
Subject: Re: portable way to get highest bit set?
Date: Fri, 13 Oct 2023 15:29:31 -0700
Organization: A noiseless patient Spider
Lines: 37
Message-ID: <86mswm9mwk.fsf@linuxsc.com>
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> <20231012173021.0000149c@yahoo.com> <86v8bbanjv.fsf@linuxsc.com> <20231013021504.12623444fc7d7fdaab87f1e0@gmail.moc>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: dont-email.me; posting-host="a03fa788dc159b4dec27c6d302b8dace";
logging-data="3620535"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+8p4r0913XCJPo99VRkDVlN5DVB34nuxA="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:UAb6jrwxV3VtNC+14mh1GtXczZE=
sha1:9Mk8sikrMpe+LiUaIeFvNy/GsP4=
 by: Tim Rentsch - Fri, 13 Oct 2023 22:29 UTC

Anton Shepelev <anton.txt@gmail.moc> writes:

> Tim Rentsch:
>
>> Can you find a different solution that works in
>> logarithmic time rather than linear time?
>
> Direct linear search comes to mind. Instead of long long
> int below, use the widest integer type that your C
> environment supports:
>
> #define LLBM CHAR_BIT * sizeof( long long ) - 1
>
> unsigned long long int
> high_bit_mask( unsigned long long n )
> { unsigned long long m = ULLONG_MAX;
> int l = 0, r = LLBM+1;
> while( 1 )
> { const int ofs = (l + r) / 2;
> const unsigned long long mn = n & (m << LLBM - ofs);
>
> if( mn > 0 ) r = ofs;
> else if( mn == 0 ) l = ofs + 1;
> if( l == r ) break;
> }
> /* How can I avoid this condition? */
> return l == LLBM + 1 ? 0 : 1 << LLBM-l;
> }

Before giving any comment on the algorithm, I'd like to ask a
question about layout style. Your code follows the unusual
practice of starting controlled blocks with an open brace at the
start (possibly indented) of a line, and the first code line of
the block on the same line as the open brace. Is this practice
something you started by yourself, or did you see it and adopt
it from somewhere else? What kinds of reasons persuaded you
to follow it?

Re: portable way to get highest bit set?

<878r86rw6y.fsf@nosuchdomain.example.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29835&group=comp.lang.c#29835

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: portable way to get highest bit set?
Date: Fri, 13 Oct 2023 15:31:33 -0700
Organization: None to speak of
Lines: 22
Message-ID: <878r86rw6y.fsf@nosuchdomain.example.com>
References: <ug5gvh$1jkar$3@dont-email.me>
<pan$e0805$faa9a91a$a60476dc$4a90cfa8@invalid.invalid>
<87h6mwu9rk.fsf@nosuchdomain.example.com>
<pan$325f3$70a576a8$3150c04a$26db7b31@invalid.invalid>
<ugagv6$2vs64$1@dont-email.me>
<pan$21acb$49c8a2a6$a1bbf038$1e40c8f0@invalid.invalid>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="8c7495bacb7913ab0d9301cc10583541";
logging-data="3625743"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18FxqshtkilLDYGO1TDZCFf"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:F55HaHO557iTwL4geYcvX+N1YTE=
sha1:uYzJUZ+qpcge9uR85n/hPqGuXX0=
 by: Keith Thompson - Fri, 13 Oct 2023 22:31 UTC

Blue-Maned_Hawk <bluemanedhawk@invalid.invalid> writes:
> James Kuyper wrote:
>> On 10/12/23 09:42, Blue-Maned_Hawk wrote:
>>> Keith Thompson wrote:
>>>> A solution that depends on a header that's introduced in C23, a
>>>> standard that hasn't been published yet, is hardly portable.
>>>
>>> Well, it will be in 2024.
>>
>> You expect the new standard to be widely adopted so much quicker than
>> any previous version? Why?
>
> What?

I don't know what was unclear. You seem to expect universal adoption of
the C23 standard by 2024. Given the adoption speed of previous editions
of the C standard, that's completely unrealistic.

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Will write code for food.
void Void(void) { Void(); } /* The recursive call of the void */

Re: portable way to get highest bit set?

<20231014015035.a51cbb621de8eea5ac6a8651@gmail.moc>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29839&group=comp.lang.c#29839

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: anton....@gmail.moc (Anton Shepelev)
Newsgroups: comp.lang.c
Subject: Re: portable way to get highest bit set?
Date: Sat, 14 Oct 2023 01:50:35 +0300
Organization: A noiseless patient Spider
Lines: 39
Message-ID: <20231014015035.a51cbb621de8eea5ac6a8651@gmail.moc>
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>
<20231012173021.0000149c@yahoo.com>
<86v8bbanjv.fsf@linuxsc.com>
<20231013021504.12623444fc7d7fdaab87f1e0@gmail.moc>
<86mswm9mwk.fsf@linuxsc.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Injection-Info: dont-email.me; posting-host="19ffa8ae0585e499a41ecb7f9e25ce90";
logging-data="3634103"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/uuXeZRqhBWXK0DqrQU+Ml70kPX68hc8o="
Cancel-Lock: sha1:eBbz6iNYEJrnXIys6VuwF+qW7I0=
X-Newsreader: Sylpheed 3.7.0 (GTK+ 2.24.30; i686-pc-mingw32)
 by: Anton Shepelev - Fri, 13 Oct 2023 22:50 UTC

Tim Rentsch:

> Before giving any comment on the algorithm, I'd like to
> ask a question about layout style. Your code follows the
> unusual practice of starting controlled blocks with an
> open brace at the start (possibly indented) of a line, and
> the first code line of the block on the same line as the
> open brace. Is this practice something you started by
> yourself, or did you see it and adopt it from somewhere
> else? What kinds of reasons persuaded you to follow it?

I was bothered by the unaligned braces in K&R and by the
too-sparse look of the opening brace on a line of its own.
When a friend of mine was working on a coding style
guide[1], he researched exising indentaion styles and told
me about them, including Horstmann:

<https://en.wikipedia.org/wiki/Indentation_style#Horstmann_style>

whence I took the main idea of placing braces. My has
decided upon a different indetation, but I forget which and
I have not his guide at hand, but his Pascal indentation can
be seen in his showcase of programming fonts:

<http://inversed.ru/Articles/Fonts_Comparison.png>

____________________
1. of which lack of bias and prejudice is an imoprtant
advantage, because before that my friend use no style
all, that is writing everything flat!

P.S.: reflecting on my solution later I decided that a
direct comparison of the analysed value with a power
of two might be better, especially because it is
already in the form required for the return value.

--
() ascii ribbon campaign -- against html e-mail
/\ www.asciiribbon.org -- against proprietary attachments


devel / comp.lang.c / Re: portable way to get highest bit set?

Pages:12345678
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor