• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Java Shift Operators

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can any one tell me where are the shift operators, left, right and unsigned, are used. I need a real time example/scenario where it should be used.
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One of the use case can be for Encryption and Decryption of data in binary format
 
Marshal
Posts: 64668
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Caclulating a 32‑bit hash code for a 64‑bit number.In that instance, because of the (int) cast, it makes little difference whether you use >> or >>>. Remember 0x20 = 32.
 
Campbell Ritchie
Marshal
Posts: 64668
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any case where multipyling or dividing integers by 2 where n is any integer smaller than the number of bits in the data type.
 
author
Posts: 23834
140
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Any case where multipyling or dividing integers by 2 where n is any integer smaller than the number of bits in the data type.



Of course, the followup question is "if shifting can be done via multiplying or dividing, then in what cases should you use shifting and in what cases should you use multiplying and dividing?".

My response is that certain things make sense, while others don't in this regard. It doesn't make sense to use shifting when the algorithm is calling for multiplying and dividing -- even if you can argue that they are functionally equivalent. Just because one can replace the other doesn't mean that it should be done -- and quite frankly, it should *not* be done as it will confuse people. After all, what happens when the "multiply by four" is to be changed to "multiply by five" because a fifth component was added?


Anyway, to answer the original question, I needed lots of shifting with networking programs especially those that need to set or get particular bits of headers. I also needed to use shifting a lot with graphic programs.

Henry
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!