The shift operators (<< and >> ) shift the bits of a number to left or right , which resulting in a new number.We use Shift operators only on integral numbers and not on floating point.
The Right Shift Operator (>>) is used to divide a number in the multiples of 2 , while the Left Shift Operator (<<) is used to multiply a number in the multiples of 2.
Right Shift Operator >>
Let us understand the use of right shift operator with example as below :
int a=16;
a=a>>3;
Here , value 16 , gets divided by 2 to the power of number specified after the operator.Here , we have 3 as the value after the right shift operator.So , 16 will be divided by the value 2 to the power of 3, which is 8.
The result , will be 2 .
When we represent 16 in binary form , we will get following binary value :
1 0 0 0 0
when we apply right shift operator , the bit represented by 1 moves by 3 positions to the right.
After shifting binary digit 1 , we get as :
0 0 0 1 0
that is a=2.
Example:
class RightShiftDemo { public static void main(String[] args) { int a=16; System.out.println("Original Value : "+a); a=a>>3; System.out.println("After >> , new Value is : "+a); } } Output : Original Value : 16 After >> , new Value is : 2
Left Shift Operator << In Java
Lets understand example :
int x=100;
x=x<<3;
When we apply Left Shift operator , value get multiplied by 2 to the power of number specified after left shift operator.In above example , we have 3 as the value after left shift operator .So, 100 will be multiplied by the value 2 to the power 3 which is 8.
The result is 800.
Example :
class LeftShiftDemo { public static void main(String[] args) { int x=100; System.out.println("Original Value : "+x); x=x<<3; System.out.println("After << , new Value is : "+x); } } Output : Original Value : 100 After << , new Value is : 800
left shift (<<), right shift (>>), and unsigned right shift (>>>). Let's explore each of them with examples:
- Left Shift (
<<
):
The left shift operator shifts the bits of a number to the left by a specified number of positions. It effectively multiplies the number by 2 raised to the power of the shift amount. Here's an example:
int number = 5; // Binary: 00000101 int shifted = number << 2; // Shift left by 2 positions System.out.println(shifted); // Output: 20
Explanation:
The binary representation of the number 5
is 00000101
. When we perform a left shift by 2 positions (number << 2
), the bits are shifted to the left, and the result is 00010100
, which is equal to 20
in decimal. It's equivalent to multiplying 5
by 2^2
.
- Right Shift (
>>
):
The right shift operator shifts the bits of a number to the right by a specified number of positions. It effectively divides the number by 2 raised to the power of the shift amount. Here's an example:
int number = 20; // Binary: 00010100 int shifted = number >> 2; // Shift right by 2 positions System.out.println(shifted); // Output: 5
Explanation:
The binary representation of the number 20
is 00010100
. When we perform a right shift by 2 positions (number >> 2
), the bits are shifted to the right, and the result is 00000101
, which is equal to 5
in decimal. It's equivalent to dividing 20
by 2^2
.
- Unsigned Right Shift (
>>>
):
The unsigned right shift operator is similar to the right shift operator, but it fills the leftmost positions with zeros, regardless of the sign bit. It's used for shifting unsigned values. Here's an example:
int number = -20; // Binary: 11111111111111111111111111101100 int shifted = number >>> 2; // Unsigned right shift by 2 positions System.out.println(shifted); // Output: 1073741829
Explanation:
The binary representation of the number -20
is 11111111111111111111111111101100
. When we perform an unsigned right shift by 2 positions (number >>> 2
), the bits are shifted to the right, and the result is 00111111111111111111111111111011
, which is equal to 1073741829
in decimal. The leftmost positions are filled with zeros, regardless of the sign bit.
The shift operators are commonly used in low-level bit manipulation, optimization techniques, and working with binary data. They offer a way to efficiently perform multiplication or division by powers of 2, as well as bitwise operations.
Here are a few more examples of Java programs that demonstrate the use of shift operators:
- Left Shift (
<<
) Example:
public class LeftShiftExample { public static void main(String[] args) { int number = 5; // Binary: 00000101 int shifted = number << 2; // Shift left by 2 positions System.out.println(shifted); // Output: 20 } }
Output:
20
Explanation:
The program defines a variable number
with a value of 5
. Using the left shift operator (<<
), the value of number
is shifted two positions to the left. The result is 20
, which is printed to the console.
- Right Shift (
>>
) Example:
public class RightShiftExample { public static void main(String[] args) { int number = 20; // Binary: 00010100 int shifted = number >> 2; // Shift right by 2 positions System.out.println(shifted); // Output: 5 } }
Output:
5
Explanation:
The program defines a variable number
with a value of 20
. Using the right shift operator (>>
), the value of number
is shifted two positions to the right. The result is 5
, which is printed to the console.
- Unsigned Right Shift (
>>>
) Example:
public class UnsignedRightShiftExample { public static void main(String[] args) { int number = -20; // Binary: 11111111111111111111111111101100 int shifted = number >>> 2; // Unsigned right shift by 2 positions System.out.println(shifted); // Output: 1073741829 } }
Output:
1073741829
Explanation:
The program defines a variable number
with a value of -20
. Using the unsigned right shift operator (>>>
), the value of number
is shifted two positions to the right. The result is 1073741829
, which is printed to the console.
These programs demonstrate the use of shift operators to shift the bits of a number and perform bitwise operations in Java.
Happy Learning.