目录标题
① &0清零
一个数与一个各位都为0的数值相与,结果为零
举例:
0000 0101 1010
& 0000 0000 0000
---------------------
0000 0000 0000
int[] arr = {
1, 2, 3, 4, 5, 6, 8};
for (int i = 0; i < arr.length; i++) {
int res = arr[i] & 0;
System.out.print(res);
}
// 0000000
② &-1值不变
举例:
0000 0101 1010
& 1111 1111 1111
---------------------
0000 0101 1010
③ 根据 ① + ②
获取某个二进制数的指定位 → 10101110 & 00001111 = 00001110
把某个二进制数的指定位置为0 → 10101110 & 00001111 = 00001110
举例:
0000 0101 1010
& 0000 1111 0000
---------------------
0000 0101 0000
④ 判断奇偶
二进制未位为0就是偶数,为1就是奇数
Integer.toBinaryString(0) 0000
Integer.toBinaryString(1) 0001
Integer.toBinaryString(2) 0010
Integer.toBinaryString(3) 0011
Integer.toBinaryString(4) 0100
# 判断奇数
if ((a & 1) == 1)
# 判断偶数
if ((a & 1) == 0)
if (a % 2 == 0)
System.out.println((5 & 1) == 1 ? "奇数" : "偶数"); // 奇数
System.out.println((6 & 1) == 1 ? "奇数" : "偶数"); // 偶数
System.out.println((0 & 1) == 1 ? "奇数" : "偶数"); // 偶数
System.out.println((-1 & 1) == 1 ? "奇数" : "偶数"); // 奇数
System.out.println((-2 & 1) == 1 ? "奇数" : "偶数"); // 偶数
⑤ 判断一个整数是否为2的整数次方
# 记住2的n次方法的二进制的样子
(proFillZero(Integer.toBinaryString(0))); 00000000000000000000000000000000
(proFillZero(Integer.toBinaryString(2))); 00000000000000000000000000000010
(proFillZero(Integer.toBinaryString(4))); 00000000000000000000000000000100
(proFillZero(Integer.toBinaryString(8))); 00000000000000000000000000001000
(proFillZero(Integer.toBinaryString(16))); 00000000000000000000000000010000
(proFillZero(Integer.toBinaryString(32))); 00000000000000000000000000100000
(proFillZero(Integer.toBinaryString(64))); 00000000000000000000000001000000
(proFillZero(Integer.toBinaryString(128))); 00000000000000000000000010000000
(proFillZero(Integer.toBinaryString(256))); 00000000000000000000000100000000
(proFillZero(Integer.toBinaryString(512))); 00000000000000000000001000000000
(proFillZero(Integer.toBinaryString(1024))); 00000000000000000000010000000000
(proFillZero(Integer.toBinaryString(2048))); 00000000000000000000100000000000
(proFillZero(Integer.toBinaryString(4096))); 00000000000000000001000000000000
(proFillZero(Integer.toBinaryString(8192))); 00000000000000000010000000000000
(proFillZero(Integer.toBinaryString(-2))); 11111111111111111111111111111110
(proFillZero(Integer.toBinaryString(-4))); 11111111111111111111111111111100
(proFillZero(Integer.toBinaryString(-8))); 11111111111111111111111111111000
(proFillZero(Integer.toBinaryString(-16))); 11111111111111111111111111110000
(proFillZero(Integer.toBinaryString(-32))); 11111111111111111111111111100000
(proFillZero(Integer.toBinaryString(-64))); 11111111111111111111111111000000
(proFillZero(Integer.toBinaryString(-128))); 11111111111111111111111110000000
(proFillZero(Integer.toBinaryString(-256))); 11111111111111111111111100000000
(proFillZero(Integer.toBinaryString(-512))); 11111111111111111111111000000000
(proFillZero(Integer.toBinaryString(-1024))); 11111111111111111111110000000000
(proFillZero(Integer.toBinaryString(-2048))); 11111111111111111111100000000000
(proFillZero(Integer.toBinaryString(-4096))); 11111111111111111111000000000000
(proFillZero(Integer.toBinaryString(-8192))); 11111111111111111110000000000000
(proFillZero(Integer.toBinaryString(7))); 00000000000000000000000000000111
(proFillZero(Integer.toBinaryString(7 - 1))); 00000000000000000000000000000110
(proFillZero(Integer.toBinaryString(4))); 00000000000000000000000000000100
(proFillZero(Integer.toBinaryString(4 - 1))); 00000000000000000000000000000011
这里减1会导致退1位,所以再和原来的取&,肯定为0, 即(num & (num-1)) == 0
private static void findLoca(int num) {
boolean noSame;
if (num >= 0) {
noSame = (num & (num - 1)) == 0;
} else {
# 正数这个算法才好用,所以负数变成正数
noSame = (-num & (-num - 1)) == 0;
}
System.out.println(noSame);
}
⑥ 【useless】不用 for 循环找到二进制最右侧的1 → num & (-num)
已知 num
& num取反
= 0
,-num
= num取反+1
,
故num
& -num
→ num
& num取反+1
→ 最后一个不同的1
private static void findLoca1(int num) {
System.out.println(" 2进制 -1 : " + Integer.toBinaryString(-1));
System.out.println("10进制 " + num + " 数: " + num);
System.out.println(" 2进制 " + num + " 绝对值: " + proFillZero(Integer.toBinaryString(num)));
System.out.println(" 2进制 " + num + " 绝对值取反: " + Integer.toBinaryString(-num - 1));
System.out.println(" 2进制 " + (-num) + " 补码: " + Integer.toBinaryString(-num));
int location = (num & (-num));
// -num就是num取反+1得到的,num&num取反=0,num&-num→最后一个不同的1
System.out.println(proFillZero(Integer.toBinaryString(location)));
}
2进制 -1 : 11111111111111111111111111111111
10进制 500 数: 500
2进制 500 绝对值: 00000000000000000000000111110100
2进制 500 绝对值取反: 11111111111111111111111000001011
2进制 -500 补码: 11111111111111111111111000001100
00000000000000000000000000000100
⑦ 【useless】将二进制表示的最右侧的1置0 → num & (-num) ^ num
System.out.println("10进制 " + num + " 数: " + num);
System.out.println(" 2进制 " + num + " 绝对值: " + proFillZero(Integer.toBinaryString(num)));
System.out.println(" 2进制 " + num + " 绝对值取反: " + Integer.toBinaryString(-num - 1));
System.out.println(" 2进制 " + (-num) + " 补码: " + Integer.toBinaryString(-num));
int location = (num & (-num));
// -num就是num取反+1得到的,num&num取反=0,num&-num→最后一个不同的1
System.out.println(proFillZero(Integer.toBinaryString(location)));
int location1 = num & (-num) ^ num;
System.out.println(proFillZero(Integer.toBinaryString(location1)));
10进制 500 数: 500
2进制 500 绝对值: 000000000000000
2进制 500 绝对值取反: 1111111111111111
2进制 -500 补码: 11111111111111
00000000000000000000000000000100
00000000000000000000000111110000