SoFunction
Updated on 2025-04-08

One article allows you to thoroughly learn Java left and right movement operations

In Java, move left (<<) and right (>>) is a bit operator used to shift binary numbers. The shift operation directly operates the bit mode of the number.

1. Left shift operator (<<)

Function:

  • The left shift operation will move the binary bit of a number to the left by the specified number of bits, and use it on the right.0Fill.
  • Each move one to the left is equivalent to multiplying the number by 2 (but not in the strict sense multiplication, which may cause overflow).

grammar:

result = value << n;
  • valueIt is the number to be operated.
  • nis the number of bits that are moved.

Features:

  • Moving left will expand the value of the number (if there is no overflow).
  • Add 0 to the right.
  • If it is a negative number, the two's calculation will be participated in the complement form.

Example:

int a = 5; // Binary: 0000000000000000000000000000000000101int result = a &lt;&lt; 2; // 2 bits left: 0000000000000000000000000000000000000000000000000000000000000 (20)(result); // Output: 20

2. Right shift operator (>>)

Function:

  • The right moves the binary bit of a number to the right by the specified number of bits.
  • Sign bit expansion: If it is a positive number, the high position is filled with 0; if it is a negative number, the high position is filled with 1 (that is, the sign bit remains unchanged).
  • Each move one right is equivalent to dividing the number by 2 (rounded down).

grammar:

result = value >> n;
  • valueIt is the number to be operated.
  • nis the number of bits that are moved.

Features:

  • For positive numbers, the high position is filled with 0 after shifting right.
  • For negative numbers, the high bit is filled with 1 after shifting right (sign bit expansion).
  • Suitable for handling signed numbers.

Example:

int a = 20; // Binary: 000000000000000000000000000000000000000000000000000010100int result = a &gt;&gt; 2; // Move right 2 bits: 0000000000000000000000000000000000000101 (5)(result); // Output: 5
int b = -20; // Binary: 111111111111111111111111111111111111111111111111111101100 (complement code representation)int result2 = b &gt;&gt; 2; // Right shift 2 bits: 1111111111111111111111111111111111111111111111111111111111 (-5)(result2); // Output: -5

3. Unsigned right-shift operator (>>>)

Apart from>>, Java also providesUnsigned right-shift operator>>>

  • Whether positive or negative, use high0Fill.
  • Usually used to manipulate unsigned data.

Example:

int a = -20; // Binary: 111111111111111111111111111111111111111111111111111101100 (complement code representation)int result = a &gt;&gt;&gt; 2; // Unsigned right shift by 2 bits: 001111111111111111111111111111111111111111111111111111111111111111111111111111111111111111(result); // Output: 1073741819

Summarize

Operator name Function
<< Move left The binary moves to the left and the right side adds 0, which is equivalent to multiplying by 2 to the n power (if it does not overflow).
>> Symbols move right The binary moves to the right, retains the sign bit, and complements the sign bit with the high (0 for positive numbers and 1 for negative numbers), which is equivalent to dividing by 2 to the n power.
>>> Unsigned right The binary moves to the right, and the high bit is always supplemented with 0, regardless of the sign bit.

Extension: i <<= j

  • In Java,i <<= jis a composite operator, which is equivalent toi = i << j, indicating that it williThe binary bit of  moves to the leftjbits, and then assign the result toi. Coming sooniThe left shift j bit is equivalent toiMultiply by2ofjTo the power.

Computation process

  • iThe binary form will move to the leftjBit.
  • After shifting, use the low position0In addition, high bits may overflow (parts beyond the current data type range will be discarded).
  • The final result is stored back to the variablei

Example

Example 1: Move the positive number left

public class Main {
    public static void main(String[] args) {
        int i = 5; // Binary: 0000000000000000000000000000000000101        int j = 2;

        i &lt;&lt;= j; // Equivalent to i = i << j        // 2 bits left: 000000000000000000 000000000000 00010100 (20)
        (i); // Output: 20    }
}

Example 2: Move the negative number left

public class Main {
    public static void main(String[] args) {
        int i = -5; // Two's (complement code): 1111111111111111111111111111111111111111111111111111111111111111111111111        int j = 2;

        i &lt;&lt;= j; // Equivalent to i = i << j        // left shift 2 bits: 1111111111111111111111111111111111111111111111111111101100 (-20)
        (i); // Output: -20    }
}

Example 3: Possible overflow issues

public class Main {
    public static void main(String[] args) {
        int i = 1073741824; // Binary: 0100000000000000000000000000000000        int j = 1;

        i &lt;&lt;= j; // left shift 1 bit: 1000000000000000000000000000000000000000        // More than 32 bits, the result becomes negative (overflow)        
        (i); // Output: -2147483648    }
}

Note

  • The left shift is equivalent to a power of multiplied by two

    • Each shift of 1 bit to the left is equivalent to multiplying by2
    • For example:5 <<= 1Equivalent to5 * 2 = 105 <<= 2Equivalent to5 * 4 = 20
    • Note that shifting left will not check for overflow issues, which may lead to negative or incorrect results.
  • Overflow problem

    • Shifting can cause high bits to be truncated, especially when the operand is close to the maximum value of the data type.
  • Applicable data types

    • The left-shift operation supports integer and long integer (intandlong)。
    • If the type of operation is smaller thanint(for examplebyteorshort) will be promoted tointThen perform shift operation.

Summarize

This is the article about Java left and right movement operations. For more information about Java left and right movement operations, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!