Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

BREAKFAST.COM Halted... Cereal Port Not Responding.


devel / comp.lang.c / 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
portable way to get highest bit set?

<ug5gvh$1jkar$3@dont-email.me>

  copy mid

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

  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: portable way to get highest bit set?
Date: Wed, 11 Oct 2023 01:56:49 -0500
Organization: A noiseless patient Spider
Lines: 8
Message-ID: <ug5gvh$1jkar$3@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 11 Oct 2023 06:56:50 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="18d77e06698ddff0bf5ccf97f96b9019";
logging-data="1691995"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/8njq9cZaoyv2+BU6uhzYbsXsaVI8tPMoXcV2J+Xgt5Q=="
User-Agent: Betterbird (Linux)
Cancel-Lock: sha1:9bw7P29l0wBJ+PTIm6p+5XUZLeo=
Content-Language: en-US
 by: candycanearter07 - Wed, 11 Oct 2023 06:56 UTC

Hi,

What is the best/most portable way to get the highest bit set?

ie. 011010001
to 010000000
--
user <candycane> is generated from /dev/urandom

Re: portable way to get highest bit set?

<20231011102714.44a870af4dfe68f756974953@g{oogle}mail.com>

  copy mid

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

  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: anton....@g{oogle}mail.com (Anton Shepelev)
Newsgroups: comp.lang.c,alt.comp.lang.c
Subject: Re: portable way to get highest bit set?
Date: Wed, 11 Oct 2023 10:27:14 +0300
Organization: A noiseless patient Spider
Lines: 13
Message-ID: <20231011102714.44a870af4dfe68f756974953@g{oogle}mail.com>
References: <ug5gvh$1jkar$3@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="ab19fc9555195d07fbc1b9946d240985";
logging-data="1805456"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+0t7VZl0quf/LggQulT0QMb3ECyDGISFg="
Cancel-Lock: sha1:IyzdaeYgUxb1AGeOgdbeNQutkhs=
X-Newsreader: Sylpheed 3.7.0 (GTK+ 2.24.30; i686-pc-mingw32)
 by: Anton Shepelev - Wed, 11 Oct 2023 07:27 UTC

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?

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

Re: portable way to get highest bit set?

<ug5q1l$1on2h$1@dont-email.me>

  copy mid

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

  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: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c,alt.comp.lang.c
Subject: Re: portable way to get highest bit set?
Date: Wed, 11 Oct 2023 11:31:32 +0200
Organization: A noiseless patient Spider
Lines: 32
Message-ID: <ug5q1l$1on2h$1@dont-email.me>
References: <ug5gvh$1jkar$3@dont-email.me>
<20231011102714.44a870af4dfe68f756974953@g{oogle}mail.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 11 Oct 2023 09:31:33 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="c643b1eac144b80c8482829ab7e66cdb";
logging-data="1858641"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18Go0UgbAZ2yqWwzsdHFY3DkOiwQwK5684="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:7g8YeQqIibvjzjUOawp2HQvYAgg=
Content-Language: en-GB
In-Reply-To: <20231011102714.44a870af4dfe68f756974953@g{oogle}mail.com>
 by: David Brown - Wed, 11 Oct 2023 09:31 UTC

On 11/10/2023 09: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?
>

And what is your definition of "best" and "most portable" ?

"Best" can mean smallest, neatest, fastest, and that can depend highly
on the compiler, target and flags.

"Portable" can mean relying only on pure standard C (and if so, which
standard version?), or one compiler but many targets, or one target and
many compilers, or a group of common targets and compilers excluding
"unusual" ones.

And what types do you want to support? All integer types, or just some
specific ones?

What is the specification for the function you want? In particular,
what should it do for 0? Do you really want a result with one bit set,
or do you actually want the bit number?

Re: portable way to get highest bit set?

<20231011123329.086687ffe639d443a6a7adef@g{oogle}mail.com>

  copy mid

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

  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: anton....@g{oogle}mail.com (Anton Shepelev)
