Page 1 of 1

PostPosted: Tue Aug 14, 2007 1:29 am
by rongviet
Look like the bitwise shift right operator returns a wrong value.

main(){
int n=-75968486;
n = n>>5;
puts("n="+n);
}

The program output is -2374016. Should it be 131843712 ?
Could someone verify the answer. I am running with Palm PocketC 7.1.3.

Thanks.
DP

PostPosted: Tue Aug 14, 2007 3:02 am
by dewey
Since an int is a signed value (PocketC doesn't support unsigned types), a right shift will always use sign extension. Thus, the output will always be negative if the value starts out negative.

PostPosted: Tue Aug 14, 2007 10:03 am
by rongviet
Thank you for response quickly. You are right, shift right a negative number should be a negative number. To get the answer as in the unsigned number, I need to AND with 0x07FFFFFF.

e.g., n = n>>5 & 0x07ffffff;

Thanks again.
DP