bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Arithmetic expression: interest in unsigned right shift?


From: Chet Ramey
Subject: Re: Arithmetic expression: interest in unsigned right shift?
Date: Sun, 17 Jul 2022 17:21:22 -0400
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:91.0) Gecko/20100101 Thunderbird/91.10.0

On 7/17/22 12:13 AM, Martin D Kealey wrote:
Printf %u already reveals  the word size, as do most kinds of overflow -
albeit messily, like $((3**40))

At least by explicitly exposing the word size in a controlled manner, we
can write code that avoids unintended overflow.

This is not a problem in practice. If you want to know how big an intmax_t
is, compute it. It's always going to be 32 or 64 bits. If you potentially
use a machine where sizeof(intmax_t) == 16, augment the tests.

INTMAX_BITS=64
(( (2**31) < 0 )) && INTMAX_BITS=32

or, if you like,

INTMAX_BITS=32
(( 2**31 > 0 )) && INTMAX_BITS=64

or use (( 2**63 < 0 )), or (( 2**64 == 0 )), or (( 2**32 == 0 )), and so
on.

Then you can compute INTMAX_MAX, should you need it.

--
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    chet@case.edu    http://tiswww.cwru.edu/~chet/



reply via email to

[Prev in Thread] Current Thread [Next in Thread]