Bit operation (top)

Novice Diary-January 18

Case 1:

Determine the unique paired number in the array

1~N-1 In an array of length N, only one element value is repeated, and the others only appear once.
Each array element can only be accessed once, and an algorithm is designed to find it out without auxiliary space.

*The first method
constructs a new array and performs an AND operation

First construct the array

public class 数组中确定唯一成对的数 {
    
    
    public static void main(String[] args) {
    
    
    int N = 11;		//N可自己自行设置
    int[] arr = new int[N];
    for (int i = 0; i < arr.length-1; i++) {
    
    
        arr[i] = i + 1;}

Take a random number for the last number and swap the position with the previous random element

	//最后一个数取随机数
    arr[arr.length-1] = new Random().nextInt(N-1)+1;
    //随机下标
    int index = new Random().nextInt(N);
    //最后一个数与前面随机一个数互换位置
    int t=arr[N-1];
    arr[N-1]=arr[index];
    arr[index]=t;
    System.out.println(Arrays.toString(arr));
    int x = 0;

Construct a new array and perform AND operation and output

	//构造一个1~N-1的数组
    for (int i = 0; i < N-1; i++) {
    
    
        x = x ^ (i + 1);
        }
    //新数组与旧数组作亦或运算,其余相同的数亦或为零
    //而成对的数加新数组里面那个数则有3个,所以亦或结果便是成对的那个数
    for (int i = 0; i < N; i++) {
    
    
        x ^= arr[i];
        }
    System.out.println(x);

*The second method

Auxiliary space solution (brute force)

int[] f = new int[N];
        for (int i = 0; i <N ; i++) {
    
    
            f[arr[i]] += 1;//原先每个元素都是0,现在给下标为arr[i]的每个都加1
                            //因为其中有两个数是一样的,所以会加两次
        }
        //System.out.println(Arrays.toString(f));
        for (int i = 0; i < N; i++) {
    
    
            //当新数组中某个元素等于2时,那么他的下标就是重复的那个数
            // (因为第一个元素被0占用了,所以下标不用加一)
            if(f[i]==2){
    
    
                System.out.println(i);
            }
        }
    }
}

Effect picture:

The result is different every time
Insert picture description here
Insert picture description here

Case 2:

Find the number of '1' in binary

Enter an integer and determine how many '1's are in its binary system

There are three solutions

1. Compare from right to left

public class 二进制中1的个数 {
    
    
    public static void main(String[] args) {
    
    
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        System.out.println(Integer.toString(N,2));
        //1.从右往左挨个比对是否为1
        int count1 = 0;
        for (int i = 0; i < 32; i++) {
    
    
            if ((N&(1<<i))==(1<<i)){
    
    
                count1++;
            }
        }
        System.out.println(count1);

2. Compare from left to right

	int count2 = 0;
        for (int i = 0; i < 32; i++) {
    
    
            if (((N>>i)&1)==1){
    
    
                count2++;
            }
        }
        System.out.println(count2);

3. Subtract 1 method.
By subtracting 1 from the input number and the original number, there are several 1

	int count3 = 0;
        //因为不知道要循环几次,所以用while
        while (N != 0){
    
    
            N = (N-1)&N;
            count3++;
        }
        System.out.println(count3);
    }
}

The following is a diagram of the subtraction method, which is a little scribble, don't mind.
Assuming that the number entered is 20, there are two 1s, then it will be the same as the two
Insert picture description here
renderings:
Insert picture description here

Case 3:

Is it a power of 2

Enter a number and judge whether it is an integer power of 2.
Thinking: When a number has and only one 1 in the binary digits, then it is an integer power of 2, for
example: 8 has only the fourth digit from right to left Is one, 64 is the 7th

public class 是不是2的整次方 {
    
    
    public static void main(String[] args) {
    
    
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int a = 2;
        if ((N & (N - 1)) == 0){
    
    
            System.out.println("yes");
        }
        else {
    
    
            System.out.println("no");
        }
    }
}

Effect picture:
Insert picture description here
Insert picture description here
Case 4:

Parity swap

Give an integer, swap its binary parity bit, and verify whether the swap is successful. For
example, the binary of 9 is: 1001, and the parity bit is swapped: 0110 equals 6.

public class 交换奇偶位 {
    
    
    public static void main(String[] args) {
    
    
        int a = 9;
        int b = huan(a);
        //验证换位是否成功,9奇偶位互换等于6
        int c = 6;
        System.out.println(b);
        if (c == b){
    
    
            System.out.println("yes");
        }
    }

    private static int huan(int i) {
    
    
    	//这里用二进制太长了,我用十六进制表示
        int ji = i & 0x55555555; //和1010 1010 1010 1010......做与运算取出奇数位
        int ou = i & 0xaaaaaaaa; //和0101 0101 0101 0101......做与运算取出偶数位
        return (ji << 1) ^ (ou >> 1); //合并成一个二进制数
    }
}

Illustration:
Assuming that the number to be exchanged is 9
Insert picture description here
renderings:
Insert picture description here

over!!!
The end of a cold and fulfilling day.

Guess you like

Origin blog.csdn.net/ghl971/article/details/112786217