Home >Java >javaTutorial >What's the Difference Between `>>` and `>>>` Operators in Java?

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

Barbara Streisand
Barbara StreisandOriginal
2025-01-01 11:45:11718browse

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.

The above is the detailed content of What's the Difference Between `>>` and `>>>` Operators in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn