Bitwise Shift
Operators
Three kinds of shift operators are present in Java namely Bitwise Left shift operator (<<), Bitwise right shift operator (>>) and Bitwise left shift with zero extension operator (>>>). If you want to know more about the precedence of operators, you can CHECK HERE.
|
Precedence |
Operator |
Type |
Associativity |
|
10 |
<< |
Bitwise left shift |
Left to Right |
|
>> |
Bitwise right
shift |
||
|
>>>
|
Bitwise right shift with zero extension |
The format for
using bitwise left shift operator is: a<<b where ‘a’ is the number and ‘b’
represents how many times the number ‘a’ has to be shifted.
Eg) 7<<1
Here, 7 is a decimal number which is first converted
into its binary equivalent.
7 – 0000 0111
Since b=1,
Each digit in 7’s
binary equivalent is shifted once towards the left side. Thus the output
becomes, 0000 1110 which is equivalent to 14. This operation can be implemented
in Java as follows,
class
operator{
public static void main(String
args[]){
// Assigning value of x as 7
byte x=7;
//Performing left shift operation
System.out.println("Bitwise
Left Shift: 7<<1 = "+(x<<1));
System.out.println("Bitwise
Left Shift: 7<<7 = "+(x<<7));
}
}
Java code to perform Bitwise right shift operation
The syntax for performing bitwise right shift
operation is a >> b and similar to the left shift operation, the number ‘a’
is shifted towards the right by ‘b’ times. Taking the example of 7 >> 1:
7 – 0000 0111
The right shift of ‘7’ by one bit results in 0000 0011
which is equivalent to 3.
Maximum number of shifts in Java
The maximum number of shifts which can occur in Java
is 32 and when user gives the value of b greater than 32, the following happens:
Eg) 7 >> 33
The binary equivalent of 33 is 0010 0001. Whenever
b> 32, only the last 5 digits of the binary equivalent of ‘b’ is taken. In
this case, the last 5 bits are 0 0001. So the number 7 is shifted once towards
the right. Hence the resultant is 0000 0011 which is equivalent to 3.
Right shift operation of a negative number
In case of negative numbers, the shifting operation is
done in a different manner.
Eg) -7 >> 1
Step 1: The 2’s complement of the number ‘a’ is taken
The equivalent binary value of 7 is 0000 0111
2’s complement = 1’s complement + 1
1’s complement if taken by substituting all 1’s with 0’s
and vice versa. Hence, the 1’s complement of 7 is 1111 1000. For taking 2’s
complement, add 1 with the 1’s complement.
1111 1000
1 +
1111 1001
Step 2: The shifting operation is then done using the 2’s complement. While
shifting, the Most Significant Bit (MSB) should be filled with 1.
Since ‘b’ value is 1, the result obtained in Step 1 is
shifted by 1 and the MSB is filled with 1. The resultant is 1111 1100
Step 3: Take 2’s complement of the resultant which is the answer for the right
shift operation of a negative number. Now, taking 2’s complement of the
resultant 1111 1100, we get:
1’s complement of 1111 1101 = 0000 0011
Adding 1 to 1’s
complement = 0000 0100
The value of -7
>> 1 is -4
class operator{
public static void main(String args[]){
// Assigning value of x as 7
byte x=7;
//Performing right shift operation
System.out.println("Bitwise right Shift: 7>>1 = "+(x>>1));
System.out.println("Bitwise right shift: 7>>33 = "+(x>>33));
System.out.println("Bitwise right shift: -7>>1 = "+(-x>>1));
}
}Output
Bitwise right shift
with zero extension (>>>) format is a >>> b and for positive
number, the operation is same as a >> b. But, for negative numbers
instead of substituting 1’s step 2, 0 is substituted. This operator returns an
unsigned 32 bit integer and the following code provides the difference between
the right shift and right shift with extension operators.
class operator{
public static void main(String args[]){
// Assigning value of x as 7
byte x=7;
// Positive values
//Performing right shift operation
System.out.println("Bitwise right Shift: 7>>11 = "+(x>>>11));
//Performing right shift operation with zero extension
System.out.println("Bitwise right shift with zero extension: 7>>>11 = "+(x>>11));
// Negative input
//Performing right shift operation
System.out.println("Bitwise right shift: -7>>11 = "+(-x>>11));
//Performing right shift operation with zero extension
System.out.println("Bitwise right shift with zero extension: -7>>>11 = "+(-x>>>11));
}
}
Output
From this output, it can be clearly visualized that the
1st 2 outputs are same, since the input is positive. Changes occur
in both the operators only when implementing negative numbers.
To learn more about Java operators, you can check these useful links:




0 Comments