int
, long
, short
, byte
and char
) but not with floating-point or boolean values.
int a = 5;
System.out.println(a << 1); // Output: 10 (binary 101 << 1 becomes 1010)
int a = 20;
System.out.println(a >> 2); // Output: 5 (binary 10100 >> 2 becomes 101)
int a = -20;
System.out.println(a >>> 2); // Output: 1073741815 (binary of -20 shifted right with zero-fill)
public class ShiftOperators
{
public static void main(String[] args)
{
// Initialize variables
int a = 5; // Binary: 0101
int b = -10; // Binary: 11111111111111111111111111110110 (in 32-bit two's complement)
// Left shift (<<)
System.out.println("Left Shift (a << 1): " + (a << 1)); // Output: 10 (Binary: 1010)
// Right shift (>>)
System.out.println("Right Shift (a >> 1): " + (a >> 1)); // Output: 2 (Binary: 0010)
// Unsigned right shift (>>>)
System.out.println("Unsigned Right Shift (a >>> 1): " + (a >>> 1)); // Output: 2 (Binary: 0010)
// Left shift with negative number
System.out.println("Left Shift (b << 1): " + (b << 1)); // Output: -20 (Binary: 11111111111111111111111111101100)
// Right shift with negative number
System.out.println("Right Shift (b >> 1): " + (b >> 1)); // Output: -5 (Binary: 11111111111111111111111111111011)
// Unsigned right shift with negative number
System.out.println("Unsigned Right Shift (b >>> 1): " + (b >>> 1)); // Output: 2147483643 (Binary: 01111111111111111111111111111011)
}
}
Left Shift (a << 1): 10 Right Shift (a >> 1): 2 Unsigned Right Shift (a >>> 1): 2 Left Shift (b << 1): -20 Right Shift (b >> 1): -5 Unsigned Right Shift (b >>> 1): 2147483643
System.out.println(4 << 2); // Output: 16 (Binary: 0100 << 2 positions results in 10000)
System.out.println(8 >> 1); // Output: 4 (Binary: 1000 >> 1 position results in 0100)
byte
, short
, int
, long
and char
.
System.out.println(5.5 << 1); // Compilation error: incompatible types: possible lossy conversion from double to int
<<
) for Multiplication:
<<
) is used for efficient multiplication by powers of two.
int x = 3;
System.out.println(x << 2); // Output: 12 (Equivalent to 3 * 2^2)
x * 4
).
>>
) for Division:
>>
) is used for efficient division by powers of two.
int x = 16;
System.out.println(x >> 2); // Output: 4 (Equivalent to 16 / 2^2)
x / 4
).
>>>
) and Negative Numbers:
>>>
) behaves differently for negative numbers by filling with zeros instead of extending the sign bit.
int x = -8;
System.out.println(x >>> 1); // Output: 2147483640 (In binary: 11111111111111111111111111111000 >> 1 results in 01111111111111111111111111111100)
>>
).
<<
) of a negative number behaves the same as for positive numbers but can result in sign changes if the leftmost bit is shifted into the sign bit.
int x = -5;
System.out.println(x << 1); // Output: -10 (Binary: 11111111111111111111111111111011 << 1 results in 11111111111111111111111111110110)
>>>
) with Negative Numbers:
>>>
) on negative numbers replaces the sign bit with 0, making it useful for operations like bit masks or extracting specific parts of a number.
int x = -4;
System.out.println(x >>> 1); // Output: 2147483643 (Binary: 11111111111111111111111111111100 >>> 1 results in 01111111111111111111111111111110)
long
):
& 0x3F
(for long shift) to avoid shifting more than 63 positions (for 64-bit long).
long x = 100L;
System.out.println(x << 3); // Output: 800 (Equivalent to 100 * 2^3)
>>
and >>>
) will throw a compile-time error.
int x = 8;
System.out.println(x >> -2); // Compile-time error: shift count must be non-negative
int x = Integer.MAX_VALUE;
System.out.println(x << 1); // Output: -2 (Overflow: 2147483647 << 1 results in a negative value)
Your feedback helps us grow! If there's anything we can fix or improve, please let us know.
We’re here to make our tutorials better based on your thoughts and suggestions.