Above and beyond bitwise operators, JLS provides 3 bit shift operators for bit manipulation in Java.
Namely:
Syntax:
Here,
Left-hand Operand is the value to be shifted, i.e., shifting of it's binary value.
Right-hand Operand specifies the shift distance.
i.e.,
It shifts the binary value of the left operand with sign to left side and fills the empty place with 0.
Let's solve some question to understand the concept fo Left shift operator:
Therefore, 20 << 2 = 80. So, Answer = 80.
Note: If you closely observe:
System.out.println(10<<2); // output = = 40
System.out.println(10<<3); // output = = 80
System.out.println(10<<4); // output = = 160
Left Shift Operator also act's as . But, remember the default size of the data type.
So, let's convert -20 to it's binary equivalent:
Now, let's shift it to left with shift distance = 2, as mentioned in Q.2.
Q. 4. what's the result of
Output:
For +ve (positive) number it fills with 0. And,
for -ve (negative) number it fills with 1.
Example: 20 >> 2
Therefore, 20 >> 2 = 5 . And it is a +ve integer. So, rest of the empty space will be filled with the sign value i.e., 0.
5 will be represented as
Since, the LHS operand is a -ve integer. The blank space will be filled by the -ve sign value '1'.
So, after shifting 2 bits right 11111011 two's complement of -5.
-20 >> 2 = -5, which is represented as
Two's complement of -2 = .
Since, the LHS operand is a +ve integer. The blank space will be filled by the +ve sign value i.e., '0'.
So, by JLS's specified rule for shift operator. least 5 digits of the RHS operand is passed through bitwise AND operation with the MASK value 0b11111.
So, 11110 AND 0b11111 = 11110 , which is the binary representation of 30.
After shifting the binary of LHS operand to 30 bits right 00000000 0.
20 >> -2 = 0, which is represented as
Two's complement of -2 = .
Since, the LHS operand is a -ve integer. The blank space will be filled by the -ve sign value i.e., '1'.
So, by JLS's specified rule for shift operator. least 5 digits of the RHS operand is passed through bitwise AND operation with the MASK value 0b11111.
So, 11110 AND 0b11111 = 11110 , which is the binary representation of 30.
After shifting the binary of LHS operand to 30 bits right 11111111 Two's complement of -1.
-20 >> -2 = -1, which is represented as
Note: If you closely observe:
System.out.println(10<<2); // output = => 2
System.out.println(10<<3); // output = => 1
System.out.println(10<<4); // output = => 0
Right Shift Operator also act's as . But, remember the default size of the data type.
Output:
Output:
Ans. To avoid redundancy. In java the sign values are stored in MSB. In left shift operation, the right side bit gets empty. So, in left shift operation there is no point in talking about signed and unsigned variable. The available left shift operator does the work.
FYI, there is no unsigned right shift operator ">>>" in C, but it's there is Java. So, we can say that it's a java specific operator.
p.s. this post content is concisely based on the data provided by the JLS ( Java Language Specification) on Java 8 release.
* ( Most of the questions have already been answered in the explanation. Yet if you find any question left unanswere. Please comment it below... )
Namely:
- Left Shift Operator (<<)
- Signed Right Shift Operator (>>)
- Unsigned Right Shift Operator (>>>)
Syntax:
Left-hand operand SHIFT OPERATOR Right-hand operand
Here,
Left-hand Operand is the value to be shifted, i.e., shifting of it's binary value.
Right-hand Operand specifies the shift distance.
i.e.,
Value to be shifted SHIFT OPERATOR the shift distance.
1. Left Shift Operator "<<":
Let's solve some question to understand the concept fo Left shift operator:
Q.1 Solve 20 << 2 shift 20 by 2 bits in the left.
Sol. The binary representation of 20 is: 00010100Fig. 1.1 - Logic behind left shift operation 20 << 2 |
Therefore, 20 << 2 = 80. So, Answer = 80.
Note: If you closely observe:
System.out.println(10<<2); // output = = 40
System.out.println(10<<3); // output = = 80
System.out.println(10<<4); // output = = 160
Left Shift Operator also act's as . But, remember the default size of the data type.
Q. 2. what's the result of
Sol. -20 << 2
?
Negative integers are stored as it two's complement in Java.So, let's convert -20 to it's binary equivalent:
Now, let's shift it to left with shift distance = 2, as mentioned in Q.2.
Since, the given integer was represented in 8 bits, we will only consider 8 bits for now. So, we got 1 0 1 1 0 0 0 0
since MSB = 1. It implies, this binary value is a representation of a -ve integer.
After inverting the digits 0 1 0 0 1 1 1 1
After adding 1 0 1 0 1 0 0 0 0
128*0+ 64*1 + 32*0 + 16*1 + 8*0 + 4*0 + 2*0 + 1*0 = 64 + 16 = 80
So, since we found that it's a -ve integer => Answer = -80.
Q. 3. what's the result of
20 << -2
?
Sol. Here the shift distance is "-2". So, this expression is considered undefined in C. But, in Java using Netbeans, we got an output for this expression. And Let's find it out how:
2 is represented in binary form as => 00000010
Inverting it => 11111101
After adding '1' => 11111110
So, -2 is represented as 11111110 in binary form.
Note: According to JSL:
- If the promoted type of the left-hand operand is int, only the five lowest-order bits of the right-hand operand are used as the shift distance. It is as if the right-hand operand were subjected to a bitwise logical AND operator with the mask value 0x1f (0b11111). The shift distance actually used is therefore always in the range 0 to 31, inclusive.
- If the promoted type of the left-hand operand is long, then only the six lowest-order bits of the right-hand operand are used as the shift distance. It is as if the right-hand operand were subjected to a bitwise logical AND operator with the mask value 0x3f (0b111111). The shift distance actually used is therefore always in the range 0 to 63, inclusive.
So, the last 5 bits of 11111110 is 11110
After bitwise AND operation we get 11110. In integer form it's represented as 30.
Shift distance = 30.
If we will shift , 30 times to left. We will get 32 0's. As 20 is a positive integer. And the size of int in java is 32 bit. Answer = 0.
Shift distance = 30.
If we will shift , 30 times to left. We will get 32 0's. As 20 is a positive integer. And the size of int in java is 32 bit. Answer = 0.
Q. 4. what's the result of -20 << -2
?
Sol. Two's complement of -20 =.
Two's complement of -2 = .
After subjecting the last 5 bits of 11111110 to bitwise AND operation with the mask 0b11111 we get 11110 it is the binary representation of 30.
shift distance = 30.
If we will shift , 30 times to left. We will get 32 0's. As -20 is an integer. And the size of int in java is 32 bit. Answer = 0.
Two's complement of -2 = .
After subjecting the last 5 bits of 11111110 to bitwise AND operation with the mask 0b11111 we get 11110 it is the binary representation of 30.
shift distance = 30.
If we will shift , 30 times to left. We will get 32 0's. As -20 is an integer. And the size of int in java is 32 bit. Answer = 0.
TEST CODE FOR ANALYSING LEFT SHIFT OPERATOR
public class ShiftOpptn {
public static void main(String[] args) {
/* Left shift operator */
System.out.println(20 << 2); // output = 80
System.out.println(Integer.toBinaryString(20 << 2)); // 1010000
System.out.println(-20 << 2); // -80
System.out.println(Integer.toBinaryString(-20 << 2));
// output =11111111111111111111111110110000
System.out.println(20 << -2); // 0
System.out.println(Integer.toBinaryString(20 << -2)); // 0
System.out.println(-20 << -2); // 0
System.out.println(Integer.toBinaryString(-20 << -2)); // 0
}
}
public static void main(String[] args) {
/* Left shift operator */
System.out.println(20 << 2); // output = 80
System.out.println(Integer.toBinaryString(20 << 2)); // 1010000
System.out.println(-20 << 2); // -80
System.out.println(Integer.toBinaryString(-20 << 2));
// output =11111111111111111111111110110000
System.out.println(20 << -2); // 0
System.out.println(Integer.toBinaryString(20 << -2)); // 0
System.out.println(-20 << -2); // 0
System.out.println(Integer.toBinaryString(-20 << -2)); // 0
}
}
Output:
Fig. 1.5 - Output produced for above code in Netbeans IDE |
2. Signed Right shift operator ">>"
It shifts the binary value of the left operand with sign to RIGHT side and fills the empty place with the value of SIGN BIT.For +ve (positive) number it fills with 0. And,
for -ve (negative) number it fills with 1.
Example: 20 >> 2
Fig. 2.1 - Logic behind signed right shift operation 20 >> 2 |
Therefore, 20 >> 2 = 5 . And it is a +ve integer. So, rest of the empty space will be filled with the sign value i.e., 0.
5 will be represented as
00000000 00000000 00000000 00000101
in binary.
Q.1. What's the result of -20 >> 2 ?
Sol. Two's complement of -20 = .Since, the LHS operand is a -ve integer. The blank space will be filled by the -ve sign value '1'.
So, after shifting 2 bits right 11111011 two's complement of -5.
-20 >> 2 = -5, which is represented as
11111111 11111111 11111111
11111011
in binary.
Q.2. What's the result of 20 >> -2 ?
Sol. Binary form of 20 = Two's complement of -2 = .
Since, the LHS operand is a +ve integer. The blank space will be filled by the +ve sign value i.e., '0'.
So, by JLS's specified rule for shift operator. least 5 digits of the RHS operand is passed through bitwise AND operation with the MASK value 0b11111.
So, 11110 AND 0b11111 = 11110 , which is the binary representation of 30.
After shifting the binary of LHS operand to 30 bits right 00000000 0.
20 >> -2 = 0, which is represented as
00000000 00000000 00000000
00000000
in binary.
Q.3. What's the result of -20 >> -2 ?
Sol. Two's complement of -20 = Two's complement of -2 = .
Since, the LHS operand is a -ve integer. The blank space will be filled by the -ve sign value i.e., '1'.
So, by JLS's specified rule for shift operator. least 5 digits of the RHS operand is passed through bitwise AND operation with the MASK value 0b11111.
So, 11110 AND 0b11111 = 11110 , which is the binary representation of 30.
After shifting the binary of LHS operand to 30 bits right 11111111 Two's complement of -1.
-20 >> -2 = -1, which is represented as
11111111 11111111 11111111
11111111
in binary.Note: If you closely observe:
System.out.println(10<<2); // output = => 2
System.out.println(10<<3); // output = => 1
System.out.println(10<<4); // output = => 0
Right Shift Operator also act's as . But, remember the default size of the data type.
TEST CODE FOR ANALYSING RIGHT SHIFT OPERATOR
public class ShiftOpptn {
public static void main(String[] args) {
/* Signed Right shift operator */
System.out.println("1. 20 in binary = "+Integer.toBinaryString(20));
System.out.println("2. 20 >> 2 = "+ (20 >> 2));
System.out.println("Binary = "+ Integer.toBinaryString(20 >> 2));
System.out.println("3. -20 >> 2 = "+ (-20 >> 2));
System.out.println("Binary = "+ Integer.toBinaryString(-20 >> 2));
System.out.println("4. 20 >> -2 = "+ (20 >> -2));
System.out.println("Binary = "+ Integer.toBinaryString(20 >> -2));
System.out.println("5. -20 >> -2 = "+ (-20 >> -2));
System.out.println("Binary = "+ Integer.toBinaryString(-20 >> -2));
}
}
public static void main(String[] args) {
/* Signed Right shift operator */
System.out.println("1. 20 in binary = "+Integer.toBinaryString(20));
System.out.println("2. 20 >> 2 = "+ (20 >> 2));
System.out.println("Binary = "+ Integer.toBinaryString(20 >> 2));
System.out.println("3. -20 >> 2 = "+ (-20 >> 2));
System.out.println("Binary = "+ Integer.toBinaryString(-20 >> 2));
System.out.println("4. 20 >> -2 = "+ (20 >> -2));
System.out.println("Binary = "+ Integer.toBinaryString(20 >> -2));
System.out.println("5. -20 >> -2 = "+ (-20 >> -2));
System.out.println("Binary = "+ Integer.toBinaryString(-20 >> -2));
}
}
Output:
Fig 2.2 Output produced for above code in Netbeans IDE |
3. Unsigned right shift operator ">>>"
It shifts the binary value of the left operand without preserving SIGN to RIGHT side and fills the empty place with 0.
So, It is also known as unsigned right shift operator, and right shift with zero fill.
>>> is an unsigned right shift operator. So, the blank space will be filled by '0'.
after shifting 2 bits right (00000000 00000000 00000000 00000101)2 = (5)10.
20 >> 2 = 5, which is represented as
>>> is an unsigned right shift operator. So, the blank space will be filled by '0'.
after shifting 2 bits right (00111111111111111111111111111011)2 = (1073741819)10.
-20 >> 2 = 1073741819, which is represented as
Two's complement of -2 = .
>>> is an unsigned right shift operator. So, the blank space will be filled by '0'.
So, by JLS's specified rule for shift operator. least 5 digits of the RHS operand is passed through bitwise AND operation with the MASK value 0b11111.
So, 11110 AND 0b11111 = 11110 , which is the binary representation of 30.
After shifting the binary of LHS operand to 30 bits right 00000000 0.
20 >> -2 = 0, which is represented as
Two's complement of -2 = .
>>> is an unsigned right shift operator. So, the blank space will be filled by '0'.
So, by JLS's specified rule for shift operator. least 5 digits of the RHS operand is passed through bitwise AND operation with the MASK value 0b11111.
So, 11110 AND 0b11111 = 11110 , which is the binary representation of 30.
After shifting the binary of LHS operand to 30 bits right (00000000000000000000000000000011)2 = (3)10
-20 >> -2 = 3, which is represented as
So, It is also known as unsigned right shift operator, and right shift with zero fill.
Q.1. What's the result of 20 >> 2 ?
Sol. Two's complement of 20 = .>>> is an unsigned right shift operator. So, the blank space will be filled by '0'.
after shifting 2 bits right (00000000 00000000 00000000 00000101)2 = (5)10.
20 >> 2 = 5, which is represented as
00000000
00000000
00000000
00000101
in binary.
Q.2. What's the result of -20 >> 2 ?
Sol. Two's complement of -20 = .>>> is an unsigned right shift operator. So, the blank space will be filled by '0'.
after shifting 2 bits right (00111111111111111111111111111011)2 = (1073741819)10.
-20 >> 2 = 1073741819, which is represented as
00111111 11111111 11111111
11111011
in binary.
Q.3. What's the result of 20 >> -2 ?
Sol. Binary form of 20 = Two's complement of -2 = .
>>> is an unsigned right shift operator. So, the blank space will be filled by '0'.
So, by JLS's specified rule for shift operator. least 5 digits of the RHS operand is passed through bitwise AND operation with the MASK value 0b11111.
So, 11110 AND 0b11111 = 11110 , which is the binary representation of 30.
After shifting the binary of LHS operand to 30 bits right 00000000 0.
20 >> -2 = 0, which is represented as
00000000 00000000 00000000
00000000
in binary.
Q.4. What's the result of -20 >> -2 ?
Sol. Two's complement of -20 = Two's complement of -2 = .
>>> is an unsigned right shift operator. So, the blank space will be filled by '0'.
So, by JLS's specified rule for shift operator. least 5 digits of the RHS operand is passed through bitwise AND operation with the MASK value 0b11111.
So, 11110 AND 0b11111 = 11110 , which is the binary representation of 30.
After shifting the binary of LHS operand to 30 bits right (00000000000000000000000000000011)2 = (3)10
-20 >> -2 = 3, which is represented as
00000000 00000000 00000000
00000011
in binary.
TEST CODE FOR ANALYSING UNSIGNED RIGHT SHIFT OPERATOR
public class ShiftOpptn {
public static void main(String[] args) {
/* Unsigned Right shift operator ">>>" */
System.out.println("1. 20 in binary = "+Integer.toBinaryString(20));
System.out.println("2. 20 >>> 2 = "+ (20 >>> 2));
System.out.println("Binary = "+ Integer.toBinaryString(20 >>> 2));
System.out.println("3. -20 >>> 2 = "+ (-20 >>> 2));
System.out.println("Binary = "+ Integer.toBinaryString(-20 >>> 2));
System.out.println("4. 20 >>> -2 = "+ (20 >>> -2));
System.out.println("Binary = "+ Integer.toBinaryString(20 >>> -2));
System.out.println("5. -20 >>> -2 = "+ (-20 >>> -2));
System.out.println("Binary = "+ Integer.toBinaryString(-20 >>> -2));
}
}
public static void main(String[] args) {
/* Unsigned Right shift operator ">>>" */
System.out.println("1. 20 in binary = "+Integer.toBinaryString(20));
System.out.println("2. 20 >>> 2 = "+ (20 >>> 2));
System.out.println("Binary = "+ Integer.toBinaryString(20 >>> 2));
System.out.println("3. -20 >>> 2 = "+ (-20 >>> 2));
System.out.println("Binary = "+ Integer.toBinaryString(-20 >>> 2));
System.out.println("4. 20 >>> -2 = "+ (20 >>> -2));
System.out.println("Binary = "+ Integer.toBinaryString(20 >>> -2));
System.out.println("5. -20 >>> -2 = "+ (-20 >>> -2));
System.out.println("Binary = "+ Integer.toBinaryString(-20 >>> -2));
}
}
Output:
Fig 3.1 The output produced by above code in Netbeans IDE |
More on bit-shift operators
- The shift operators are left-associative.
- After Unary numeric promotion, if the type of each of the operands of a shift operator is not a primitive integral type, then that will cause a compile time error.
Capsule Notes
The shift operators include left shift <<
, signed right shift >>
, and unsigned right shift >>>
.- The value of
n<<s
isn
left-shifteds
bit positions with zero-extension. - The value of
n>>s
isn
right-shifteds
bit positions with sign-extension. - The value of
n>>>s
isn
right-shifteds
bit positions with zero-extension.
- Negative integers are stores as it's two's complement in Java.
- Note: According to JSL:
- If the promoted type of the left-hand operand is
int
, only the five lowest-order bits of the right-hand operand are used as the shift distance after getting subjected to a bitwise logical AND operator with the mask value0x1f
(0b11111
). The shift distance actually used is therefore always in the range0
to31
, inclusive. - If the promoted type of the left-hand operand is
long
, then only the six lowest-order bits of the right-hand operand are used as the shift distance. It is as if the right-hand operand were subjected to a bitwise logical AND operator with the mask value0x3f
(0b111111
). The shift distance actually used is therefore always in the range0
to63
, inclusive.
- The shift operators are left-associative.
- After Unary numeric promotion, if the type of each of the operands of a shift operator is not a primitive integral type, then that will cause a compile time error.
Interview Questions:
Q. 1. Why is there no <<< operator in Java 8?Ans. To avoid redundancy. In java the sign values are stored in MSB. In left shift operation, the right side bit gets empty. So, in left shift operation there is no point in talking about signed and unsigned variable. The available left shift operator does the work.
FYI, there is no unsigned right shift operator ">>>" in C, but it's there is Java. So, we can say that it's a java specific operator.
p.s. this post content is concisely based on the data provided by the JLS ( Java Language Specification) on Java 8 release.
* ( Most of the questions have already been answered in the explanation. Yet if you find any question left unanswere. Please comment it below... )
Bibliography:
[1] This article uses material from the Wikipedia article Operator associativity, which is released under the Creative Commons Attribution-Share-Alike License 3.0 (view authors).
1 comment
CS Casino:GO - Play now with £10 bonus - Shootercasino
Welcome dafabet link to CS Casino and get £10 free bonus on your first deposit. Play against thousands of players from around the 온라인카지노 world, compete ทางเข้า m88 in CSGO leagues,
commentPost a Comment