"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > What is the difference between `>`` and `>>>` operators in Java?

What is the difference between `>`` and `>>>` operators in Java?

Posted on 2025-04-12
Browse:585

What's the Difference Between `>>` and `>>>` Operators in Java?
>` and `>>>` Operators in Java? " />

Understanding the Distinction: >>> vs >> Operators in Java

The >> and >>> operators in Java play distinct roles in performing shift operations on primitive data types.

The Difference:

  • Arithmetic Shift Right (>>):

    • Preserves the sign of the number being shifted by extending the sign bit to the right.
  • Logical Shift Right (>>>):

    • Ignores the sign bit and fills in the vacated bits on the left with zeroes.

Example:

Consider the 8-bit representation of -2 (11111110, with the most significant bit signifying a negative value).

  • Arithmetic Shift (>> 1):

    • Extends the sign bit, resulting in 11111111 (-1)
  • Logical Shift (>>> 1):

    • Ignores the sign bit and fills in 0s, resulting in 01111111 (positive value)

Key Distinction:

The arithmetic shift retains the signedness of the number, while the logical shift treats it simply as an unsigned binary number. This distinction becomes important when dealing with negative values.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3