January 6th, 2003, 05:40 AM
Here is a some code i executed , its giving some what strange values , right shift means dividing the value by 2 . On this basis i have excuted the code.
The code follows
char ch = -0x7f ;
int i = ch;
printf("%d %d %d %d\n",ch,ch>>1,-127/2,i/2);
Out put is :
-127 -64 -63 -63
Here -127/2 and i/2 are giving -63 as a result
But ch>>1 is giving -64 as a result . How it is possible ?
Please explain me.
January 6th, 2003, 01:49 PM
-0x7f is 1111111110000001 in binary (assuming a 16 bit variable). Right shifting this by one bit gives us 1111111110000000 which translates to 64 in decimal. Now -127/2 is -63.5 actually. However, when C performs integer divisions (i.e. both numerator and denominator are integers), it always truncates the value. Hence 63.5 will be truncated to 63.