Content received from: http://JavaFAQ.nu/java-article68.html


Q: Why do we need to use shift operator in Java? Can you give some examples?
Friday, February 28, 2003 (00:00:00)

Posted by jalex

Q: Why do we need to use shift operator in Java? Can you give some examples?

Answer: Using shift operators in Java we can:

  • 1. Make faster integer division/multiplication operations:

    4839534 * 4
    can be done like this:
    4839534 << 2

    or

    543894 / 2
    can be done like this:
    543894 >> 1

    Shift operations much more faster than multiplication for most of processors.
  • 2. Reassembling byte streams to int values
  • 3. For accelerating operations with graphics since Red, Green and Blue colors coded by separate bytes.
  • 4. Packing small numbers into one single long...