Arun Prasath

Ranch Hand

Posts: 60

posted 11 years ago

How the 2's copmliment is performed while doing signed left shifting and

and arithmatic operations when number is in negative.?

say for example,

(in binary,)

int x=-1;

so x=1000 0000 0000 0000 0000 0000 0000 0001; (this is in binary, for

understanding,i know binary cannot be input)

x=x>>30;

here x is printed as -1. why and how is this?

and arithmatic operations when number is in negative.?

say for example,

(in binary,)

int x=-1;

so x=1000 0000 0000 0000 0000 0000 0000 0001; (this is in binary, for

understanding,i know binary cannot be input)

x=x>>30;

here x is printed as -1. why and how is this?

posted 11 years ago

Well, -1 is not 1000 ... 0001, it is all 1's.

So you have x = 1111 1111 1111 1111 1111 1111 1111 1111 (binary) and you shift right with sign extension - all the bits will still be 1's, and the result is still -1.

The pattern 1000 ... 0001 is -2147483647, not -1.

Two's complement

[ July 31, 2006: Message edited by: Jesper Young ]

So you have x = 1111 1111 1111 1111 1111 1111 1111 1111 (binary) and you shift right with sign extension - all the bits will still be 1's, and the result is still -1.

The pattern 1000 ... 0001 is -2147483647, not -1.

Two's complement

[ July 31, 2006: Message edited by: Jesper Young ]