This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Evaluate 16-bit signed value 0x8000 after left shift by 2


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.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]