Shift Operator in Java (original) (raw)

Last Updated : 22 Jan, 2026

Shift operators in Java are bitwise operators that shift the binary bits of a number left or right. They work directly on binary data and are commonly used for fast arithmetic operations and low-level bit manipulation.

Shift Operators in Java

Java provides the following three shift operators:

1. Signed Left Shift Operator (<<)

The left shift operator shifts the bits of a number to the left by a specified number of positions. Zeros are added to the right side.

Syntax:

left_operand << number

Java `

class GFG { public static void main(String[] args) { byte a = 64, b; int i; i = a << 2; b = (byte) (a << 2); System.out.println("Original value of a: " + a); System.out.println("i and b: " + i + " " + b); } }

`

Output

Original value of a: 64 i and b: 256 0

**Explanation:

2. Signed Right Shift Operator (>>)

The right shift operator shifts bits to the right. The sign bit (MSB) is copied to fill vacant positions, preserving the number’s sign.

Syntax:

left_operand >> number

Java `

class GFG { public static void main(String[] args) { int number = 8;

    int ans = number >> 2;
    System.out.println(ans);
}

}

`

**Explanation:

class GFG { public static void main(String[] args) { char hex[] = { '0','1','2','3','4','5','6','7', '8','9','a','b','c','d','e','f' }; byte b = (byte) 0xf1; System.out.println("b = 0x" + hex[(b >> 4) & 0x0f] + hex[b & 0x0f]); } }

`

**Explanation:

3. Unsigned Right Shift Operator (>>>)

The unsigned right shift operator shifts bits to the right and fills the leftmost bits with 0, regardless of the sign.

Syntax:

left_operand >>> number

Java `

class GFG { public static void main(String[] args) { byte num1 = 8; byte num2 = -8; System.out.println(num1 >>> 2); System.out.println(num2 >>> 2); } }

`

**Explanation:

4. Unsigned Left Shift Operator in Java

Java does not provide an unsigned left shift operator (<<<) because: