• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

shift operators

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can anyone explain me how exactly the shift operations << ,>> and >>> work!!
 
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Preetha!
Check out numbering over here.

https://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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Akhil
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
remember everyone - this topic is on 1.4 but not tiger...
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please look at

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

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