Newsgroups: comp.lang.c,alt.comp.lang.c
Subject: Re: portable way to get highest bit set?
Date: Wed, 11 Oct 2023 12:33:29 +0300
Organization: A noiseless patient Spider
Lines: 11
Message-ID: <20231011123329.086687ffe639d443a6a7adef@g{oogle}mail.com>
References: <ug5gvh$1jkar$3@dont-email.me>
<20231011102714.44a870af4dfe68f756974953@g{oogle}mail.com>
<ug5q1l$1on2h$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="ab19fc9555195d07fbc1b9946d240985";
logging-data="1854300"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+20vOjKCsklEBWKscOg5FKg0ZO2ubSuEY="
Cancel-Lock: sha1:iGQqqV78l11LjLDTCzmD9hNeDQw=
X-Newsreader: Sylpheed 3.7.0 (GTK+ 2.24.30; i686-pc-mingw32)
 by: Anton Shepelev - Wed, 11 Oct 2023 09:33 UTC

David Brown:

> And what types do you want to support? All integer types,
> or just some specific ones?

The OP's example shows a byte -- an 8-bit value, but I
encourage them to try and publish their own solution first.

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

Re: portable way to get highest bit set?

<ug62lb$1qfmd$1@dont-email.me>

  copy mid

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

  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: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c,alt.comp.lang.c
Subject: Re: portable way to get highest bit set?
Date: Wed, 11 Oct 2023 13:58:35 +0200
Organization: A noiseless patient Spider
Lines: 15
Message-ID: <ug62lb$1qfmd$1@dont-email.me>
References: <ug5gvh$1jkar$3@dont-email.me>
<20231011102714.44a870af4dfe68f756974953@g{oogle}mail.com>
<ug5q1l$1on2h$1@dont-email.me>
<20231011123329.086687ffe639d443a6a7adef@g{oogle}mail.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 11 Oct 2023 11:58:35 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="c643b1eac144b80c8482829ab7e66cdb";
logging-data="1916621"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/mWZ9n1xgZWoSyiNpYQxBgluzc7lD56G0="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:Av8ChLyQ8I3r886rcNwaMcv3eEU=
In-Reply-To: <20231011123329.086687ffe639d443a6a7adef@g{oogle}mail.com>
Content-Language: en-GB
 by: David Brown - Wed, 11 Oct 2023 11:58 UTC

On 11/10/2023 11:33, Anton Shepelev wrote:
> David Brown:
>
>> And what types do you want to support? All integer types,
>> or just some specific ones?
>
> The OP's example shows a byte -- an 8-bit value, but I
> encourage them to try and publish their own solution first.
>

I fully agree with that suggestion.

But he/she also needs to say if 8-bit values are all that are of
interest, when considering portability.

Re: portable way to get highest bit set?

<20231011151734.ba0c52c02ebe2e2ccd99b67d@g{oogle}mail.com>

  copy mid

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

  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: anton....@g{oogle}mail.com (Anton Shepelev)
Newsgroups: comp.lang.c,alt.comp.lang.c
Subject: Re: portable way to get highest bit set?
Date: Wed, 11 Oct 2023 15:17:34 +0300
Organization: A noiseless patient Spider
Lines: 13
Message-ID: <20231011151734.ba0c52c02ebe2e2ccd99b67d@g{oogle}mail.com>
References: <ug5gvh$1jkar$3@dont-email.me>
<20231011102714.44a870af4dfe68f756974953@g{oogle}mail.com>
<ug5q1l$1on2h$1@dont-email.me>
<20231011123329.086687ffe639d443a6a7adef@g{oogle}mail.com>
<ug62lb$1qfmd$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="ab19fc9555195d07fbc1b9946d240985";
logging-data="1922772"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19IYI9wh2xpbhZ4Q2ab6y20GolC2InIR3E="
Cancel-Lock: sha1:kWtB4uW+AuVX/emPYgV1RsZPelE=
X-Newsreader: Sylpheed 3.7.0 (GTK+ 2.24.30; i686-pc-mingw32)
 by: Anton Shepelev - Wed, 11 Oct 2023 12:17 UTC

David Brown:

> But he/she also needs to say if 8-bit values are all that
> are of interest, when considering portability.

Yes, endianness, and then a hackish way to hanle it is to
use socket functions. Hackish, because conceptualy that
means the reliance of a general low-level function on a
more specific and higher-level function/library.

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

Re: portable way to get highest bit set?

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

  copy mid

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

  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: 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: Wed, 11 Oct 2023 13:56:10 +0100
Organization: A noiseless patient Spider
Lines: 14
Message-ID: <87r0m15ndh.fsf@bsb.me.uk>
References: <ug5gvh$1jkar$3@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="2e91839ee387702d4c2caeb7b10fd37b";
logging-data="1941170"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1//pCo7rdgZDlBBmfVclKtygOWfKEFPXm0="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux)
Cancel-Lock: sha1:uiSIsTmx4nbBRUL0BxxhRIz7mw8=
sha1:1NyOVV+ezkvaFTnis7W3uNtBl2k=
X-BSB-Auth: 1.fa9331fe7933df0823ad.20231011135610BST.87r0m15ndh.fsf@bsb.me.uk
 by: Ben Bacarisse - Wed, 11 Oct 2023 12:56 UTC

candycanearter07 <no@thanks.net> writes:

> What is the best/most portable way to get the highest bit set?
>
> ie. 011010001
> to 010000000

While bit twiddling solutions will be needed for some years, it's worth
noting that C23 has <stdbit.h> which includes functions (including
type-generic ones) like stdc_bit_floor. C++ has had something similar
since C++20.

--
Ben.

Re: portable way to get highest bit set?

<ug6b3n$1sbid$1@dont-email.me>

  copy mid

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

  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: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c,alt.comp.lang.c
Subject: Re: portable way to get highest bit set?
Date: Wed, 11 Oct 2023 16:22:47 +0200
Organization: A noiseless patient Spider
Lines: 15
Message-ID: <ug6b3n$1sbid$1@dont-email.me>
References: <ug5gvh$1jkar$3@dont-email.me>
<20231011102714.44a870af4dfe68f756974953@g{oogle}mail.com>
<ug5q1l$1on2h$1@dont-email.me>
<20231011123329.086687ffe639d443a6a7adef@g{oogle}mail.com>
<ug62lb$1qfmd$1@dont-email.me>
<20231011151734.ba0c52c02ebe2e2ccd99b67d@g{oogle}mail.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 11 Oct 2023 14:22:47 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="c643b1eac144b80c8482829ab7e66cdb";
logging-data="1977933"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+8/fZVlCBf1dZn6SW1DiB6zaYpGx1zuAY="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:PxGeSbqB5JSiFhN2GXw28tB9WyY=
In-Reply-To: <20231011151734.ba0c52c02ebe2e2ccd99b67d@g{oogle}mail.com>
Content-Language: en-GB
 by: David Brown - Wed, 11 Oct 2023 14:22 UTC

On 11/10/2023 14:17, Anton Shepelev wrote:
> David Brown:
>
>> But he/she also needs to say if 8-bit values are all that
>> are of interest, when considering portability.
>
> Yes, endianness, and then a hackish way to hanle it is to
> use socket functions. Hackish, because conceptualy that
> means the reliance of a general low-level function on a
> more specific and higher-level function/library.
>

Endianness should not be an issue. The question is about values, and
values don't have endianness. Only storage has endianness.

Re: portable way to get highest bit set?

<ug6cv8$1r3g4$1@dont-email.me>

  copy mid

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

  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: Wed, 11 Oct 2023 14:54:32 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 18
Message-ID: <ug6cv8$1r3g4$1@dont-email.me>
References: <ug5gvh$1jkar$3@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 11 Oct 2023 14:54:32 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="b073f97b156e77adb08f4b90dc62385a";
logging-data="1936900"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/2X/yfL6Ca6QlYzlm8gRjkNnFoLTxntmM="
User-Agent: Pan/0.139 (Sexual Chocolate; GIT bf56508
git://git.gnome.org/pan2)
Cancel-Lock: sha1:rj5Z+A5YsMlVqjxgl7zzLe9kg9g=
 by: Lew Pitcher - Wed, 11 Oct 2023 14:54 UTC

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.

--
Lew Pitcher
"In Skills We Trust"

Re: portable way to get highest bit set?

<20231011180836.601f0fa22beafde974dfbc1c@g{oogle}mail.com>

  copy mid

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

  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: anton....@g{oogle}mail.com (Anton Shepelev)
Newsgroups: comp.lang.c,alt.comp.lang.c
Subject: Re: portable way to get highest bit set?
Date: Wed, 11 Oct 2023 18:08:36 +0300
Organization: A noiseless patient Spider
Lines: 11
Message-ID: <20231011180836.601f0fa22beafde974dfbc1c@g{oogle}mail.com>
References: <ug5gvh$1jkar$3@dont-email.me>
<20231011102714.44a870af4dfe68f756974953@g{oogle}mail.com>
<ug5q1l$1on2h$1@dont-email.me>
<20231011123329.086687ffe639d443a6a7adef@g{oogle}mail.com>
<ug62lb$1qfmd$1@dont-email.me>
<20231011151734.ba0c52c02ebe2e2ccd99b67d@g{oogle}mail.com>
<ug6b3n$1sbid$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="ab19fc9555195d07fbc1b9946d240985";
logging-data="1997141"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/mI1Enty91D+we0YvnCHJ+M7AYurh/4MQ="
Cancel-Lock: sha1:wSYk1D9IoLWd0WKOIlm/bN+qB9Q=
X-Newsreader: Sylpheed 3.7.0 (GTK+ 2.24.30; i686-pc-mingw32)
 by: Anton Shepelev - Wed, 11 Oct 2023 15:08 UTC

David Brown corrected Anton Shepelev:

> Endianness should not be an issue. The question is about
> values, and values don't have endianness. Only storage
> has endianness.

I blundered bad!

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

Re: portable way to get highest bit set?

<ug6huc$1rvp1$1@dont-email.me>

  copy mid

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

  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 11:19:24 -0500
Organization: A noiseless patient Spider
Lines: 21
Message-ID: <ug6huc$1rvp1$1@dont-email.me>
References: <ug5gvh$1jkar$3@dont-email.me>
<20231011102714.44a870af4dfe68f756974953@g{oogle}mail.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 11 Oct 2023 16:19:25 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="18d77e06698ddff0bf5ccf97f96b9019";
logging-data="1965857"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18kN8sW51YhnnScFMareD9ogpbxv/Bz8spUElSFhLOQ6Q=="
User-Agent: Betterbird (Linux)
Cancel-Lock: sha1:Ob0Ua/XEtBNy2bXRqNA3JFr3I1Y=
In-Reply-To: <20231011102714.44a870af4dfe68f756974953@g{oogle}mail.com>
Content-Language: en-US
 by: candycanearter07 - Wed, 11 Oct 2023 16:19 UTC

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
--
user <candycane> is generated from /dev/urandom

Re: portable way to get highest bit set?

<874jix5dw7.fsf@bsb.me.uk>

  copy mid

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

  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: 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: Wed, 11 Oct 2023 17:20:56 +0100
Organization: A noiseless patient Spider
Lines: 10
Message-ID: <874jix5dw7.fsf@bsb.me.uk>
References: <ug5gvh$1jkar$3@dont-email.me>
<20231011102714.44a870af4dfe68f756974953@g{oogle}mail.com>
<ug5q1l$1on2h$1@dont-email.me>
<20231011123329.086687ffe639d443a6a7adef@g{oogle}mail.com>
<ug62lb$1qfmd$1@dont-email.me>
<20231011151734.ba0c52c02ebe2e2ccd99b67d@g{oogle}mail.com>
<ug6b3n$1sbid$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="2e91839ee387702d4c2caeb7b10fd37b";
logging-data="2028405"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX192KmJFXEoEsUmErqWxboHAB6/M3/fVtUY="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux)
Cancel-Lock: sha1:BpAeeidejKbJ/PQ0Nj5LXxTQ/rU=
sha1:QYb/rA6ZTy2wLyiyBiKn6rvb5eo=
X-BSB-Auth: 1.65e3c132d0cec40b9de1.20231011172056BST.874jix5dw7.fsf@bsb.me.uk
 by: Ben Bacarisse - Wed, 11 Oct 2023 16:20 UTC

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

> Endianness should not be an issue. The question is about values, and
> values don't have endianness. Only storage has endianness.

But solutions could use the byte representation and endianness would
then come into it.

--
Ben.

Re: portable way to get highest bit set?

<ug6if8$1u34j$1@dont-email.me>

  copy mid

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

  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: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c,alt.comp.lang.c
Subject: Re: portable way to get highest bit set?
Date: Wed, 11 Oct 2023 18:28:24 +0200
Organization: A noiseless patient Spider
Lines: 13
Message-ID: <ug6if8$1u34j$1@dont-email.me>
References: <ug5gvh$1jkar$3@dont-email.me>
<20231011102714.44a870af4dfe68f756974953@g{oogle}mail.com>
<ug5q1l$1on2h$1@dont-email.me>
<20231011123329.086687ffe639d443a6a7adef@g{oogle}mail.com>
<ug62lb$1qfmd$1@dont-email.me>
<20231011151734.ba0c52c02ebe2e2ccd99b67d@g{oogle}mail.com>
<ug6b3n$1sbid$1@dont-email.me> <874jix5dw7.fsf@bsb.me.uk>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 11 Oct 2023 16:28:24 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="5295c542bd6f342589dff8b16b57eae2";
logging-data="2034835"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+KCw5TDlxelm/LfmUfcAx1NGxQH8zGdl8="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:tX6jwjdK8PoNez3Qei9vu/G+qd4=
In-Reply-To: <874jix5dw7.fsf@bsb.me.uk>
Content-Language: en-GB
 by: David Brown - Wed, 11 Oct 2023 16:28 UTC

On 11/10/2023 18:20, Ben Bacarisse wrote:
> David Brown <david.brown@hesbynett.no> writes:
>
>> Endianness should not be an issue. The question is about values, and
>> values don't have endianness. Only storage has endianness.
>
> But solutions could use the byte representation and endianness would
> then come into it.
>

Sure. But that would be an artefact of a particular solution, rather
than being inherent in the problem.

Re: portable way to get highest bit set?

<ug6jef$1ubnu$1@dont-email.me>

  copy mid

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

  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: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c,alt.comp.lang.c
Subject: Re: portable way to get highest bit set?
Date: Wed, 11 Oct 2023 18:45:02 +0200
Organization: A noiseless patient Spider
Lines: 50
Message-ID: <ug6jef$1ubnu$1@dont-email.me>
References: <ug5gvh$1jkar$3@dont-email.me>
<20231011102714.44a870af4dfe68f756974953@g{oogle}mail.com>
<ug6huc$1rvp1$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 11 Oct 2023 16:45:03 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="5295c542bd6f342589dff8b16b57eae2";
logging-data="2043646"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/BgVyuXdk2pZfL667x8VI4jKgRKJrq0jI="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:ApUGcNhJ3MP7i2FV8G6ouxq47zs=
In-Reply-To: <ug6huc$1rvp1$1@dont-email.me>
Content-Language: en-GB
 by: David Brown - Wed, 11 Oct 2023 16:45 UTC

On 11/10/2023 18: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

OK, that's a start.

First, I strongly recommend you use unsigned integer types for anything
involving bit manipulation, masking, shifting, etc. You avoid any
awkwardness or potential undefined behaviour, and have the full range of
the bits. I also recommend using the C99 <stdint.h> size-specific
types. They are not quite as portable as the standard integer types,
but they are portable enough for the vast majority of real-world cases,
and you are always sure of the exact number of bits, which is often
clearer IMHO. (Note that not all C programmers will agree with my
preferences here.)

Now, you still have not answered what you mean by "portable". How I
would approach this would depend significantly on that. If "portable"
meant "any target for gcc or clang", I'd be using gcc builtins. If it
meant "any C23 compiler", I'd likely use "auto" in my solution. If it
meant "any C11 or C17 compiler", I'd use "_Generic". If it meant "any
C99 compiler", I'd use <stdint.h> types. If "portable" means any
standard C version, with any hypothetical implementation, it's going to
get messy putting together a solution that will work even for extended
unsigned integer types that are bigger than "unsigned long".

These different approaches can have significantly different
efficiencies, if that is important, but each has different requirements
of the standard and compiler.

Re: portable way to get highest bit set?

<b2e341ea-a49d-c0cf-d8ee-96235db44038@gmail.com>

  copy mid

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

  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: deathsta...@gmail.com (Ian C)
Newsgroups: comp.lang.c,alt.comp.lang.c
Subject: Re: portable way to get highest bit set?
Date: Wed, 11 Oct 2023 18:03:38 +0100
Organization: A noiseless patient Spider
Lines: 28
Message-ID: <b2e341ea-a49d-c0cf-d8ee-96235db44038@gmail.com>
References: <ug5gvh$1jkar$3@dont-email.me>
<20231011102714.44a870af4dfe68f756974953@g{oogle}mail.com>
<ug6huc$1rvp1$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Info: dont-email.me; posting-host="c45550e91e867ea6aa4e8876e5f5907d";
logging-data="2030851"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18BIOt83aePdsD4JKd4EjJNGV0QXzuuE3M="
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0)
Gecko/20100101 Thunderbird/102.15.1
Cancel-Lock: sha1:Y4arXblZtfpEYEwvq97lYx81e+Y=
In-Reply-To: <ug6huc$1rvp1$1@dont-email.me>
 by: Ian C - Wed, 11 Oct 2023 17:03 UTC

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

Re: portable way to get highest bit set?

<86mswpaxk2.fsf@linuxsc.com>

  copy mid

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

  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: Wed, 11 Oct 2023 10:17:17 -0700
Organization: A noiseless patient Spider
Lines: 35
Message-ID: <86mswpaxk2.fsf@linuxsc.com>
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=us-ascii
Injection-Info: dont-email.me; posting-host="c9974d524a342450c72757464b9d47a6";
logging-data="2054933"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19pFEs/MpMIrUDUKjPgvT1KpvBcRDzi72M="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:E+LqWDmpG+ZoIUJx8hZV8q+EPIc=
sha1:Msmg1AVUwoh/Mcfn5mHFd1HDFTQ=
 by: Tim Rentsch - Wed, 11 Oct 2023 17:17 UTC

Ian C <deathstation9000@gmail.com> writes:

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

Wouldn't you first write a test function to be sure the
value returned is correct? ;)

I found it harder to write a correct test function than
to write a function that just returns a correct value.
Interesting, eh?

Re: portable way to get highest bit set?

<20231011101856.805@kylheku.com>

  copy mid

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

  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: 864-117-...@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.c,alt.comp.lang.c
Subject: Re: portable way to get highest bit set?
Date: Wed, 11 Oct 2023 17:23:36 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 25
Message-ID: <20231011101856.805@kylheku.com>
References: <ug5gvh$1jkar$3@dont-email.me>
Injection-Date: Wed, 11 Oct 2023 17:23:36 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="70087148053750615cb184eff93c6977";
logging-data="2054018"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19yAQXzgjtOiS/hFCwZ95Lvg3xVamkrpaE="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:EZqy3CENHV2WFfkIG6E4Q/IvhZ0=
 by: Kaz Kylheku - Wed, 11 Oct 2023 17:23 UTC

