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

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

 



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 of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux