• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Shift operators

 
Ranch Hand
Posts: 55
Android Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ???
 
author & internet detective
Posts: 41250
849
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
vamsi acharya
Ranch Hand
Posts: 55
Android Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh thank you henry......This info is very much needed to me
 
Marshal
Posts: 76450
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What about calculating hash codes for longs and doubles?
 
These are the worst of times and these are the best of times. And this is the best tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic