Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

When we write programs that "learn", it turns out we do and they don't.


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?

<8734yg4ty3.fsf@bsb.me.uk>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ben.use...@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.lang.c
Subject: Re: portable way to get highest bit set?
Date: Thu, 12 Oct 2023 00:31:48 +0100
Organization: A noiseless patient Spider
Lines: 33
Message-ID: <8734yg4ty3.fsf@bsb.me.uk>
References: <ug5gvh$1jkar$3@dont-email.me>
<20231011102714.44a870af4dfe68f756974953@g{oogle}mail.com>
<ug6huc$1rvp1$1@dont-email.me> <86h6mxawqq.fsf@linuxsc.com>
<ug71l8$21irh$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="625706da703fbb31625f3f3bd250e95a";
logging-data="2212586"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18yg2SEzdbot0wYKI0KSGWVcSUV/ri/3Gw="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux)
Cancel-Lock: sha1:+RGYk0/zYScMGx/xeTNJiI0iR6Q=
sha1:Sct4eq929M7fV6btt1tLAuCCSzU=
X-BSB-Auth: 1.406628c98ff04d265fdf.20231012003148BST.8734yg4ty3.fsf@bsb.me.uk
 by: Ben Bacarisse - Wed, 11 Oct 2023 23:31 UTC

David Brown <david.brown@hesbynett.no> writes:

> On 11/10/2023 19:34, Tim Rentsch wrote:
....
>> * Very few cases with undefined behavior
>
> Why would you accept any cases?
>
>> * Forms an excellent basis for several interview questions
>
> Anyone suggesting such a solution for an interview in my world would get an
> extremely poor scoring. But they might get points for showing why it is
> such a bad idea.

Exactly. That's what Tim said -- good for several interview questions.

I think you missed the whole tone of the post. There were lots of
signals, but surely "Very few cases with undefined behavior" should have
sealed it.

>> (I'm resisting the temptation to make an off-topic remark
>> regarding the number of properties.)
>> P.S. This response is meant to be more entertaining than
>> useful. I do hope it helps broaden your thinking in
>> searching for an answer.
>
> It's fine to give some "off the beaten track" solutions, but don't lie
> about them.

What is the lie? I don't see one.

--
Ben.

Re: portable way to get highest bit set?

<ug7een$1rvp2$3@dont-email.me>

  copy mid

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

  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: no...@thanks.net (candycanearter07)
Newsgroups: comp.lang.c,alt.comp.lang.c
Subject: Re: portable way to get highest bit set?
Date: Wed, 11 Oct 2023 19:25:59 -0500
Organization: A noiseless patient Spider
Lines: 7
Message-ID: <ug7een$1rvp2$3@dont-email.me>
References: <ug5gvh$1jkar$3@dont-email.me>
<20231011102714.44a870af4dfe68f756974953@g{oogle}mail.com>
<ug6huc$1rvp1$1@dont-email.me> <ug6nvv$1va0l$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 00:26:00 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="90e680e9319fe9aa362feba4c57ff022";
logging-data="1965858"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+5KGXby1lgHK7SHJ0zKj5wf+IdqQ5Bx7N7DzioA5d2Tw=="
User-Agent: Betterbird (Linux)
Cancel-Lock: sha1:Lse5XyWNeWgQluQPocyZZG8t81E=
Content-Language: en-US
In-Reply-To: <ug6nvv$1va0l$1@dont-email.me>
 by: candycanearter07 - Thu, 12 Oct 2023 00:25 UTC

On 10/11/23 13:02, Richard Harnden wrote:
> Why 0x100000000 and not 0x800000000?

Wait, how do you specify a binary constant again?
--
user <candycane> is generated from /dev/urandom

Re: portable way to get highest bit set?

<ug7efd$1rvp2$4@dont-email.me>

  copy mid

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

  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: no...@thanks.net (candycanearter07)
Newsgroups: comp.lang.c,alt.comp.lang.c
Subject: Re: portable way to get highest bit set?
Date: Wed, 11 Oct 2023 19:26:21 -0500
Organization: A noiseless patient Spider
Lines: 34
Message-ID: <ug7efd$1rvp2$4@dont-email.me>
References: <ug5gvh$1jkar$3@dont-email.me>
<20231011102714.44a870af4dfe68f756974953@g{oogle}mail.com>
<ug6huc$1rvp1$1@dont-email.me>
<b2e341ea-a49d-c0cf-d8ee-96235db44038@gmail.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 12 Oct 2023 00:26:22 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="90e680e9319fe9aa362feba4c57ff022";
logging-data="1965858"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19qGcw6TZnu1RP0NvKBkkH7wylQSTUYb86tW7bzwwZWJw=="
User-Agent: Betterbird (Linux)
Cancel-Lock: sha1:5vyC1OHr3u6Ib0eax2ajwb0NzQc=
Content-Language: en-US
In-Reply-To: <b2e341ea-a49d-c0cf-d8ee-96235db44038@gmail.com>
 by: candycanearter07 - Thu, 12 Oct 2023 00:26 UTC

On 10/11/23 12:03, Ian C wrote:
> On 11/10/2023 17:19, candycanearter07 wrote:
>> On 10/11/23 02:27, Anton Shepelev wrote:
>>> candycanearter07:
>>>
>>>> What is the best/most portable way to get the highest bit
>>>> set?
>>>>
>>>> ie. 011010001
>>>> to  010000000
>>>
>>> What is your best attempt, even if unsuccessful?
>>>
>>
>> int out;
>> for(out = 0x100000000; out; out >> 1)
>>      if(out & input) break;
>>
>> I'm trying to find something size independent, though.
>> Or at least able to be swapped out with a #define
>
> If you want the highest bit, 0x800000000 may be a better start :-)
>
> If it was me I'd do it in reverse.  Assuming it's unsigned, shift your
> input to the right until it hits zero, counting the number of shifts
> which will be your bit number.  Assuming my head is currently working.
>
>
> Ian C

Actually yeah, I think that could work.
--
user <candycane> is generated from /dev/urandom

Re: portable way to get highest bit set?

<ug7evl$243f3$1@dont-email.me>

  copy mid

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

  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: bc...@freeuk.com (Bart)
Newsgroups: comp.lang.c,alt.comp.lang.c
Subject: Re: portable way to get highest bit set?
Date: Thu, 12 Oct 2023 01:35:03 +0100
Organization: A noiseless patient Spider
Lines: 10
Message-ID: <ug7evl$243f3$1@dont-email.me>
References: <ug5gvh$1jkar$3@dont-email.me>
<20231011102714.44a870af4dfe68f756974953@g{oogle}mail.com>
<ug6huc$1rvp1$1@dont-email.me> <ug6nvv$1va0l$1@dont-email.me>
<ug7een$1rvp2$3@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 00:35:01 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="9fe28f5d9611cb8b79db509fb88e5f35";
logging-data="2231779"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+6sw3dADu4K2DsHU3e9dKbyxaBuhhVkOg="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.1
Cancel-Lock: sha1:LlNIeu/1t3OFfgLTg+6NoFrk6CI=
In-Reply-To: <ug7een$1rvp2$3@dont-email.me>
 by: Bart - Thu, 12 Oct 2023 00:35 UTC

On 12/10/2023 01:25, candycanearter07 wrote:
> On 10/11/23 13:02, Richard Harnden wrote:
>> Why 0x100000000 and not 0x800000000?
>
> Wait, how do you specify a binary constant again?

Use 0b rather than 0x. But without numeric separators, you will need to
count carefully if you have up to 64 bits.

Easier might be to write 1U<<31 or 1ULL<<63.

Re: portable way to get highest bit set?

<87cyxku0ji.fsf@nosuchdomain.example.com>

  copy mid

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

  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: Wed, 11 Oct 2023 17:50:09 -0700
