• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

confused by shift operators

 
SaiPrasad Jukalkar
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi ,
i am preparing for scjp exam, i am very much confused with the shift operators,
one of my questions is how to arrive at the binary representation of an negative number.
and example of >>> shift of negative numbers,
i have searched through the javaranch for shift operators, even though i found some good examples, but still am not clear about the concept, i appreciate any help.
thanks in advance.
 
Junilu Lacar
Bartender
Posts: 7606
54
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Google for twos complement
Or search this fantastic forum
Junilu
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Janilu says, search this forum - there's more information on bit operations and bit shifts and two's complement than you can shake a stick at, ya li'l doggie!
Here's a quick answer:
To get a negative version of a number, take the positive version, flip the bits, and add 1.

The >>> operator (unsigned right shift) shifts all bits to the right and fills the left with 0's.
Corey
 
Rodney Woodruff
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this link. It helped me alot.
http://www.javaranch.com/campfire/StoryBits.jsp
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic