You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1.5 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

一个数的相反数为取反加1.
取反的结果和原来的数相加结果为2^32^-1,再加1则进位为0。
2^32^-1二进制为
```shell
11111111 11111111 11111111 11111111
```
```shell
# 例如 int a = 1024;二进制为
00000000 00000000 00001000 00000000
# 取反后
11111111 11111111 11110111 11111111
# 相加为
11111111 11111111 11111111 11111111
a+~a+1=0;则-a=~a+1;
```
```Java
# 打印int类型二进制字符串方法
private static void printCompleteBinaryStr(int num){
for (int i = 31; i >= 0; i--) {
System.out.print((num & (1 << i)) == 0 ? "0" : "1");
if (i < 31 && (i + 1) % 8 == 0) {
System.out.print(" ");
}
}
System.out.println();
}
```
Integer.MIN_VALUE取反加一后仍然为Integer.MIN_VALUE这不是bug。
Integer.MAX_VALUE加1后溢出为Integer.MIN_VALUEInteger.MIN_VALUE减一则溢出为Integer.MAX_VALUE。
异或运算满足交换律和结合律
a^b=b^a
(a^b)^c=a^(b^c)
a、b交换
a=a^b
b=a^b
a=a^b
给定数组arr只有一个数出现了奇数次其它数都是偶数次找到并打印这个数。
偶数次异或为0奇数次异或为本身遍历每一个元素并异或最后结果就是奇数次的数字。
拿到一个数二进制最右侧的1
a&(~a+1)
给定数组arr只有一种数出现了K次其他数都出现M次。
假设a出现k次b出现m次
将所有数字的二进制位相加则t[i] % m就是a的二进制在i位置次数要么是0要么是k