<< 左移运算符(一个整数左移n位,相当于整数乘以2的n次方) ;
>> 右移运算符
1 Scanner scanner4 = new Scanner(System.in); 2 System.out.println("请输入一个整数:"); 3 long number1 = scanner4.nextLong(); 4 System.out.println("你输入的数字是:" + number1); 5 System.out.println("该数字乘以2的运算结果是:" + (number1 << 1)); 6 System.out.println("该数字乘以4的运算结果是:" + (number1 << 2)); 7 System.out.println("该数字乘以8的运算结果是:" + (number1 << 3)); 8 System.out.println("该数字乘以16的运算结果是:" + (number1 << 4));
实现两个数大小交换(经典):
1 Scanner scanner5 = new Scanner(System.in); 2 System.out.println("请输入变量A的值"); 3 long A = scanner5.nextLong(); 4 System.out.println("请输入变量B的值"); 5 long B = scanner5.nextLong(); 6 System.out.println("A=" + A + " B=" + B); 7 System.out.println("执行变量互换..."); 8 A = A ^ B; 9 B = B ^ A; 10 A = A ^ B; 11 System.out.println("A=" + A + " B=" + B);