Organization: None to speak of
Lines: 28
Message-ID: <87cyxku0ji.fsf@nosuchdomain.example.com>
References: <ug5gvh$1jkar$3@dont-email.me>
<20231011102714.44a870af4dfe68f756974953@g{oogle}mail.com>
<ug6huc$1rvp1$1@dont-email.me> <ug6nvv$1va0l$1@dont-email.me>
<ug7een$1rvp2$3@dont-email.me> <ug7evl$243f3$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="0d09a1fd828b856f1a82ca58c0c8ac6b";
logging-data="2236266"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18tO9oRqKMLId8fpahIOvMF"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:qFzuc4eNehKP3oKKcIQXf0isTcE=
sha1:TTFB9FQqYuR2UsiTUUQC/RHIJgA=
 by: Keith Thompson - Thu, 12 Oct 2023 00:50 UTC

Bart <bc@freeuk.com> writes:
> On 12/10/2023 01:25, candycanearter07 wrote:
>> On 10/11/23 13:02, Richard Harnden wrote:
>>> Why 0x100000000 and not 0x800000000?

I think there's an extra 0 on both of those constants.

>> Wait, how do you specify a binary constant again?
>
> Use 0b rather than 0x. But without numeric separators, you will need
> to count carefully if you have up to 64 bits.
>
> Easier might be to write 1U<<31 or 1ULL<<63.

C does not currently support binary constants. C23 adds them, and gcc
and clang both support them as an extension (some other compilers might
also).

In C23, a binary integer constant starts with "0b" or "0B".

C23 will also allow apostrophe as a digit separator (a feature borrowed
from C++), so you can write 0b1100'1001 -- but don't count on any
pre-C23 compilers supporting that as an extension.

--
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?

<ug7gl3$1rvp2$7@dont-email.me>

  copy mid

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

  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: no...@thanks.net (candycanearter07)
Newsgroups: comp.lang.c,alt.comp.lang.c
Subject: Re: portable way to get highest bit set?
Date: Wed, 11 Oct 2023 20:03:31 -0500
Organization: A noiseless patient Spider
Lines: 10
Message-ID: <ug7gl3$1rvp2$7@dont-email.me>
References: <ug5gvh$1jkar$3@dont-email.me>
<20231011102714.44a870af4dfe68f756974953@g{oogle}mail.com>
<ug6huc$1rvp1$1@dont-email.me> <ug6nvv$1va0l$1@dont-email.me>
<ug7een$1rvp2$3@dont-email.me> <ug7evl$243f3$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 01:03:31 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="90e680e9319fe9aa362feba4c57ff022";
logging-data="1965858"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18mtJh6AjDzRWRghf+zcOOehne85MTkdy8Y8y5f88YDqw=="
User-Agent: Betterbird (Linux)
Cancel-Lock: sha1:/FFYlKsZ2DlWKfJrRpyb38wKFpw=
Content-Language: en-US
In-Reply-To: <ug7evl$243f3$1@dont-email.me>
 by: candycanearter07 - Thu, 12 Oct 2023 01:03 UTC

On 10/11/23 19:35, Bart wrote:
> Use 0b rather than 0x. But without numeric separators, you will need to
> count carefully if you have up to 64 bits.
>
> Easier might be to write 1U<<31 or 1ULL<<63.

Okey thanks.
--
user <candycane> is generated from /dev/urandom

Re: portable way to get highest bit set?

<ug86d3$2c9mp$1@dont-email.me>

  copy mid

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

  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 09:14:43 +0200
Organization: A noiseless patient Spider
Lines: 37
Message-ID: <ug86d3$2c9mp$1@dont-email.me>
References: <ug5gvh$1jkar$3@dont-email.me> <ug70cu$219ov$1@dont-email.me>
<878r884vi5.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 07:14:44 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="f86e1bc216bf95e550ccf04f50e7d0ae";
logging-data="2500313"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+nWmFTSuq0beovIQmxlk/O"
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:Dm6HU7FyJ3d37CM9oaQ06/rwcMs=
In-Reply-To: <878r884vi5.fsf@bsb.me.uk>
 by: jak - Thu, 12 Oct 2023 07:14 UTC

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:

log(18446744073709541615) / log(2) =
63.99999999999999921849

>
>> ret = (unsigned long)pow(2., (double)(unsigned long)(log((double)val) /
>> log(2.)));
>>
>> printf("%lu -> %lu", val, ret);
>>
>> return 0;
>> }
>

Re: portable way to get highest bit set?

<20231012111100.272c96b3209baad26a150e55@g{oogle}mail.com>

  copy mid

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

  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 11:11:00 +0300
Organization: A noiseless patient Spider
Lines: 36
Message-ID: <20231012111100.272c96b3209baad26a150e55@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>
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="2510911"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/QWQin2Ssjlvzof28HbJ037TtexPlWbgk="
Cancel-Lock: sha1:8cD+Q/sOkNVhrVp86Mr38eyiXb8=
X-Newsreader: Sylpheed 3.7.0 (GTK+ 2.24.30; i686-pc-mingw32)
 by: Anton Shepelev - Thu, 12 Oct 2023 08:11 UTC

Tim Rentsch:

> I've coded up a dozen[*] different solutions.
> Here is my current avorite[**]:
>
> typedef union {
> long double d;
> struct { unsigned long long s; unsigned e:15;} b;
> } X;
>
> unsigned long long
> high_bit_set( unsigned long long u ){
> return 1ULL << (X){ .d = u }.b.e - 16383 & u;
> }

And mine is quite anticlimactic. It accepts a byte, because
I wrote it yesterday, before learning that the OP needs a
more generic solution:

unsigned char highest_bit( unsigned char byte )
{ int i ;
unsigned char mask = 1 << 7;
for( i = 0; i <= 8; i += 1 )
{ if( byte & mask )
{ break; }
mask >>= 1;
}
return mask;
}

It is the most straight-forward method that I could think
of, and extensible to larger sizes of the variable.

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

Re: portable way to get highest bit set?

<4e3940f1-8c6e-4786-a9f6-f9f17e172b8fn@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:6214:3113:b0:65a:fd9b:e057 with SMTP id ks19-20020a056214311300b0065afd9be057mr373683qvb.0.1697102431298;
Thu, 12 Oct 2023 02:20:31 -0700 (PDT)
X-Received: by 2002:a05:6870:9897:b0:1dc:dbb0:60aa with SMTP id
eg23-20020a056870989700b001dcdbb060aamr8869044oab.6.1697102431061; Thu, 12
Oct 2023 02:20:31 -0700 (PDT)
Path: i2pn2.org!rocksolid2!news.neodome.net!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Thu, 12 Oct 2023 02:20:30 -0700 (PDT)
In-Reply-To: <ug71l8$21irh$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=199.203.251.52; posting-account=ow8VOgoAAAAfiGNvoH__Y4ADRwQF1hZW
NNTP-Posting-Host: 199.203.251.52
References: <ug5gvh$1jkar$3@dont-email.me> <20231011102714.44a870af4dfe68f756974953@g{oogle}mail.com>
<ug6huc$1rvp1$1@dont-email.me> <86h6mxawqq.fsf@linuxsc.com> <ug71l8$21irh$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <4e3940f1-8c6e-4786-a9f6-f9f17e172b8fn@googlegroups.com>
Subject: Re: portable way to get highest bit set?
From: already5...@yahoo.com (Michael S)
Injection-Date: Thu, 12 Oct 2023 09:20:31 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 5317
 by: Michael S - Thu, 12 Oct 2023 09:20 UTC

