-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathBitOperator.java
More file actions
37 lines (31 loc) · 1.27 KB
/
Copy pathBitOperator.java
File metadata and controls
37 lines (31 loc) · 1.27 KB
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
//位运算
public class BitOperator {
//编写一个main方法
public static void main(String[] args) {
//看老师的推导过程
//1. 先得到 2的补码 => 2的原码 00000000 00000000 00000000 00000010
// 2的补码 00000000 00000000 00000000 00000010
//2. 3的补码 3的原码 00000000 00000000 00000000 00000011
// 3的补码 00000000 00000000 00000000 00000011
//3. 按位&
// 00000000 00000000 00000000 00000010
// 00000000 00000000 00000000 00000011
// 00000000 00000000 00000000 00000010 & 运算后的补码
// 运算后的原码 也是 00000000 00000000 00000000 00000010
// 结果就是 2
System.out.println(2&3);//2
//推导
//1. 先得到 -2的原码 10000000 00000000 00000000 00000010
//2. -2的 反码 11111111 11111111 11111111 11111101
//3. -2的 补码 11111111 11111111 11111111 11111110
//4. ~-2操作 00000000 00000000 00000000 00000001运算后的补码
//5. 运算后的原码 就是 00000000 00000000 00000000 00000001 => 1
System.out.println(~-2);//1
//推导
//1. 得到2的补码 00000000 00000000 00000000 00000010
//2. ~2操作 11111111 11111111 11111111 11111101 运算后的补码
//3. 运算后的反码 11111111 11111111 11111111 11111100
//4. 运算后的原码 10000000 00000000 00000000 00000011=>-3
System.out.println(~2); //-3
}
}