打印二进制
/**
* 打印2进制
* @param num
*/
public static void print2(int num) {
for (int i = 32; i >= 0; i--) {
System.out.print((num & (1 << i)) == 0 ? "0" : "1");
}
System.out.println();
}
与运算符&
两个操作数位中都为1,那么结果是1,否则为0
100000000000000000000000000000011
000000000000000000000000000000100 &
000000000000000000000000000000000
或运算符|
两个操作数位中只要有一个为1,那么结果是1,否则为0
100000000000000000000000000000011
000000000000000000000000000000100 |
100000000000000000000000000000111
非运算符 ~
如果位为0,那么结果为1,如果位为1,那么结果是0
100000000000000000000000000000011 ~
011111111111111111111111111111100
异或运算符 ^
两个操作数位中,相同结果为0,不同为1
100000000000000000000000000000011
000000000000000000000000000000100 ^
100000000000000000000000000000111