The signed shift left operator <<
shifts a bit pattern to the left. 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 0010
bit pattern using the <<
operator 2 position will produce a 1000
bit pattern. The signed shift left operator produce a result that equals to multiplying a number by 2, which double the value of a number.
package org.kodejava.basic;
public class SignedLeftShiftOperator {
public static void main(String[] args) {
// The binary representation of 2 is "0010"
int number = 2;
System.out.println("number = " + number);
System.out.println("binary = " +
Integer.toBinaryString(number));
// Using the shift left operator we shift the bits two
// times to the left. This will shift the "0010" into
// "1000"
int shiftedLeft = number << 2;
System.out.println("shiftedLeft = " + shiftedLeft);
System.out.println("binary = " +
Integer.toBinaryString(shiftedLeft));
}
}
The result of the code snippet:
number = 2
binary = 10
shiftedLeft = 8
binary = 1000
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
Bolches yarboclos
1 << 33 is 1
Hi Paok,
It’s 2 right? not 1