This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Evaluate 16-bit signed value 0x8000 after left shift by 2
- From: Andrew Haley <aph at redhat dot com>
- To: Ãke Forslund <aforslund at lasermaxroll dot se>, "gcc-help at gcc dot gnu dot org" <gcc-help at gcc dot gnu dot org>
- Date: Fri, 13 Jun 2014 16:23:15 +0100
- Subject: Re: Evaluate 16-bit signed value 0x8000 after left shift by 2
- Authentication-results: sourceware.org; auth=none
- References: <b22d5b5f7a8a46bcb436966230f76e64 at HKXPR03MB166 dot apcprd03 dot prod dot outlook dot com> <539B0BC5 dot 40106 at redhat dot com> <E993883FF752B04C97E12BAA9E4DC824714B6719 at SEMAIL2 dot rollsys dot com>
On 06/13/2014 03:57 PM, Ãke Forslund wrote:
> On fre, 2014-06-13 at 15:33 +0100, Andrew Haley wrote:
>> On 06/13/2014 07:42 AM, Sandeep Kumar Singh wrote:
>>
>>> I need some help/clarification,
>>> I am expecting zero value of 16-bit signed value 0x8000 after left shift by 2
>>
>> It's undefined.
>
> A small side question is implemention defined the same as implemention
> defined in this case?
No, it's undefined. Anything might happen.
>> ISO/IEC 9899:1999 (E)
>>
>> 6.5 Expressions
>>
>> 5 If an exceptional condition occurs during the evaluation of an
>> expression (that is, if the result is not mathematically defined or
>> not in the range of representable values for its type), the behavior
>> is undefined.
>>
>
>>> I have verified the same behavior with 32-bit compilers and expected (val2=0)
>>> with 16-bit compiler. I can correct this by shifting 18 in place of 2 as a
>>> workaround. I want some verify my understanding for this point and another
>>> way to get expected result (if any).
>>
>> Use an unsigned type.
>
>> 6.2.5 Types
>>
>> 9 ... A computation involving unsigned operands can never overflow, because a
>> result that cannot be represented by the resulting unsigned integer
>> type is reduced modulo the number that is one greater than the largest
>> value that can be represented by the resulting type.
>>
> The result is still 0x8000 and not 0 as the original author expects.
It could be 666. Demons may fly out of your nose.
http://catb.org/jargon/html/N/nasal-demons.html
Andrew.