有符号左移运算符<<将一个位模式左移。该运算符使用两个操作数进行操作,左操作数要移位,右操作数告诉要移位多少位置。
0010使用<<运算符2的位置移动位图将产生1000位图。带符号的左移运算符产生的结果等于将数字乘以2,这会使数字的值加倍。
package org.nhooo.example.fundamental;
public class SignedLeftShiftOperator {
public static void main(String[] args) {
int number = 2;
System.out.println("number = " + number);
System.out.println("binary = " + Integer.toBinaryString(number));
int shiftedLeft = number << 2;
System.out.println("shiftedLeft = " + shiftedLeft);
System.out.println("binary = " + Integer.toBinaryString(shiftedLeft));
}
}代码段的结果:
number = 2 binary = 10 shiftedLeft = 8 binary = 1000