• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

shift operators

 
Preetha Vasudevan
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can anyone explain me how exactly the shift operations << ,>> and >>> work!!
 
Akhilesh Trivedi
Ranch Hand
Posts: 1599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Preetha!
Check out numbering over here.

http://www.coderanch.com/t/250859/java-programmer-SCJP/certification/Bitwise-Complement-Operator

Then come to shifts,

a << 3

Actually means shift/remove left most 3 bits and add three zeros in the other end.
For ex.
if a = 0000 0101 1101 0011 0000 0000 0000 1101 then
a<<3 would be
0010 1110 1001 1000 0000 00 0001 1010

similarly,
>>4 means remove right most 4 bits and add four sign bit yes sign-bit in the other end. (So +ve numbers will remain +ve and -ve will remain -ve)

>>> is called zero-filled shift. Same as above but fills zeros instead of sign bits. i.e. result will always be +ve. Hope this helps.

[ October 04, 2005: Message edited by: Akhil Trivedi ]
[ October 04, 2005: Message edited by: Akhil Trivedi ]
 
Preetha Vasudevan
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Akhil
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
remember everyone - this topic is on 1.4 but not tiger...
 
Richard Rex
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Akhil,

How about those right operands that are negative numbers. How is this done?

Ex.
a. 8 << -3 = 0
b. 8 >> -3 = 0
c. 8 >>> -3 = 0

Can you explain what is being done for negative numbers on the right operand? Can we move bit negatively?
 
Ramya Iyer
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please look at

http://www.coderanch.com/t/250975/java-programmer-SCJP/certification/Shift-Operators

The above thread provides the explanation if right operand is negative.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic