aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Right shift question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Right shift question" Watch "Right shift question" New topic
Author

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.

Please help

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.
 
wood burning stoves
 
subject: Right shift question