# Right shift question

Nikhil Bansal

Ranch Hand

Posts: 60

posted 10 years ago

Hi Ranchers,

A very simple ques .

What should be the value of i

i = -1 >> 2;

I think -1 = 10000000 00000000 00000000 00000001 in binary.

Then by right shift it should be 11100000 00000000 00000000 00000000 which is equal to 3758096384 in decimal.

When I run the program, then I get i as -1.

Please help

Regards

Nikhil Bansal

A very simple ques .

What should be the value of i

i = -1 >> 2;

I think -1 = 10000000 00000000 00000000 00000001 in binary.

Then by right shift it should be 11100000 00000000 00000000 00000000 which is equal to 3758096384 in decimal.

When I run the program, then I get i as -1.

Please help

Regards

Nikhil Bansal

ban$al

wise owen

Ranch Hand

Posts: 2023

Praveen Babu

Ranch Hand

Posts: 138

posted 10 years ago

The negative numbers are represented in 2's compliment notation

2's compliment = flip all digits + 1

so,

1 = 00000000 00000000 00000000 00000001

and, -1 = flip all digits + 1

that is,

11111111 11111111 11111111 11111110

00000000 00000000 00000000 00000001

____________________________________

11111111 11111111 11111111 11111111

and hence, the number will remain the same if you right shift it by any number.

[ November 01, 2006: Message edited by: Praveen Babu ]

2's compliment = flip all digits + 1

so,

1 = 00000000 00000000 00000000 00000001

and, -1 = flip all digits + 1

that is,

11111111 11111111 11111111 11111110

00000000 00000000 00000000 00000001

____________________________________

11111111 11111111 11111111 11111111

and hence, the number will remain the same if you right shift it by any number.

[ November 01, 2006: Message edited by: Praveen Babu ]