/* 位运算
* java使用补码来表示2进制数,最高位为符号位,正数为0,负数为1,补码规定:
* 整数,最高位是0,其余是本身,如 +42的补 码 为 00101010
* 负数,最高位是1,将其余的决定值按位取反,最后+1,即为负数的补码;
* 如 -42的 补 码 为 11010110 (00101010 按位 取 反 11010101 +1=11010110 )
********
*逻辑运算*
********
a&b 与[真真为真,真假为假]
0000 0000 0000 0000 0000 0000 0000 0001
0000 0000 0000 0000 0000 0000 0000 0010
=
0000 0000 0000 0000 0000 0000 0000 0000
System.out.println(1 & 2);
0000 0000 0000 0000 0000 0000 0000 0001
0000 0000 0000 0000 0000 0000 0000 0010
=
0000 0000 0000 0000 0000 0000 0000 0011
System.out.println(1 | 2);
0000 0000 0000 0000 0000 0000 0000 0001
=
1111 1111 1111 1111 1111 1111 1111 1110
System.out.println(~1);
0000 0000 0000 0000 0000 0000 0000 0001
0000 0000 0000 0000 0000 0000 0000 0010
=
0000 0000 0000 0000 0000 0000 0000 0011
System.out.println(1^2);
*移位运算*
********
a>>b 有符号右移位;将a右移b位;若正数,高位补0,负数,高位补1
0000 0000 0000 0000 0000 0000 0000 0011
>>1
=
0000 0000 0000 0000 0000 0000 0000 0001
System.out.println(3>>1);
0000 0000 0000 0000 0000 0000 0000 0011
<<2
0000 0000 0000 0000 0000 0000 0000 1100
System.out.println(3<<2);
0000 0000 0000 0000 0000 0000 0000 0011
>>1
=
0000 0000 0000 0000 0000 0000 0000 0001
System.out.println(3>>>1);
结果为1,与3/2的1次幂相同
*/
System.out.println(3>>>1);
System.out.println();
* java使用补码来表示2进制数,最高位为符号位,正数为0,负数为1,补码规定:
* 整数,最高位是0,其余是本身,如 +42的补 码 为 00101010
* 负数,最高位是1,将其余的决定值按位取反,最后+1,即为负数的补码;
* 如 -42的 补 码 为 11010110 (00101010 按位 取 反 11010101 +1=11010110 )
********
*逻辑运算*
********
a&b 与[真真为真,真假为假]
0000 0000 0000 0000 0000 0000 0000 0001
0000 0000 0000 0000 0000 0000 0000 0010
=
0000 0000 0000 0000 0000 0000 0000 0000
System.out.println(1 & 2);
结果为0
0000 0000 0000 0000 0000 0000 0000 0001
0000 0000 0000 0000 0000 0000 0000 0010
=
0000 0000 0000 0000 0000 0000 0000 0011
System.out.println(1 | 2);
结果为3
0000 0000 0000 0000 0000 0000 0000 0001
=
1111 1111 1111 1111 1111 1111 1111 1110
System.out.println(~1);
结果为:-2
0000 0000 0000 0000 0000 0000 0000 0001
0000 0000 0000 0000 0000 0000 0000 0010
=
0000 0000 0000 0000 0000 0000 0000 0011
System.out.println(1^2);
结果为 3
*移位运算*
********
a>>b 有符号右移位;将a右移b位;若正数,高位补0,负数,高位补1
0000 0000 0000 0000 0000 0000 0000 0011
>>1
=
0000 0000 0000 0000 0000 0000 0000 0001
System.out.println(3>>1);
结果为1,结果与 3 / 2的1次幂相同
0000 0000 0000 0000 0000 0000 0000 0011
<<2
0000 0000 0000 0000 0000 0000 0000 1100
System.out.println(3<<2);
结果为12,与3*2的2次幂相同
0000 0000 0000 0000 0000 0000 0000 0011
>>1
=
0000 0000 0000 0000 0000 0000 0000 0001
System.out.println(3>>>1);
结果为1,与3/2的1次幂相同
*/
System.out.println(3>>>1);
System.out.println();