The signed shift right operator >>
shifts a bit pattern to the right. This operator operates with two operand, the left-hand operand to be shifted and the right-hand operand tells how much position to shift.
Shifting a 1000
bit pattern using the >>
operator 2 position will produce a 0010
bit pattern. The signed shift right operator produce a result that equals to dividing a number by 2.
package org.kodejava.basic;
public class SignedRightShiftOperator {
public static void main(String[] args) {
// The binary representation of 32 is
// "00000000000000000000000000100000"
int number = 32;
System.out.println("number = " + number);
System.out.println("binary = " +
Integer.toBinaryString(number));
// Using the shift right operator we shift the bits
// four times to the right which resulting the result
// of "00000000000000000000000000000010"
int shiftedRight = number >> 4;
System.out.println("shiftedRight = " + shiftedRight);
System.out.println("binary = " +
Integer.toBinaryString(shiftedRight));
}
}
The result of the code snippet:
number = 32
binary = 100000
shiftedRight = 2
binary = 10
Latest posts by Wayan (see all)
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024