Usos de las operaciones AND bit a bit de Java

① &0 claro

Cuando se aplica AND a un número con un valor cuyos bits son todos 0, el resultado es cero

举例:
   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 valor sin cambios

举例:
   0000 0101 1010
&  1111 1111 1111 
---------------------
   0000 0101 1010

③ Según ① + ②

Obtenga el bit especificado de un número binario → 10101110 & 00001111 = 00001110
establezca el bit especificado de un número binario en 0 →10101110 & 00001111 = 00001110

举例:
   0000 0101 1010
&  0000 1111 0000
---------------------
   0000 0101 0000

④ Juzgar la paridad

二进制未位为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 ? "奇数" : "偶数"); // 偶数

⑤ Determinar si un número entero es una potencia entera de 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);
}

⑥ 【inútil】 No utilices el bucle for para encontrar el 1 en el extremo derecho del binario →num & (-num)

Conocido num& num取反= 0, -num= num取反+1,
entonces num& -numnum& num取反+1→ el último diferente1

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

⑦ [inútil] Establezca el 1 más a la derecha de la representación binaria en 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

Supongo que te gusta

Origin blog.csdn.net/weixin_37646636/article/details/132675518
Recomendado
Clasificación