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

Sandeep Kumar Singh Sandeep.Singh2@kpit.com
Fri Jun 13 13:47:00 GMT 2014


Hi,

I need some help/clarification,
I am expecting zero value of 16-bit signed value 0x8000 after left shift by 2
Below is my test case.

Test case:
========
int main()
{
	register signed short val1 = 0x8000 ;
	signed short val2 ;
	val2 = ( ( val1 << 2 ) >> 2 ) ;              /* val2 should be 0. */
	printf( "\nval2 is :%x\n", val2 ) ;    /* But Val2 is ffff8000 */
	return 0 ;
}

Steps to reproduce:
$gcc   test.c
$run   a.out

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

Best Regards,
Sandeep Kumar Singh




More information about the Gcc-help mailing list