Path: csiph.com!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: Tim Rentsch
Newsgroups: comp.lang.c
Subject: Re: C wish list
Date: Thu, 21 Mar 2019 22:55:29 -0700
Organization: A noiseless patient Spider
Lines: 28
Message-ID: <86sgvfo3la.fsf@mailhub.linuxsc.com>
References: <11b51bdf-7847-40fc-bdf5-b3777f41b4da@googlegroups.com> <18RkE.296542$DH4.255445@fx24.am4>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: reader02.eternal-september.org; posting-host="12002fc6e8015032ba78a259e24530e4"; logging-data="9771"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18Q9c+cYq5r8OTLldM1trV8BZ+pl4MkwAE="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:gq0lRhh4AniL/Daw8fkn+mCH58E= sha1:TVHFPDYEzpaxIoUSioSF1gR7lCA=
Xref: csiph.com comp.lang.c:142223
Keith Thompson writes:
> David Brown writes:
> [...]
>
>> C has a " << " operator. It does not have left-shift instructions. You
>> appear to be mixing up C and assembly again.
>>
>> x << y has the result x * 2^y, reduced by modulo if x is unsigned. If
>> the value of the result can't fit in the type - which is the case for
>> signed 1 << 31 - then the result is undefined. C prefers to say it
>> doesn't make sense and the language does not allow it, rather than pick
>> an incorrect and meaningless value for it (regardless of any underlying
>> implementation).
>>
>> So Auntie Spam here is correct. It is not (AFAIK) the main reason why 1
>> << 31 is not defined (for 32-bit int), but it is a convenient bonus.
>
> 1 << 31 is undefined (assuming 32-bit int) for two reasons.
>
> 6.5.7p3: It's undefined because the right operand is greater than
> or equal to the width of the promoted right operand. (0 << 31 is
> undefined for the same reason.)
This part is wrong. The width of a 32-bit int is 32, not 31.
The /precision/ of a 32-bit int is 31, but it's the width that
matters for the constraints on shifting. The expression 0 << 31
is well defined for 32-bit int's.