On 2023-10-11, candycanearter07 <no@thanks.net> wrote:
> Hi,
>
> What is the best/most portable way to get the highest bit set?
>
> ie. 011010001
> to 010000000

I don't know about best or portable, but in the run-time of the
TXR language, I solved it like this (see highest_bit function):

https://www.kylheku.com/cgit/txr/tree/arith.c

If we have GNU C builtin primitives for this, we use them.
Otherwise do a statically coded binary search (no loop).

The build configuration system provides constants like SIZEOF_PTR.

That is not essential because you can just use regular conditionals.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca
NOTE: If you use Google Groups, I don't see you, unless you're whitelisted.

Re: portable way to get highest bit set?

<86h6mxawqq.fsf@linuxsc.com>

  copy mid

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

  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: Wed, 11 Oct 2023 10:34:53 -0700
Organization: A noiseless patient Spider
Lines: 57
Message-ID: <86h6mxawqq.fsf@linuxsc.com>
References: <ug5gvh$1jkar$3@dont-email.me> <20231011102714.44a870af4dfe68f756974953@g{oogle}mail.com> <ug6huc$1rvp1$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: dont-email.me; posting-host="c9974d524a342450c72757464b9d47a6";
logging-data="2064482"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/NUV9052bkxu46JYy3RF8X0n7hSGbXJmM="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:bTndiKAYgdgUEvQ6xp1tG3DoEXA=
sha1:gJZ9NozqCS3XLXn6A8GkNkx6Quw=
 by: Tim Rentsch - Wed, 11 Oct 2023 17:34 UTC

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
* No looping or testing
* Large domain of inputs supported
* Widely portable
* Very few cases with undefined behavior
* Forms an excellent basis for several interview questions
* Sure to launch a torrent of followups on its pros and cons

(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.

[*] that might be a baker's dozen, due to off-by-one error

[**] for some values of "favorite"

Re: portable way to get highest bit set?

<ug6nvv$1va0l$1@dont-email.me>

  copy mid

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

  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: richard....@gmail.invalid (Richard Harnden)
Newsgroups: comp.lang.c,alt.comp.lang.c
Subject: Re: portable way to get highest bit set?
Date: Wed, 11 Oct 2023 19:02:38 +0100
Organization: A noiseless patient Spider
Lines: 23
Message-ID: <ug6nvv$1va0l$1@dont-email.me>
References: <ug5gvh$1jkar$3@dont-email.me>
<20231011102714.44a870af4dfe68f756974953@g{oogle}mail.com>
<ug6huc$1rvp1$1@dont-email.me>
Reply-To: nospam.harnden@invalid.com
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 11 Oct 2023 18:02:39 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="2e4cd0c210dbf0e5f1d2ad668fc30796";
logging-data="2074645"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+2WpvUxEvOK4irymQHEaGriRMY0FimGnM="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:KlkyWuxE493QlneJ1iXhAxLMjRM=
Content-Language: en-GB
In-Reply-To: <ug6huc$1rvp1$1@dont-email.me>
 by: Richard Harnden - Wed, 11 Oct 2023 18:02 UTC

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;

Why 0x100000000 and not 0x800000000?

>
> I'm trying to find something size independent, though.
> Or at least able to be swapped out with a #define

Re: portable way to get highest bit set?

<ug70cu$219ov$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c alt.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: nos...@please.ty (jak)
Newsgroups: comp.lang.c,alt.comp.lang.c
Subject: Re: portable way to get highest bit set?
Date: Wed, 11 Oct 2023 22:26:05 +0200
Organization: A noiseless patient Spider
Lines: 26
Message-ID: <ug70cu$219ov$1@dont-email.me>
References: <ug5gvh$1jkar$3@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 11 Oct 2023 20:26:06 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="b8a5251be9bceeb62180cc8a968a80e7";
logging-data="2139935"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/rkvEBBNTB1aqK6orZNjoS"
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:OR+zDgLBA0jAfYdOMWS8VdW6kNo=
In-Reply-To: <ug5gvh$1jkar$3@dont-email.me>
 by: jak - Wed, 11 Oct 2023 20:26 UTC

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;

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?

<ug71l8$21irh$1@dont-email.me>

  copy mid

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

  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: Wed, 11 Oct 2023 22:47:35 +0200
Organization: A noiseless patient Spider
Lines: 104
Message-ID: <ug71l8$21irh$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>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 11 Oct 2023 20:47:36 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="cb70e5e7e3235bfee4a137b1d5eeb246";
logging-data="2149233"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+wyxT7yaGoqcpTFsFbbzfCSVfRFA3NZXw="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:UroYrHU54/Ows4f5Q9bQk1wM7Jc=
In-Reply-To: <86h6mxawqq.fsf@linuxsc.com>
Content-Language: en-GB
 by: David Brown - Wed, 11 Oct 2023 20:47 UTC

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.

> * 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.

Re: portable way to get highest bit set?

<pan$e0805$faa9a91a$a60476dc$4a90cfa8@invalid.invalid>

  copy mid

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

  copy link   Newsgroups: comp.lang.c alt.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,alt.comp.lang.c
Subject: Re: portable way to get highest bit set?
Date: Wed, 11 Oct 2023 21:16:48 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 26
Message-ID: <pan$e0805$faa9a91a$a60476dc$4a90cfa8@invalid.invalid>
References: <ug5gvh$1jkar$3@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 11 Oct 2023 21:16:48 -0000 (UTC)
Injection-Info: bluemanedhawk.eternal-september.org; posting-host="84e166662ab860df93def1746ab818a2";
logging-data="2142465"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19JKaPm9dI/eF2yfYlTOqZecKXwqTF8aSM="
User-Agent: Pan/0.154 (Izium; 517acf4)
Cancel-Lock: sha1:lUgRz7s45SJ+L+GpZ2Fa6/tOg70=
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 - Wed, 11 Oct 2023 21:16 UTC

candycanearter07 wrote:

> Hi,
>
> What is the best/most portable way to get the highest bit set?
>
> ie. 011010001 to 010000000

#include <stdbit.h>

unsigned char highest_bit_setc(unsigned char x)
{ return stdc_first_leading_one(x) ? (1 << stdc_first_leading_one(x)
- 1) : 0;
}

/* Repeat for all appropriate types and then probably make a generic macro
for all the subroutines. */

--
Blue-Maned_Hawk│shortens to
Hawk│/
blu.mɛin.dÊ°ak/
│he/him/his/himself/Mr.
bluemanedhawk.github.io
Minor vehicular events!

Re: portable way to get highest bit set?

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

  copy mid

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

  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 14:30:55 -0700
Organization: None to speak of
Lines: 42
Message-ID: <87h6mwu9rk.fsf@nosuchdomain.example.com>
References: <ug5gvh$1jkar$3@dont-email.me>
<pan$e0805$faa9a91a$a60476dc$4a90cfa8@invalid.invalid>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Info: dont-email.me; posting-host="fae7e8aa6d7cdf9e2610e8a92119df27";
logging-data="2158596"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+66l14gCj1Rb7STb0Kvlol"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:MbaW+EnGMV53U57JTiAKB7Ftx64=
sha1:sgfml4k3Bgqazygu7ZHNWtHGS5o=
 by: Keith Thompson - Wed, 11 Oct 2023 21:30 UTC

Blue-Maned_Hawk <bluemanedhawk@invalid.invalid> writes:
> candycanearter07 wrote:
>> What is the best/most portable way to get the highest bit set?
>>
>> ie. 011010001 to 010000000
>
> #include <stdbit.h>
>
> unsigned char highest_bit_setc(unsigned char x)
> {
> return stdc_first_leading_one(x) ? (1 << stdc_first_leading_one(x)
> - 1) : 0;
> }
>
> /* Repeat for all appropriate types and then probably make a generic macro
> for all the subroutines. */

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

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!

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

<20231011143809.748@kylheku.com>

  copy mid

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

  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: 864-117-...@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.c,alt.comp.lang.c
Subject: Re: portable way to get highest bit set?
Date: Wed, 11 Oct 2023 21:57:06 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 80
Message-ID: <20231011143809.748@kylheku.com>
References: <ug5gvh$1jkar$3@dont-email.me>
Injection-Date: Wed, 11 Oct 2023 21:57:06 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="70087148053750615cb184eff93c6977";
logging-data="2174366"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18jYjcmY69/fJUKKSnEg32xyJ9HXPR9gcc="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:44dE8nPfEN094RKIIrzk3c0QGew=
 by: Kaz Kylheku - Wed, 11 Oct 2023 21:57 UTC

On 2023-10-11, candycanearter07 <no@thanks.net> wrote:
> Hi,
>
> What is the best/most portable way to get the highest bit set?
>
> ie. 011010001
> to 010000000

I just noticed you really want the *mask* of the highest bit set,
not its position.

That's pretty easy to do in a portable way.

First we calculate this fully saturated mask:

011111111

then, we just right shift that to produce this mask:

001111111

and XOR them together to get

010000000

Now how do we get that first mask? Like ethis:

Start with x:

011010001 x

OR it with x >> 1:

| 001101000 x >> 1
------------
= 011111001

Take that to be the new x. OR it with x >> 2

011111001
| 000111110
------------
= 011111111

And here we are done; but we continue to x >> 4 and x >> 8.

E.g. 32 bit code:

uint32_t fill_mask_down(uint32_t x)
{
x |= x >> 1; // e.g. 1000...0000 -> 1100...0000
x |= x >> 2; // e.g. 1100...0000 -> 1111...0000
x |= x >> 4; // e.g. 11110000... -> 11111111...
x |= x >> 8;
x |= x >> 16;

return x;
}

Thus:

uint32_t isolate_highest_bit(uint32_t x)
{
uint32_t m = fill_mask_down(x);
return m ^ (m >> 1);
}

Note that this has no branches whatsoever. There are data hazards
because we are updating an accumulator in place; that's likely
going to cost some pipeline-level parallelism.

Note that the numbeer of |= steps in fill_mask_down is the log2
of the number of bits. The 64 bit code just ads x |= x >> 32.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca
NOTE: If you use Google Groups, I don't see you, unless you're whitelisted.

Re: portable way to get highest bit set?

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

  copy mid

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

  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: 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: Wed, 11 Oct 2023 23:58:10 +0100
Organization: A noiseless patient Spider
Lines: 31
Message-ID: <878r884vi5.fsf@bsb.me.uk>
References: <ug5gvh$1jkar$3@dont-email.me> <ug70cu$219ov$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="2201473"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+k4vAg9FWj7mV75MiWjqMNwAKa+TICY1U="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux)
Cancel-Lock: sha1:aL9KA6Eb9n0qtW0pU6lMcRDoskU=
sha1:I3Hso8mdJij8RY/8I1TCnQUcCfo=
X-BSB-Auth: 1.738cbe4adb4be978f3bd.20231011235810BST.878r884vi5.fsf@bsb.me.uk
 by: Ben Bacarisse - Wed, 11 Oct 2023 22:58 UTC

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

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

--
Ben.

Pages:12345678
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor