• 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

shiftoperators

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

wht is the diff b/w shift operators <<,>>,>>> operators?
I know tht 2<<2 i.e 2*2power2
2>>2 i.e 2/2power 2
2>>>2 wht it gives


Regards
Suhita
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/bitwise.html
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually that may not make the distinction clear. When using the >> it will perform an arithmetic shift, that is to say the left bits will be filled with sign bit. When using >>> it will perform a logical shift and the left bits will be filled with zeros. The distinction becomes important when dealing with numbers that are signed, in this case specifically negative numbers.

Take a look at this example:



You'll notice that with 'a' there is no difference because it is positive and both fill the left side with zeros. With 'b', however, the distinction becomes very noticeable because rather than filling the left side with the sign bit it fills it with zeros. With both >> and >>> a will become 3 whereas with b the >> will make it -4 and >>> will make it 1073741820. Big difference.
[ July 06, 2006: Message edited by: Ken Blair ]
 
Ranch Hand
Posts: 1252
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ken Blair:
Actually that may not make the distinction clear. When using the >> it will perform an arithmetic shift, that is to say the left bits will be filled with sign bit. When using >>> it will perform a logical shift and the left bits will be filled with zeros. The distinction becomes important when dealing with numbers that are signed, in this case specifically negative numbers.

Take a look at this example:



You'll notice that with 'a' there is no difference because it is positive and both fill the left side with zeros. With 'b', however, the distinction becomes very noticeable because rather than filling the left side with the sign bit it fills it with zeros. With both >> and >>> a will become 3 whereas with b the >> will make it -4 and >>> will make it 1073741820. Big difference.

[ July 06, 2006: Message edited by: Ken Blair ]



Additional Input in Ken's Message is if you really want to get through hand on in Bitwise Operators. This will give you a more clear understanding.

Bitwise Operators.....

Hope it helps you.

 
reply
    Bookmark Topic Watch Topic
  • New Topic