This week's book giveaway is in the OCAJP forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Shift operators Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA Java SE 8 Programmer I Study Guide 1Z0-808 this week in the OCAJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Shift operators" Watch "Shift operators" New topic
Author

Shift operators

vamsi acharya
Ranch Hand

Joined: Jun 28, 2009
Posts: 55

Hello guys one question has been hovering me from the beginning of my java learning career .... why do we use shift operators i have been referring to many books and googled it online still not sure about the usage and pondering about its working ....can anybody please elucidate me ???


"Known is a drop , unknown is an ocean " !
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31421
    
184

Vamsi,
In practice, most people don't use the shift operators. Maybe if you were writing a low level program with bytes and needed to shuffle them.


[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19235
    
  41


Obvious uses are...

1. Graphics. Shifting is needed to create bit patterns -- or to create masks that can be used to set (OR) or clear (AND) dots.

2. Networking. Shifting is need to create network masks. The masks needed to set or clear bits specified in a protocol.

3. Security. Encryption needs this a lot.

Less obvious use... Some mathematical operations are faster with shifting. With modern processors, this is not true anymore, as they are loaded with instructions that can do practically anything needed by math.


Now, all of this may be moot, as Java has libraries that does all of this for you. In fact, most new developers rarely, if ever, encounter a need to use the shifting (or bitwise) operators.... which is a shame.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
vamsi acharya
Ranch Hand

Joined: Jun 28, 2009
Posts: 55

oh thank you henry......This info is very much needed to me
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40486
    
  28
What about calculating hash codes for longs and doubles?
 
Consider Paul's rocket mass heater.
 
subject: Shift operators