-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBinary.java
More file actions
30 lines (26 loc) · 912 Bytes
/
Binary.java
File metadata and controls
30 lines (26 loc) · 912 Bytes
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
package cn.ucaner.core.base;
/**
* 补码/反码相关知识
* https://www.cnblogs.com/zhangziqiu/archive/2011/03/30/ComputerCode.html
* http://weihe6666.iteye.com/blog/1190033
*
* 在计算机中,负数以原码的补码形式表达。
*/
public class Binary {
public static void main(String[] args) {
int i = 5;
int j = 10;
System.out.println(i + ~j);
int[] arr = new int[] {3, -2};
for (int a : arr) {
//原数
System.out.println("a:" + a + " 二进制:" + Integer.toBinaryString(a));
// 按位取反
System.out.println("~a:" + ~a + " 二进制:" + Integer.toBinaryString(~a));
// 相反数
System.out.println("-a:" + -a + " 二进制:" + Integer.toBinaryString(-a));
System.out.println(-a == ~a + 1);
System.out.println(~a == -a - 1);
}
}
}