Evaluate 16-bit signed value 0x8000 after left shift by 2
Åke Forslund
aforslund@lasermaxroll.se
Fri Jun 13 15:23:00 GMT 2014
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?
> 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.
>
The result is still 0x8000 and not 0 as the original author expects.
/Åke
More information about the Gcc-help
mailing list