- /*
- 位运算符:
- 1.<< : 将左操作数的二进制位,向左移动右操作数的位数;右侧空位补0;
- int num1 = 10;
- System.out.println(num1 << 2);
- 实际移动的位数 = 右操作数 % 32
- 每左移1位,相当于原数 * 2;
- 2.>> : 带符号右移:每右移1位,右侧数字丢弃,左侧补原最高位的(符号位)的值。
- 每右移一位,相当于原数 / 2;
- 正数右移,始终是正数;
- 负数右移,始终是负数;
- 3.>>> : 无符号右移:每右移1位,右侧数字丢弃,左侧补0;
- 对于负数,一旦无符号右移1位,将变为正数;
- 4.& :按位与:两位都是1,结果为1,其它都为0;
- 5.| : 按位或:有1位是1,结果为1,其它都为0;
- 6.^ : 按位异或:两位相同,结果0,不同结果为1;
- 7.~ : 按位取反:0 变 1 ,1 变 0;
- */
- class Demo
- {
- public static void main(String[] args)
- {
- int num1 = 10;
- System.out.println("num1 << 2 : " + (num1 << 2));//40
- System.out.println("num1 = " + num1);//原值不变
- int num2 = 1;//0000 0000 0000 0000 0000 0000 0000 0001
- System.out.println("num2 << 30 : " + (num2 << 30));
- System.out.println("num2 << 31 : " + (num2 << 31));
- System.out.println("num2 << 32 : " + (num2 << 32));
- System.out.println("num2 << 33 : " + (num2 << 33));
- int num3 = 1;
- System.out.println("num3 >> 1 : " + (num3 >> 1));
- int num4 = -1;
- System.out.println("num4 >> 1 : " + (num4 >> 1));
- int num5 = -48789;
- System.out.println("num5 >>> 1 : " + (num5 >>> 1));//结果是正数;
- int num6 = 3;
- int num7 = 5;
- System.out.println("num6 & num7 : " + (num6 & num7));
- int num8 = 3;
- int num9 = 4;
- System.out.println("num8 | num9 : " + (num8 | num9));
- int num10 = 3;
- int num11 = 5;
- System.out.println("num10 ^ num11 : " + (num10 ^ num11));
- int num12 = 1;
- System.out.println("~num12 : " + (~num12));
- int num13 = -1;
- System.out.println("~num13 : " + (~num13));
- }
- }
复制代码
|
|