1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
| public static void main(String[] args) {
int t1 = 1;
int t2 = 1 << 1;
int t3 = 1 << 2;
int t4 = 1 << 3;
int t5 = 1 << 4;
int t6 = 1 << 5;
int t7 = 1 << 6;
binaryString(t1);
binaryString(t2);
binaryString(t3);
binaryString(t4);
binaryString(t5);
binaryString(t6);
binaryString(t7);
System.out.println("======");
//开启t1, t3
int tmp = t1 + t3;
binaryString(tmp);
//判断开启了 t1
System.out.println((tmp & t1) == t1);
//判断开启了 t3
System.out.println((tmp & t3) == t3);
//判断同时开启了 t1,t3
System.out.println((tmp & (t1 + t3)) == (t1 + t3));
System.out.println("======");
//再追加开启t2
tmp = (tmp + t2);
//打印二进制
binaryString(tmp);
//判断开启了 t2
System.out.println((tmp & t2) == t2);
//判断同时开启了 t1,t2, t3
System.out.println((tmp & (t1 + t2 + t3)) == (t1 + t2 + t3));
//关闭t3 开启t5
tmp = tmp - t3 + t5;
//判断同时开启了 t1,t2
System.out.println((tmp & (t1 + t2)) == (t1 + t2));
//判断同时开启了 t5
System.out.println((tmp & (t5)) == (t5));
//判断同时开启了 t4
System.out.println((tmp & (t4)) == (t4));
}
/**
* 打印二进制
* mysql转二进制 : select bin(67) from dual;
*
* @param num
*/
public static void binaryString(int num) {
String binaryString = Integer.toBinaryString(num);
System.out.println(num + " -> " + binaryString);
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| 1 -> 1
2 -> 10
4 -> 100
8 -> 1000
16 -> 10000
32 -> 100000
64 -> 1000000
======
5 -> 101
true
true
true
======
7 -> 111
true
true
true
true
false
|