File APIs for Java Developers Manipulate DOC, XLS, PPT, PDF and many others from your application. http://aspose.com/file-tools
Big Moose Saloon
 Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies Register / Login Win a copy of Head First Android this week in the Android forum!

# Right shift question

Nikhil Bansal
Ranch Hand

Joined: Jan 24, 2005
Posts: 60
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.

Regards

Nikhil Bansal

ban\$al
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023

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

-1 = 11111111 11111111 11111111 11111111
Praveen Babu
Ranch Hand

Joined: Jul 30, 2006
Posts: 138
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 ]
Andy Morris
Ranch Hand

Joined: May 30, 2004
Posts: 78
Bit shifting is not in the 1.5 exam in case you didn't know.

I agree. Here's the link: http://aspose.com/file-tools

subject: Right shift question