The algorithm is very beautiful and the notes are organized (the pilot course 1-bit operation some applications)

Some applications of bit operations (Blue Bridge College)

Reference

Detailed explanation of bit operation and shift operation in java

Supplement one knowledge

1.正数的源码是他的反码和补码
2.负数的反码是将他源码除符号位进行按位取反
3.负数的补码是将他反码加1得到的
//后面的运算都建立在补码之上
4.按位与 & : 两个相应的二进制对应,同为1是1,否则为0
5.按位或 | :有1为1,否则为0
6.异或 ^ : 相同为零, 否则为1
7.<<: 左移运算, 右边空出的位用0填补, 高位左移溢出则舍弃该高位
8.>>:右移运算, 左边空出的位,正数用0填补,负数用1填补。注:不同的环境填补方式可能不同;低位右移溢出则舍弃该位。
9.>>> : 无符号右移, 正数与右移规则一样,负数的无符号右移,就是相应的补码移位所得,在高位补0即可

1. Determine whether it is odd or even

The bitwise AND is used here, and the bitwise AND with 1, the previous numbers are all 0, and if the last number is the same as 1, it is an odd number (the value of bitwise AND is 1), if the last number is different from you , It is an even number (the value of bitwise AND is 0)

import java.util.Scanner;

public class Hello {
    
    
    public static void main(String[] args) {
    
    
    //判断一个数是奇数还是偶数
        Scanner sc = new Scanner(System.in);
        System.out.println("输入一个数");
        while(true) {
    
    
            int a = sc.nextInt();
            System.out.println(a + "是:" + (((a & 1) == 0) ? "偶数" : "奇数"));
        }
    }
}

Second, determine the number on the binary digit

Same, the bitwise AND technique is used here

import java.util.Scanner;

public class Hello {
    
    
    public static void main(String[] args) {
    
    
    //获取二进制位是0还是1,这里指的是补码
        Scanner sc = new Scanner(System.in);
        System.out.println("输入一个数");
        while(true) {
    
    
            int a = sc.nextInt();
            System.out.println(a + "第5个二进制位是:" + ((((a>>4) & 1) == 0)?0:1));//这里取第5个(从右往左)
        }
    }
}

Three, exchange two numbers

Here we need to understand a little bit of knowledge, one's exclusive OR is 0 for oneself, and one's exclusive OR is 0 for oneself

import java.util.Scanner;

public class Hello {
    
    
    public static void main(String[] args) {
    
    
    //使用异或技巧交换两个数
        Scanner sc = new Scanner(System.in);
        System.out.println("输入两个数");
        while(true) {
    
    
            int a = sc.nextInt();
            int b = sc.nextInt();
            a = a ^ b;
            b = a ^ b;//把上面的东西带进去, b^b为0, a^0为a
            a = a ^ b;//把上面的东西带进去, a^a为0, b^0为b
            System.out.println(a + " " + b);
        }
    }
}

Four, find the absolute value of a number

//Thinking: a number first shifts its sign to the right, it will shift the sign to all positions, and then perform an exclusive OR operation with itself, the complement of a positive number remains unchanged, and the complement of a negative number is equivalent to inversion

//Finally, we are adding one, and only the sign bit is changed at this time. It is the same as the idea of ​​converting the original code to the complement. You can try to convert the original code twice, and it will return to the original situation.

import java.util.Scanner;

public class Hello {
    
    
    public static void main(String[] args) {
    
    
    //求绝对值
        Scanner sc = new Scanner(System.in);
        System.out.println("输入一个数");
        while(true) {
    
    
            int a = sc.nextInt();
            System.out.println((a ^ (a>>31)) + (a>>>31));//注意区分无符号移动和有符号移动

        }
    }
}

Guess you like

Origin blog.csdn.net/qq_45911278/article/details/112623125