On Wednesday, October 11, 2023 at 11:47:56 PM UTC+3, David Brown wrote:
> On 11/10/2023 19:34, Tim Rentsch wrote:
> > candycanearter07 <n...@thanks.net> writes:
> >
> >> On 10/11/23 02:27, Anton Shepelev wrote:
> >>
> >>> candycanearter07:
> >>>
> >>>> What is the best/most portable way to get the highest bit
> >>>> set?
> >>>>
> >>>> ie. 011010001
> >>>> to 010000000
> >>>
> >>> What is your best attempt, even if unsuccessful?
> >>
> >> int out;
> >> for(out = 0x100000000; out; out >> 1)
> >> if(out & input) break;
> >>
> >> I'm trying to find something size independent, though.
> >> Or at least able to be swapped out with a #define
> >
> > I've coded up a dozen[*] different solutions. Here is my current
> > favorite[**]:
> >
> > typedef union {
> > long double d;
> > struct { unsigned long long s; unsigned e:15;} b;
> > } X;
> >
> > unsigned long long
> > high_bit_set( unsigned long long u ){
> > return 1ULL << (X){ .d = u }.b.e - 16383 & u;
> > }
> >
> >
> > This answer has these nice properties:
> >
> > * Single line function body
> Irrelevant.
> > * No looping or testing
> That could be useful for efficiency. But a solution that can boil down
> to some kind of "count leading zeros" or "find first one" instruction
> will be much more efficient. (And the shift will be a loop on smaller
> processors.) And if you had bothered to look at the generated code,
> even on efficient compilers for the one target it supports, you'd see it
> generates tests and either a branch or a conditional move.
> > * Large domain of inputs supported
> True - it supports up to 64 bits.
>
> > * Widely portable
>
> Completely incorrect - unless you think that x86 extended precision
> floating point format is "widely portable". It's not even portable in
> the x86 world. Basically, it will fail for any compiler for which "long
> double" is the same format as "double", which includes MSVC, and gcc for
> many non-x86 targets.

It will also fail for any compiler for which "long double" is *not* the same
format as "double", but also not the same as Intel's 80-bit. That includes all
aarch64 Linux compiler where 'long double' is equivalent of IEEE
binary128 and is implemented in software.

> > * Very few cases with undefined behavior
> Why would you accept any cases?
> > * Forms an excellent basis for several interview questions
> Anyone suggesting such a solution for an interview in my world would get
> an extremely poor scoring. But they might get points for showing why it
> is such a bad idea.
> > * Sure to launch a torrent of followups on its pros and cons
> /That/ one I agree on, though of course you will ignore my reply.
>
> It also has not-nice properties including:
>
> * Totally dependent on floating point standards found only on some
> compilers.
>
> * Slow and inefficient on x86, because it requires non-SIMD floating point.
>

That's not true. On prevalent Intel and AMD processors, for scalar
calculations like above, x87 FPU is about as fast SSE/AVX FPU.
Speed wise, on x64 Linux or BSD Tim's solution is quite o.k, second
only to non-portable intrinsic and to non-existing C23 variants.

> * Non-obvious code with a magic number, using features well outside the
> problem domain.
>
> * Unnecessarily outside the C++ compatible subset of C (this is
> important to some people).
> >
> > (I'm resisting the temptation to make an off-topic remark
> > regarding the number of properties.)
> >
> >
> > P.S. This response is meant to be more entertaining than
> > useful. I do hope it helps broaden your thinking in
> > searching for an answer.
> >
> It's fine to give some "off the beaten track" solutions, but don't lie
> about them.

Re: portable way to get highest bit set?

<ug8dv1$2dsi2$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: portable way to get highest bit set?
Date: Thu, 12 Oct 2023 11:23:45 +0200
Organization: A noiseless patient Spider
Lines: 50
Message-ID: <ug8dv1$2dsi2$1@dont-email.me>
References: <ug5gvh$1jkar$3@dont-email.me>
<20231011102714.44a870af4dfe68f756974953@g{oogle}mail.com>
<ug6huc$1rvp1$1@dont-email.me> <86h6mxawqq.fsf@linuxsc.com>
<ug71l8$21irh$1@dont-email.me> <8734yg4ty3.fsf@bsb.me.uk>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 12 Oct 2023 09:23:45 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="31bfe17730ce6012eded28f48a4621e4";
logging-data="2552386"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/nalEbOee4Cj0o6iHp35Dt0QDnm0YYYuE="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:iCOfEj3Kt6MMS7pVMbsfsCp+dHk=
In-Reply-To: <8734yg4ty3.fsf@bsb.me.uk>
Content-Language: en-GB
 by: David Brown - Thu, 12 Oct 2023 09:23 UTC

On 12/10/2023 01:31, Ben Bacarisse wrote:
> David Brown <david.brown@hesbynett.no> writes:
>
>> On 11/10/2023 19:34, Tim Rentsch wrote:
> ...
>>> * Very few cases with undefined behavior
>>
>> Why would you accept any cases?
>>
>>> * Forms an excellent basis for several interview questions
>>
>> Anyone suggesting such a solution for an interview in my world would get an
>> extremely poor scoring. But they might get points for showing why it is
>> such a bad idea.
>
> Exactly. That's what Tim said -- good for several interview questions.
>
> I think you missed the whole tone of the post. There were lots of
> signals, but surely "Very few cases with undefined behavior" should have
> sealed it.
>
>>> (I'm resisting the temptation to make an off-topic remark
>>> regarding the number of properties.)
>>> P.S. This response is meant to be more entertaining than
>>> useful. I do hope it helps broaden your thinking in
>>> searching for an answer.
>>
>> It's fine to give some "off the beaten track" solutions, but don't lie
>> about them.
>
> What is the lie? I don't see one.
>

"Widely portable". In general, I thought his post might mislead people
into thinking it was a serious line of attack for the problem - creative
uses of floating point operations have been seen before for this kind of
thing. (I started writing a longer explanation, but scraped it to avoid
rambling off topic, so we can get back to the C.)

One thing that I did find interesting, however, was when I tried it on
godbolt and made a couple of test functions like :

