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 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.

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.

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