Bit operations-Xiaobai's introductory study notes

1. Symbol recognition:

&——Bitwise AND
|——Bitwise OR
^——Bitwise XOR

2. Basic method:

x&(x-1) Eliminate the last digit 1
x&(x-1)==0 Determine whether it is a power of 2 (the power of 2 has only one 1, and it becomes 0 if it is removed)
while(x!=0) {x = x&(x-1);b++;} Determine how many 1
Sample code 1:

import java.util.Scanner;
//常用方法
public class WeiYunSuan {
    
    
	
	public static void F0(int x) {
    
           //判断奇偶
		if((x&1)==1)
			System.out.println(x+"是奇数");
		else System.out.println(x+"是偶数");
	}

	public static int F(int x) {
    
             //消去最后一位1
		int k = x&(x-1);
		return k;
	}
	
	public static int F1(int x) {
    
            //判断有多少个1
		int b = 0;
		while(x!=0) {
    
    
		x = x&(x-1);
		b++;
		}
		return b;
	}
	
	public static void F2(int x) {
    
           //判断是否是2的幂次数(含有一个1)
		int z=x;
		int b = 0;
		while(x!=0) {
    
    
		x = x&(x-1);
		b++;
		}
		if(b==1)
			System.out.println(z+"是二的幂次数");
		else System.out.println(z+"不是二的幂次数");
	}
	
	
	public static void main(String[] args) {
    
    
		Scanner in = new Scanner(System.in);
		int a = in.nextInt();
		F0(a);
    	int k = F1(a);
    	System.out.println(a + "的二进制中1有:"+k+"个");
    	F2(a);
		
//		System.out.println(k);
	}
}

x ^ a^a=x XOR formula application:

Application 1:

In the array, only one number appears once, and the rest appear twice. Find the one that appears once

Problem-solving ideas:

XOR all the values ​​of the array, the same number will be offset, and the rest is just one number

Source code:

//应用一 数组中,只有一个数出现一次,剩下都出现三次,找出出现一次的
public class WeiYunSuan2 {
    
    
public static void main(String[] args) {
    
    
	int a[]= {
    
    1,2,2,3,3,4,4,1,5};
	int n=0;
	for(int i=0;i<a.length;i++)
	{
    
    
		n =n^a[i];
	}
	System.out.println(n);
}
}

Application 2:

In the array, only one number appears once, and the rest appear three times. Find the one that appears once

Problem-solving ideas:

Use the nth bit of a b array to access the nth bit of each number in the array. After the access is completed, each item in the b array is judged whether it is a multiple of 3. If it is not, it means that the digit of the number we are looking for is 1.

Source code:


public class WeiYunSuan3 {
    
    
	//数组中,只有一个数出现一次,剩下都出现三次,找出出现一次的。
	public static int F(int a[]) {
    
    
		int bit[] = new int [4];
		int result = 0;
		for(int i=0;i<a.length;i++) 
		{
    
    	for(int j=0;j<4;j++) 
				bit[j]+=((a[i]>>j)&1);
			}
		
		for(int k=0;k<4;k++) {
    
    
			if(bit[k]%3==1) result+=1<<k;
		}
		return result;
	}
	
public static void main(String[] args) {
    
    
	int a[]= {
    
    1,3,3,2,2,2,3,4,4,4,1,1,5};
	int k = F(a);
	System.out.println(k);
}
}

Application 3:

In the array, only two numbers appear once, and the rest appear twice. Find the two numbers x and y that appear once

Problem-solving ideas:

Similar to application 1, but now the number obtained after all XORing is the number res of x^y, this number is looped to find the last position of 1, where x and y are different, according to this position is 1. Or 0 separates the arrays, x and y are in different arrays, and then applies one to any array to get x, and then x ^ res gets y.

Source code


public class WeiYunSuan4 {
    
    
	//应用三 数组中,只有两个数出现一次,剩下都出现两次,找出出现一次的
	
	public static void F(int a[]) {
    
    
		int bit[] = new int [4];
		int result = 0,pos=0;
		int x=0,y=0;
		for(int i=0;i<a.length;i++) 
	    result = result^a[i];
		System.out.println(result);
		int temp=result;
		while((temp&1)==0) {
    
    
			pos++;
			temp>>=1;
		}
		System.out.println(pos);
		for(int i=0;i<a.length;i++) {
    
    
			if(((a[i]>>pos)&1)==1)
				x^=a[i];
		}
		y=x^result;
		System.out.println(y+" "+x);
		
		return ;
	}
	
public static void main(String[] args) {
    
    
	int a[]= {
    
    3,3,2,2,6,4,4,1,1,5};
	F(a);
	
}
}

2020.11.10–21.53

Guess you like

Origin blog.csdn.net/weixin_44919936/article/details/109607995