unsigned test1(void) { return high_bit_set(7654(; }

gcc was happy to pre-calculate the correct result at compile time. I
know gcc (and clang) can simulate floating point hardware accurately at
compile time, but it surprised me that these compilers managed that.

Re: portable way to get highest bit set?

<1d76464c-1f6d-423d-ab97-ea24af85558cn@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:620a:4cf:b0:770:58ab:afb4 with SMTP id 15-20020a05620a04cf00b0077058abafb4mr316849qks.8.1697102856713;
Thu, 12 Oct 2023 02:27:36 -0700 (PDT)
X-Received: by 2002:a05:620a:208b:b0:76f:f0e:e650 with SMTP id
e11-20020a05620a208b00b0076f0f0ee650mr306894qka.9.1697102856508; Thu, 12 Oct
2023 02:27:36 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Thu, 12 Oct 2023 02:27:36 -0700 (PDT)
In-Reply-To: <20231012111100.272c96b3209baad26a150e55@g{oogle}mail.com>
Injection-Info: google-groups.googlegroups.com; posting-host=199.203.251.52; posting-account=ow8VOgoAAAAfiGNvoH__Y4ADRwQF1hZW
NNTP-Posting-Host: 199.203.251.52
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>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <1d76464c-1f6d-423d-ab97-ea24af85558cn@googlegroups.com>
Subject: Re: portable way to get highest bit set?
From: already5...@yahoo.com (Michael S)
Injection-Date: Thu, 12 Oct 2023 09:27:36 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 2667
 by: Michael S - Thu, 12 Oct 2023 09:27 UTC

On Thursday, October 12, 2023 at 11:11:16 AM UTC+3, Anton Shepelev wrote:
> Tim Rentsch:
> > I've coded up a dozen[*] different solutions.
> > Here is my current avorite[**]:
> >
> > typedef union {
> > long double d;
> > struct { unsigned long long s; unsigned e:15;} b;
> > } X;
> >
> > unsigned long long
> > high_bit_set( unsigned long long u ){
> > return 1ULL << (X){ .d = u }.b.e - 16383 & u;
> > }
> And mine is quite anticlimactic. It accepts a byte, because
> I wrote it yesterday, before learning that the OP needs a
> more generic solution:
>
> unsigned char highest_bit( unsigned char byte )
> { int i ;
> unsigned char mask = 1 << 7;
> for( i = 0; i <= 8; i += 1 )
> { if( byte & mask )
> { break; }
> mask >>= 1;
> }
> return mask;
> }
>
> It is the most straight-forward method that I could think
> of, and extensible to larger sizes of the variable.
> --

There exist more straight-forward method that you could think of
if you try to think a little longer. It is suggested by IanC near beginning
of this thread starting from "If it was me I'd do it in reverse".

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

Re: portable way to get highest bit set?

<86cyxkb2ka.fsf@linuxsc.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.hispagatos.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 02:41:25 -0700
Organization: A noiseless patient Spider
Lines: 53
Message-ID: <86cyxkb2ka.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>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: dont-email.me; posting-host="8a29b169449756014084ae3fe2a46ca6";
logging-data="2560063"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX185rh8UDkgyKWMicT8iBexYINHejVm3ec4="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:M6SJag/U2cPSbeqNwsRwWnbxDSs=
sha1:2S3LtOacVe7+wngz2jtb8/CudsU=
 by: Tim Rentsch - Thu, 12 Oct 2023 09:41 UTC

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

> Tim Rentsch:
>
>> I've coded up a dozen[*] different solutions.
>> Here is my current avorite[**]:
>>
>> typedef union {
>> long double d;
>> struct { unsigned long long s; unsigned e:15;} b;
>> } X;
>>
>> unsigned long long
>> high_bit_set( unsigned long long u ){
>> return 1ULL << (X){ .d = u }.b.e - 16383 & u;
>> }
>
> And mine is quite anticlimactic. It accepts a byte, because
> I wrote it yesterday, before learning that the OP needs a
> more generic solution:
>
> unsigned char highest_bit( unsigned char byte )
> { int i ;
> unsigned char mask = 1 << 7;
> for( i = 0; i <= 8; i += 1 )
> { if( byte & mask )
> { break; }
> mask >>= 1;
> }
> return mask;
> }
>
> It is the most straight-forward method that I could think
> of, and extensible to larger sizes of the variable.

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.
So something like the following, with type T being an
unknown unsigned integer type:

T
highest_bit_set( T u ){
T r;
/* set r to the highest bit set in u */
return r;
}

I have written code for this form of the problem, but
won't be posting it before the weekend. Try it!

P.S. My earlier "solution" was just for fun, not a serious
attempt to solve the problem.

Re: portable way to get highest bit set?

<868r88b2gx.fsf@linuxsc.com>

  copy mid

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

  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 02:43:26 -0700
Organization: A noiseless patient Spider
Lines: 36
Message-ID: <868r88b2gx.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> <ug71l8$21irh$1@dont-email.me> <8734yg4ty3.fsf@bsb.me.uk>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: dont-email.me; posting-host="8a29b169449756014084ae3fe2a46ca6";
logging-data="2560063"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+aEY5mHGM9wqCnT5CqSdO/EqdBNPBS23E="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:QeWTYIhcF1Sff0VrhBOo5pPwOxY=
sha1:W9lj+trtonVu8VUtEbAFGQeHKCI=
 by: Tim Rentsch - Thu, 12 Oct 2023 09:43 UTC

Ben Bacarisse <ben.usenet@bsb.me.uk> writes:

> David Brown <david.brown@hesbynett.no> writes:
>
>> On 11/10/2023 19:34, Tim Rentsch wrote:
>
> ...
>
>>> * Very few cases with undefined behavior
>>
>> Why would you accept any cases?
>>
>>> * Forms an excellent basis for several interview questions
>>
>> Anyone suggesting such a solution for an interview in my world would
>> get an extremely poor scoring. But they might get points for showing
>> why it is such a bad idea.
>
> Exactly. That's what Tim said -- good for several interview questions.
>
> I think you missed the whole tone of the post. There were lots of
> signals, but surely "Very few cases with undefined behavior" should
> have sealed it.
>
>>> (I'm resisting the temptation to make an off-topic remark
>>> regarding the number of properties.)
>>> P.S. This response is meant to be more entertaining than
>>> useful. I do hope it helps broaden your thinking in
>>> searching for an answer.
>>
>> It's fine to give some "off the beaten track" solutions, but don't lie
>> about them.
>
> What is the lie? I don't see one.

Thank you Ben. Your comments are all spot on.

Re: portable way to get highest bit set?

<87edi0qide.fsf@bsb.me.uk>

  copy mid

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

  copy link   Newsgroups: comp.lang.c alt.comp.lang.c
Path: i2pn2.org!i2pn.org!paganini.bofh.team!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ben.use...@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.lang.c,alt.comp.lang.c
Subject: Re: portable way to get highest bit set?
Date: Thu, 12 Oct 2023 10:50:53 +0100
Organization: A noiseless patient Spider
Lines: 30
Message-ID: <87edi0qide.fsf@bsb.me.uk>
References: <ug5gvh$1jkar$3@dont-email.me> <ug70cu$219ov$1@dont-email.me>
<878r884vi5.fsf@bsb.me.uk> <ug86d3$2c9mp$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 8bit
Injection-Info: dont-email.me; posting-host="625706da703fbb31625f3f3bd250e95a";
logging-data="2562446"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18uZpiUlOL62XCtGqvub/Yfc4JHnN4ZmT0="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux)
Cancel-Lock: sha1:KZ4akkEyM6+W3N5+RK+T6688Op4=
sha1:/90hxeV2G9EqdvqxKgQABUtYwFw=
X-BSB-Auth: 1.6c7d286379ad60a7067a.20231012105053BST.87edi0qide.fsf@bsb.me.uk
 by: Ben Bacarisse - Thu, 12 Oct 2023 09:50 UTC

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!

--
Ben.

Re: portable way to get highest bit set?

<32e915a6-3aa4-4d6b-8cf3-ed0ad6de1d6cn@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:620a:8286:b0:765:a4f2:51ec with SMTP id ox6-20020a05620a828600b00765a4f251ecmr318261qkn.4.1697104364645;
Thu, 12 Oct 2023 02:52:44 -0700 (PDT)
X-Received: by 2002:a05:6871:414:b0:1e9:ba2a:1498 with SMTP id
d20-20020a056871041400b001e9ba2a1498mr378542oag.9.1697104364438; Thu, 12 Oct
2023 02:52:44 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!nntp.club.cc.cmu.edu!45.76.7.193.MISMATCH!3.us.feeder.erje.net!feeder.erje.net!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Thu, 12 Oct 2023 02:52:44 -0700 (PDT)
In-Reply-To: <4e3940f1-8c6e-4786-a9f6-f9f17e172b8fn@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=199.203.251.52; posting-account=ow8VOgoAAAAfiGNvoH__Y4ADRwQF1hZW
NNTP-Posting-Host: 199.203.251.52
References: <ug5gvh$1jkar$3@dont-email.me> <20231011102714.44a870af4dfe68f756974953@g{oogle}mail.com>
<ug6huc$1rvp1$1@dont-email.me> <86h6mxawqq.fsf@linuxsc.com>
<ug71l8$21irh$1@dont-email.me> <4e3940f1-8c6e-4786-a9f6-f9f17e172b8fn@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <32e915a6-3aa4-4d6b-8cf3-ed0ad6de1d6cn@googlegroups.com>
Subject: Re: portable way to get highest bit set?
From: already5...@yahoo.com (Michael S)
Injection-Date: Thu, 12 Oct 2023 09:52:44 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 5831
 by: Michael S - Thu, 12 Oct 2023 09:52 UTC

On Thursday, October 12, 2023 at 12:20:39 PM UTC+3, Michael S wrote:
> On Wednesday, October 11, 2023 at 11:47:56 PM UTC+3, David Brown wrote:
> > On 11/10/2023 19:34, Tim Rentsch wrote:
> > > candycanearter07 <n...@thanks.net> writes:
> > >
> > >> On 10/11/23 02:27, Anton Shepelev wrote:
> > >>
> > >>> candycanearter07:
> > >>>
> > >>>> What is the best/most portable way to get the highest bit
> > >>>> set?
> > >>>>
> > >>>> ie. 011010001
> > >>>> to 010000000
> > >>>
> > >>> What is your best attempt, even if unsuccessful?
> > >>
> > >> int out;
> > >> for(out = 0x100000000; out; out >> 1)
> > >> if(out & input) break;
> > >>
> > >> I'm trying to find something size independent, though.
> > >> Or at least able to be swapped out with a #define
> > >
> > > I've coded up a dozen[*] different solutions. Here is my current
> > > favorite[**]:
> > >
> > > typedef union {
> > > long double d;
> > > struct { unsigned long long s; unsigned e:15;} b;
> > > } X;
> > >
> > > unsigned long long
> > > high_bit_set( unsigned long long u ){
> > > return 1ULL << (X){ .d = u }.b.e - 16383 & u;
> > > }
> > >
> > >
> > > This answer has these nice properties:
> > >
> > > * Single line function body
> > Irrelevant.
> > > * No looping or testing
> > That could be useful for efficiency. But a solution that can boil down
> > to some kind of "count leading zeros" or "find first one" instruction
> > will be much more efficient. (And the shift will be a loop on smaller
> > processors.) And if you had bothered to look at the generated code,
> > even on efficient compilers for the one target it supports, you'd see it
> > generates tests and either a branch or a conditional move.
> > > * Large domain of inputs supported
> > True - it supports up to 64 bits.
> >
> > > * Widely portable
> >
> > Completely incorrect - unless you think that x86 extended precision
> > floating point format is "widely portable". It's not even portable in
> > the x86 world. Basically, it will fail for any compiler for which "long
> > double" is the same format as "double", which includes MSVC, and gcc for
> > many non-x86 targets.
> It will also fail for any compiler for which "long double" is *not* the same
> format as "double", but also not the same as Intel's 80-bit. That includes all
> aarch64 Linux compiler where 'long double' is equivalent of IEEE
> binary128 and is implemented in software.
> > > * Very few cases with undefined behavior
> > Why would you accept any cases?
> > > * Forms an excellent basis for several interview questions
> > Anyone suggesting such a solution for an interview in my world would get
> > an extremely poor scoring. But they might get points for showing why it
> > is such a bad idea.
> > > * Sure to launch a torrent of followups on its pros and cons
> > /That/ one I agree on, though of course you will ignore my reply.
> >
> > It also has not-nice properties including:
> >
> > * Totally dependent on floating point standards found only on some
> > compilers.
> >
> > * Slow and inefficient on x86, because it requires non-SIMD floating point.
> >
> That's not true. On prevalent Intel and AMD processors, for scalar
> calculations like above, x87 FPU is about as fast SSE/AVX FPU.
> Speed wise, on x64 Linux or BSD Tim's solution is quite o.k, second
> only to non-portable intrinsic and to non-existing C23 variants.
> > * Non-obvious code with a magic number, using features well outside the
> > problem domain.
> >
> > * Unnecessarily outside the C++ compatible subset of C (this is
> > important to some people).
> > >
> > > (I'm resisting the temptation to make an off-topic remark
> > > regarding the number of properties.)
> > >
> > >
> > > P.S. This response is meant to be more entertaining than
> > > useful. I do hope it helps broaden your thinking in
> > > searching for an answer.
> > >
> > It's fine to give some "off the beaten track" solutions, but don't lie
> > about them.

Test.
Trying to figure out why Eternal September does not show the message
that I am replying to.

Re: portable way to get highest bit set?

<5e191d45-b12a-421d-b3c0-4b4ff0fc2fd9n@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:ad4:4f0a:0:b0:66d:4e5:3d68 with SMTP id fb10-20020ad44f0a000000b0066d04e53d68mr84211qvb.1.1697105146092;
Thu, 12 Oct 2023 03:05:46 -0700 (PDT)
X-Received: by 2002:a05:6870:610a:b0:1e9:9340:2bb9 with SMTP id
s10-20020a056870610a00b001e993402bb9mr1791573oae.4.1697105145747; Thu, 12 Oct
2023 03:05:45 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Thu, 12 Oct 2023 03:05:45 -0700 (PDT)
In-Reply-To: <ug71l8$21irh$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=199.203.251.52; posting-account=ow8VOgoAAAAfiGNvoH__Y4ADRwQF1hZW
NNTP-Posting-Host: 199.203.251.52
References: <ug5gvh$1jkar$3@dont-email.me> <20231011102714.44a870af4dfe68f756974953@g{oogle}mail.com>
<ug6huc$1rvp1$1@dont-email.me> <86h6mxawqq.fsf@linuxsc.com> <ug71l8$21irh$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <5e191d45-b12a-421d-b3c0-4b4ff0fc2fd9n@googlegroups.com>
Subject: Re: portable way to get highest bit set?
From: already5...@yahoo.com (Michael S)
Injection-Date: Thu, 12 Oct 2023 10:05:46 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 4843
 by: Michael S - Thu, 12 Oct 2023 10:05 UTC

On Wednesday, October 11, 2023 at 11:47:56 PM UTC+3, David Brown wrote:
> On 11/10/2023 19:34, Tim Rentsch wrote:
> > candycanearter07 <n...@thanks.net> writes:
> >
> >> On 10/11/23 02:27, Anton Shepelev wrote:
> >>
> >>> candycanearter07:
> >>>
> >>>> What is the best/most portable way to get the highest bit
> >>>> set?
> >>>>
> >>>> ie. 011010001
> >>>> to 010000000
> >>>
> >>> What is your best attempt, even if unsuccessful?
> >>
> >> int out;
> >> for(out = 0x100000000; out; out >> 1)
> >> if(out & input) break;
> >>
> >> I'm trying to find something size independent, though.
> >> Or at least able to be swapped out with a #define
> >
> > I've coded up a dozen[*] different solutions. Here is my current
> > favorite[**]:
> >
> > typedef union {
> > long double d;
> > struct { unsigned long long s; unsigned e:15;} b;
> > } X;
> >
> > unsigned long long
> > high_bit_set( unsigned long long u ){
> > return 1ULL << (X){ .d = u }.b.e - 16383 & u;
> > }
> >
> >
> > This answer has these nice properties:
> >
> > * Single line function body
> Irrelevant.
> > * No looping or testing
> That could be useful for efficiency. But a solution that can boil down
> to some kind of "count leading zeros" or "find first one" instruction
> will be much more efficient. (And the shift will be a loop on smaller
> processors.) And if you had bothered to look at the generated code,
> even on efficient compilers for the one target it supports, you'd see it
> generates tests and either a branch or a conditional move.
> > * Large domain of inputs supported
> True - it supports up to 64 bits.
>
> > * Widely portable
>
> Completely incorrect - unless you think that x86 extended precision
> floating point format is "widely portable". It's not even portable in
> the x86 world. Basically, it will fail for any compiler for which "long
> double" is the same format as "double", which includes MSVC, and gcc for
> many non-x86 targets.
> > * Very few cases with undefined behavior
> Why would you accept any cases?
> > * Forms an excellent basis for several interview questions
> Anyone suggesting such a solution for an interview in my world would get
> an extremely poor scoring. But they might get points for showing why it
> is such a bad idea.
> > * Sure to launch a torrent of followups on its pros and cons
> /That/ one I agree on, though of course you will ignore my reply.
>
> It also has not-nice properties including:
>
> * Totally dependent on floating point standards found only on some
> compilers.
>
> * Slow and inefficient on x86, because it requires non-SIMD floating point.
>
> * Non-obvious code with a magic number, using features well outside the
> problem domain.
>
> * Unnecessarily outside the C++ compatible subset of C (this is
> important to some people).
> >
> > (I'm resisting the temptation to make an off-topic remark
> > regarding the number of properties.)
> >
> >
> > P.S. This response is meant to be more entertaining than
> > useful. I do hope it helps broaden your thinking in
> > searching for an answer.
> >
> It's fine to give some "off the beaten track" solutions, but don't lie
> about them.

Another test.
Still trying to figure out what Eternal September shows and what not.

Re: portable way to get highest bit set?

<c188371e-d304-4777-9195-be4ca1f72c98n@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:620a:25b:b0:774:1003:2bbb with SMTP id q27-20020a05620a025b00b0077410032bbbmr394322qkn.3.1697105707448;
Thu, 12 Oct 2023 03:15:07 -0700 (PDT)
X-Received: by 2002:a05:6808:138c:b0:3a8:48fc:aaa5 with SMTP id
c12-20020a056808138c00b003a848fcaaa5mr12133847oiw.5.1697105707255; Thu, 12
Oct 2023 03:15:07 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Thu, 12 Oct 2023 03:15:07 -0700 (PDT)
In-Reply-To: <ug71l8$21irh$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=199.203.251.52; posting-account=ow8VOgoAAAAfiGNvoH__Y4ADRwQF1hZW
NNTP-Posting-Host: 199.203.251.52
References: <ug5gvh$1jkar$3@dont-email.me> <20231011102714.44a870af4dfe68f756974953@g{oogle}mail.com>
<ug6huc$1rvp1$1@dont-email.me> <86h6mxawqq.fsf@linuxsc.com> <ug71l8$21irh$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <c188371e-d304-4777-9195-be4ca1f72c98n@googlegroups.com>
Subject: Re: portable way to get highest bit set?
From: already5...@yahoo.com (Michael S)
Injection-Date: Thu, 12 Oct 2023 10:15:07 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 5424
 by: Michael S - Thu, 12 Oct 2023 10:15 UTC

Repeating myself,
I can't figure out why Eternal September server refuses to show a previous
response.

On Wednesday, October 11, 2023 at 11:47:56 PM UTC+3, David Brown wrote:
> On 11/10/2023 19:34, Tim Rentsch wrote:
> > candycanearter07 <n...@thanks.net> writes:
> >
> >> On 10/11/23 02:27, Anton Shepelev wrote:
> >>
> >>> candycanearter07:
> >>>
> >>>> What is the best/most portable way to get the highest bit
> >>>> set?
> >>>>
> >>>> ie. 011010001
> >>>> to 010000000
> >>>
> >>> What is your best attempt, even if unsuccessful?
> >>
> >> int out;
> >> for(out = 0x100000000; out; out >> 1)
> >> if(out & input) break;
> >>
> >> I'm trying to find something size independent, though.
> >> Or at least able to be swapped out with a #define
> >
> > I've coded up a dozen[*] different solutions. Here is my current
> > favorite[**]:
> >
> > typedef union {
> > long double d;
> > struct { unsigned long long s; unsigned e:15;} b;
> > } X;
> >
> > unsigned long long
> > high_bit_set( unsigned long long u ){
> > return 1ULL << (X){ .d = u }.b.e - 16383 & u;
> > }
> >
> >
> > This answer has these nice properties:
> >
> > * Single line function body
> Irrelevant.
> > * No looping or testing
> That could be useful for efficiency. But a solution that can boil down
> to some kind of "count leading zeros" or "find first one" instruction
> will be much more efficient. (And the shift will be a loop on smaller
> processors.) And if you had bothered to look at the generated code,
> even on efficient compilers for the one target it supports, you'd see it
> generates tests and either a branch or a conditional move.
> > * Large domain of inputs supported
> True - it supports up to 64 bits.
>
> > * Widely portable
>
> Completely incorrect - unless you think that x86 extended precision
> floating point format is "widely portable". It's not even portable in
> the x86 world. Basically, it will fail for any compiler for which "long
> double" is the same format as "double", which includes MSVC, and gcc for
> many non-x86 targets.

It will also fail for any compiler for which "long double" is *not* the same
format as "double", but also not the same as Intel's 80-bit. That includes all
aarch64 Linux compiler where 'long double' is equivalent of IEEE
binary128 and is implemented in software.

> > * Very few cases with undefined behavior
> Why would you accept any cases?
> > * Forms an excellent basis for several interview questions
> Anyone suggesting such a solution for an interview in my world would get
> an extremely poor scoring. But they might get points for showing why it
> is such a bad idea.
> > * Sure to launch a torrent of followups on its pros and cons
> /That/ one I agree on, though of course you will ignore my reply.
>
> It also has not-nice properties including:
>
> * Totally dependent on floating point standards found only on some
> compilers.
>
> * Slow and inefficient on x86, because it requires non-SIMD floating point.

That's not true. On prevalent Intel and AMD processors, for scalar
calculations like above, x87 FPU is about as fast SSE/AVX FPU.
Speed wise, on x64 Linux or BSD Tim's solution is quite o.k, second
only to non-portable intrinsic and to non-yet-existing C23 variants.

>
> * Non-obvious code with a magic number, using features well outside the
> problem domain.
>
> * Unnecessarily outside the C++ compatible subset of C (this is
> important to some people).
> >
> > (I'm resisting the temptation to make an off-topic remark
> > regarding the number of properties.)
> >
> >
> > P.S. This response is meant to be more entertaining than
> > useful. I do hope it helps broaden your thinking in
> > searching for an answer.
> >
> It's fine to give some "off the beaten track" solutions, but don't lie
> about them.

Re: portable way to get highest bit set?

<878r88qh0o.fsf@bsb.me.uk>

  copy mid

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

  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: ben.use...@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.lang.c
Subject: Re: portable way to get highest bit set?
Date: Thu, 12 Oct 2023 11:20:07 +0100
Organization: A noiseless patient Spider
Lines: 59
Message-ID: <878r88qh0o.fsf@bsb.me.uk>
References: <ug5gvh$1jkar$3@dont-email.me>
<20231011102714.44a870af4dfe68f756974953@g{oogle}mail.com>
<ug6huc$1rvp1$1@dont-email.me> <86h6mxawqq.fsf@linuxsc.com>
<ug71l8$21irh$1@dont-email.me> <8734yg4ty3.fsf@bsb.me.uk>
<ug8dv1$2dsi2$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="625706da703fbb31625f3f3bd250e95a";
logging-data="2577434"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19OHAMRuAOdva+f881dyVZWM9/Sn233Gc8="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux)
Cancel-Lock: sha1:OtrLa5a8sF5ETA7fXJd2Uws6x28=
sha1:RXcxnhVSqUfQ/xkqHyiRFVNLhbo=
X-BSB-Auth: 1.c9b6009a3931d621b110.20231012112007BST.878r88qh0o.fsf@bsb.me.uk
 by: Ben Bacarisse - Thu, 12 Oct 2023 10:20 UTC

David Brown <david.brown@hesbynett.no> writes:

> On 12/10/2023 01:31, Ben Bacarisse wrote:
>> David Brown <david.brown@hesbynett.no> writes:
>>
>>> On 11/10/2023 19:34, Tim Rentsch wrote:
>> ...
>>>> * Very few cases with undefined behavior
>>>
>>> Why would you accept any cases?
>>>
>>>> * Forms an excellent basis for several interview questions
>>>
>>> Anyone suggesting such a solution for an interview in my world would get an
>>> extremely poor scoring. But they might get points for showing why it is
>>> such a bad idea.
>> Exactly. That's what Tim said -- good for several interview questions.
>> I think you missed the whole tone of the post. There were lots of
>> signals, but surely "Very few cases with undefined behavior" should have
>> sealed it.
>>
>>>> (I'm resisting the temptation to make an off-topic remark
>>>> regarding the number of properties.)
>>>> P.S. This response is meant to be more entertaining than
>>>> useful. I do hope it helps broaden your thinking in
>>>> searching for an answer.
>>>
>>> It's fine to give some "off the beaten track" solutions, but don't lie
>>> about them.
>> What is the lie? I don't see one.
>>
>
> "Widely portable". In general, I thought his post might mislead people
> into thinking it was a serious line of attack for the problem - creative
> uses of floating point operations have been seen before for this kind of
> thing. (I started writing a longer explanation, but scraped it to avoid
> rambling off topic, so we can get back to the C.)

Lying involves a deliberate intent to decisive. The intent here was to
amuse and entertain. Now it's not always easy to get that across in a
post, so there is a risk that misleading information (pretty much the
whole of the post) could be taken seriously, but that's not lying.

> One thing that I did find interesting, however, was when I tried it on
> godbolt and made a couple of test functions like :
>
> unsigned test1(void) { return high_bit_set(7654(; }
>
> gcc was happy to pre-calculate the correct result at compile time. I know
> gcc (and clang) can simulate floating point hardware accurately at compile
> time, but it surprised me that these compilers managed that.

Interesting. I would not have expected that either. Mind you, there is
no need to simulate the /behaviour/ of the hardware. The code relies
only on the meaning of a set of bits when interpreted as floating point
so it is, perhaps, at the easier end of the spectrum.

--
Ben.

Re: portable way to get highest bit set?

<20231012132842.000054bd@yahoo.com>

  copy mid

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

  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 13:28:42 +0300
Organization: A noiseless patient Spider
Lines: 127
Message-ID: <20231012132842.000054bd@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>
<ug71l8$21irh$1@dont-email.me>
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="2505757"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+rDHZrG7bu7dYgQVeP/LRIcByjdQxViX0="
Cancel-Lock: sha1:LX8uLavDdee23b0NeXM88IdYfw0=
X-Newsreader: Claws Mail 3.19.1 (GTK+ 2.24.33; x86_64-w64-mingw32)
 by: Michael S - Thu, 12 Oct 2023 10:28 UTC

Repeating myself once again.
I can't figure out why Eternal September server refuses to show a
previous response twice.

On Wed, 11 Oct 2023 22:47:35 +0200
David Brown <david.brown@hesbynett.no> wrote:

> On 11/10/2023 19:34, Tim Rentsch wrote:
> > candycanearter07 <no@thanks.net> writes:
> >
> >> On 10/11/23 02:27, Anton Shepelev wrote:
> >>
> >>> candycanearter07:
> >>>
> >>>> What is the best/most portable way to get the highest bit
> >>>> set?
> >>>>
> >>>> ie. 011010001
> >>>> to 010000000
> >>>
> >>> What is your best attempt, even if unsuccessful?
> >>
> >> int out;
> >> for(out = 0x100000000; out; out >> 1)
> >> if(out & input) break;
> >>
> >> I'm trying to find something size independent, though.
> >> Or at least able to be swapped out with a #define
> >
> > I've coded up a dozen[*] different solutions. Here is my current
> > favorite[**]:
> >
> > typedef union {
> > long double d;
> > struct { unsigned long long s; unsigned e:15;} b;
> > } X;
> >
> > unsigned long long
> > high_bit_set( unsigned long long u ){
> > return 1ULL << (X){ .d = u }.b.e - 16383 & u;
> > }
> >
> >
> > This answer has these nice properties:
> >
> > * Single line function body
>
> Irrelevant.
>
> > * No looping or testing
>
> That could be useful for efficiency. But a solution that can boil
> down to some kind of "count leading zeros" or "find first one"
> instruction will be much more efficient. (And the shift will be a
> loop on smaller processors.) And if you had bothered to look at the
> generated code, even on efficient compilers for the one target it
> supports, you'd see it generates tests and either a branch or a
> conditional move.
>
> > * Large domain of inputs supported
>
> True - it supports up to 64 bits.
>
> > * Widely portable
>
> Completely incorrect - unless you think that x86 extended precision
> floating point format is "widely portable". It's not even portable
> in the x86 world. Basically, it will fail for any compiler for which
> "long double" is the same format as "double", which includes MSVC,
> and gcc for many non-x86 targets.
>

It will also fail for any compiler for which "long double" is *not* the
same format as "double", but also not the same as Intel's 80-bit. That
includes all aarch64 Linux compiler where 'long double' is equivalent
of IEEE binary128 and is implemented in software.

> > * Very few cases with undefined behavior
>
> Why would you accept any cases?
>
> > * Forms an excellent basis for several interview questions
>
> Anyone suggesting such a solution for an interview in my world would
> get an extremely poor scoring. But they might get points for showing
> why it is such a bad idea.
>
> > * Sure to launch a torrent of followups on its pros and cons
>
> /That/ one I agree on, though of course you will ignore my reply.
>
> It also has not-nice properties including:
>
> * Totally dependent on floating point standards found only on some
> compilers.
>
> * Slow and inefficient on x86, because it requires non-SIMD floating
> point.
>

That's not true. On prevalent Intel and AMD processors, for scalar
calculations like above, x87 FPU is about as fast SSE/AVX FPU.
Speed wise, on x64 Linux or BSD Tim's solution is quite o.k, second
only to non-portable intrinsic and to non-existing C23 variants.

> * Non-obvious code with a magic number, using features well outside
> the problem domain.
>
> * Unnecessarily outside the C++ compatible subset of C (this is
> important to some people).
>
> >
> > (I'm resisting the temptation to make an off-topic remark
> > regarding the number of properties.)
> >
> >
> > P.S. This response is meant to be more entertaining than
> > useful. I do hope it helps broaden your thinking in
> > searching for an answer.
> >
>
> It's fine to give some "off the beaten track" solutions, but don't
> lie about them.
>
>

Re: portable way to get highest bit set?

<20231012141719.99f5a10ec921db3ee6f7d948@g{oogle}mail.com>

  copy mid

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

  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 14:17:19 +0300
Organization: A noiseless patient Spider
Lines: 53
Message-ID: <20231012141719.99f5a10ec921db3ee6f7d948@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>
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="2599889"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/oXPGLeTLRkdVV0yuZ489envw0wNXvuys="
Cancel-Lock: sha1:i6fAvGT63fw5BnSNapDd0PeCXHs=
X-Newsreader: Sylpheed 3.7.0 (GTK+ 2.24.30; i686-pc-mingw32)
 by: Anton Shepelev - Thu, 12 Oct 2023 11:17 UTC

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

unsigned long long highest_bit_2( void* val, size_t sz )
{ int i,f,inc;
unsigned char *p = val;
unsigned char c;
char le = is_le();

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

f = sz;
for( i = 0; i < sz; i += 1 )
{ if( (c = *p) == 0 )
{ p += inc; }
else
{ f = i;
break;
}
}
return highest_bit_1( c ) << (CHAR_BIT*(sz-1-f));
}

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

Re: portable way to get highest bit set?

<20231012142030.edd82e8ad2b3c10bba7931a5@g{oogle}mail.com>

  copy mid

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

  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 14:20:30 +0300
Organization: A noiseless patient Spider
Lines: 11
Message-ID: <20231012142030.edd82e8ad2b3c10bba7931a5@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>
<ug71l8$21irh$1@dont-email.me>
<4e3940f1-8c6e-4786-a9f6-f9f17e172b8fn@googlegroups.com>
<32e915a6-3aa4-4d6b-8cf3-ed0ad6de1d6cn@googlegroups.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="2599889"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/RtVGZ45X20vSZoMeSZtP3Fwe9AmFOD1c="
Cancel-Lock: sha1:bcOK4CZ/1/1DRQYVl55FUNPsZVY=
X-Newsreader: Sylpheed 3.7.0 (GTK+ 2.24.30; i686-pc-mingw32)
 by: Anton Shepelev - Thu, 12 Oct 2023 11:20 UTC

Michael S:

> Test. Trying to figure out why Eternal September does not
> show the message that I am replying to.

If you suspect a false positive for its SPAM filter, report
its `Message-ID:' header in eternal-september.support .

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

Re: portable way to get highest bit set?

<ug8ll6$2fjms$1@dont-email.me>

  copy mid

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

  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 13:35:01 +0200
Organization: A noiseless patient Spider
Lines: 33
Message-ID: <ug8ll6$2fjms$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 11:35:02 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="e61172f60fef4771e29be2ec94b64ca0";
logging-data="2608860"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19puRFxELHwp1u7IboM/Hd2"
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:z/1xvltMXQXfJpi8AJjFCRp7U9Q=
In-Reply-To: <87edi0qide.fsf@bsb.me.uk>
 by: jak - Thu, 12 Oct 2023 11:35 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!
>

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.

Re: portable way to get highest bit set?

<22409111-1019-4b5f-9aa1-f28e0b109631n@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a0c:c486:0:b0:66d:365:a767 with SMTP id u6-20020a0cc486000000b0066d0365a767mr99640qvi.8.1697113737902;
Thu, 12 Oct 2023 05:28:57 -0700 (PDT)
X-Received: by 2002:a05:6870:5b0b:b0:1e9:a86f:ec3b with SMTP id
ds11-20020a0568705b0b00b001e9a86fec3bmr942644oab.2.1697113737752; Thu, 12 Oct
2023 05:28:57 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Thu, 12 Oct 2023 05:28:57 -0700 (PDT)
In-Reply-To: <ug8ll6$2fjms$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=199.203.251.52; posting-account=ow8VOgoAAAAfiGNvoH__Y4ADRwQF1hZW
NNTP-Posting-Host: 199.203.251.52
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>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <22409111-1019-4b5f-9aa1-f28e0b109631n@googlegroups.com>
Subject: Re: portable way to get highest bit set?
From: already5...@yahoo.com (Michael S)
Injection-Date: Thu, 12 Oct 2023 12:28:57 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 2920
 by: Michael S - Thu, 12 Oct 2023 12:28 UTC

On Thursday, October 12, 2023 at 2:35:34 PM UTC+3, jak wrote:
> Ben Bacarisse ha scritto:
> > jak <nos...@please.ty> writes:
> >
> >> Ben Bacarisse ha scritto:
> >>> jak <nos...@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.

Re: portable way to get highest bit set?

<20231012153657.000056bc@yahoo.com>

  copy mid

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

  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: already5...@yahoo.com (Michael S)
Newsgroups: comp.lang.c,alt.comp.lang.c
Subject: Re: portable way to get highest bit set?
Date: Thu, 12 Oct 2023 15:36:57 +0300
Organization: A noiseless patient Spider
Lines: 84
Message-ID: <20231012153657.000056bc@yahoo.com>
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>
MIME-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Injection-Info: dont-email.me; posting-host="df0da282e3f59609704589ea08055b8e";
logging-data="2631684"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/uRa3m8sxCgJeqVX9lLR/kndjKxHs+McE="
Cancel-Lock: sha1:4W43Dek/OS/4XQAMEVIMJnYUCiY=
X-Newsreader: Claws Mail 3.19.1 (GTK+ 2.24.33; x86_64-w64-mingw32)
 by: Michael S - Thu, 12 Oct 2023 12:36 UTC

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.

Re: portable way to get highest bit set?

<ug8qeg$2gl42$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: portable way to get highest bit set?
Date: Thu, 12 Oct 2023 14:56:47 +0200
Organization: A noiseless patient Spider
Lines: 92
Message-ID: <ug8qeg$2gl42$1@dont-email.me>
References: <ug5gvh$1jkar$3@dont-email.me>
<20231011102714.44a870af4dfe68f756974953@g{oogle}mail.com>
<ug6huc$1rvp1$1@dont-email.me> <86h6mxawqq.fsf@linuxsc.com>
<ug71l8$21irh$1@dont-email.me> <8734yg4ty3.fsf@bsb.me.uk>
<ug8dv1$2dsi2$1@dont-email.me> <878r88qh0o.fsf@bsb.me.uk>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 12 Oct 2023 12:56:48 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="31bfe17730ce6012eded28f48a4621e4";
logging-data="2643074"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+m3jP295HyAReb8uUznyicyjwcrMZkBbk="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:svTRT2ObGHIKqUjd2/CMqNQKDuQ=
Content-Language: en-GB
In-Reply-To: <878r88qh0o.fsf@bsb.me.uk>
 by: David Brown - Thu, 12 Oct 2023 12:56 UTC

On 12/10/2023 12:20, Ben Bacarisse wrote:
> David Brown <david.brown@hesbynett.no> writes:
>
>> On 12/10/2023 01:31, Ben Bacarisse wrote:
>>> David Brown <david.brown@hesbynett.no> writes:
>>>
>>>> On 11/10/2023 19:34, Tim Rentsch wrote:
>>> ...
>>>>> * Very few cases with undefined behavior
>>>>
>>>> Why would you accept any cases?
>>>>
>>>>> * Forms an excellent basis for several interview questions
>>>>
>>>> Anyone suggesting such a solution for an interview in my world would get an
>>>> extremely poor scoring. But they might get points for showing why it is
>>>> such a bad idea.
>>> Exactly. That's what Tim said -- good for several interview questions.
>>> I think you missed the whole tone of the post. There were lots of
>>> signals, but surely "Very few cases with undefined behavior" should have
>>> sealed it.
>>>
>>>>> (I'm resisting the temptation to make an off-topic remark
>>>>> regarding the number of properties.)
>>>>> P.S. This response is meant to be more entertaining than
>>>>> useful. I do hope it helps broaden your thinking in
>>>>> searching for an answer.
>>>>
>>>> It's fine to give some "off the beaten track" solutions, but don't lie
>>>> about them.
>>> What is the lie? I don't see one.
>>>
>>
>> "Widely portable". In general, I thought his post might mislead people
>> into thinking it was a serious line of attack for the problem - creative
>> uses of floating point operations have been seen before for this kind of
>> thing. (I started writing a longer explanation, but scraped it to avoid
>> rambling off topic, so we can get back to the C.)
>
> Lying involves a deliberate intent to decisive. The intent here was to
> amuse and entertain. Now it's not always easy to get that across in a
> post, so there is a risk that misleading information (pretty much the
> whole of the post) could be taken seriously, but that's not lying.

That's a reasonable viewpoint. I think Tim should have considered that
his post might be misinterpreted and worded it differently (the
readership includes people relatively new to C, people for whom English
is a challenge, and people for who find it difficult to interpret
subtexts such as sarcasm or parody), but I agree that he did not
deliberately intend to deceive, so "lie" was an unfair and inappropriate
choice of words.

>
>> One thing that I did find interesting, however, was when I tried it on
>> godbolt and made a couple of test functions like :
>>
>> unsigned test1(void) { return high_bit_set(7654(; }
>>
>> gcc was happy to pre-calculate the correct result at compile time. I know
>> gcc (and clang) can simulate floating point hardware accurately at compile
>> time, but it surprised me that these compilers managed that.
>
> Interesting. I would not have expected that either. Mind you, there is
> no need to simulate the /behaviour/ of the hardware. The code relies
> only on the meaning of a set of bits when interpreted as floating point
> so it is, perhaps, at the easier end of the spectrum.
>

I am not an expert on the details of floating point - for my own use, I
am happy with the "they give approximate answers to real-number
calculations" interpretation. I am under the impression - perhaps
incorrectly - that some types of floating point calculations can give
slightly different results on different implementations. That is why
gcc includes a rather large library that aims to mimic real floating
point hardware for such compile-time calculation. But it is entirely
possible that this was not needed here, and the behaviour is fully
covered by IEEE floating point specifications.

Regardless, I thought the optimisation here was impressive.

A more useful challenge might be to find a way to express the problem
(for a given fixed size of integer type) in standard C in a way that gcc
and/or clang can turn into a "count leading zeros" or similar assembly
instruction. The most efficient implementation of this function is
typically achievable by using a C23 <stdbit.h> function, a compiler
__builtin instruction, or a target-specific intrinsic or inline
assembly. It would be nice to see a portable C implementation that gave
similar efficiency in the generated code, at least for some
compiler/target/flag combinations.


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

Pages:12345678
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor