The algorithm is beautiful (1)---bit operation

1. Find the number placed in the array:

In an array, except for a certain number that only appears once, other numbers appear twice (only twice). Please write a program without auxiliary space, traverse the array once to find out this number

Idea: Use the conclusion that the XOR between the same numbers is 0, and the XOR between different numbers is 1, so that all the numbers in the array are XORed one by one.

public class Hello {
    
    
    public static void main(String[] args) {
    
    
        int arr [] = new int [11];
        for(int i = 0;i<5;i++){
    
    
            arr[i] = i;
            arr[i+5] = i;
        }
        arr[10] = 6;//这个数是落单的,问怎么找出这个数
        int a = arr[10];
        for(int i = 0;i<10;i++){
    
    
            a = a ^ arr[i];
        }
        System.out.println(a);
    }
}

2. How many ones are there in a binary number?

Ideas:
1. You can use the previous shift operation to move all 32 bits by 1 to perform XOR with 1. The result is 1, which means that the bit is 1, and then this number is counted, which is the number of 1. Count
2. Subtract one from a number, then the 1 on the first binary digit with 1 from right to left becomes 0, and the 0 on the right becomes 1, and then do the & operation with yourself to make the binary on the right The number in the position and the number in the current position all become 0, and proceed in sequence until the number is equal to 0. Note down the number of operations, which is the number of 1.

public class Hello {
    
    
    public static void main(String[] args) {
    
    
    //方法1
      int count = 0;
      int b = 335;//这个数的二进制有多少个1?
      int i = 0;
      for(i = 0;i<32;i++){
    
    
          if(((b>>>i)&1) == 1){
    
     //这是移位的操作
              count++;
          }
      }
        System.out.println(count);
      //方法2
        int count1 = 0;
        while(b!= 0){
    
    
            b = (b-1)&b; //用他减1根他自己取异或
            count1++;
        }
        System.out.println(count1);
    }

}
  • If an integer (not including negative numbers) is a binary integer power, there is at most one 1 in its binary digit.

3. Swap the parity bits of integers:

Idea: An integer and a binary representation of (1010 1010...) and (0101 0101...) are respectively ANDed, and then the former is shifted to the left by one bit, the latter is shifted to the right by one bit, and then the exclusive OR operation can be performed. got the answer

public class Hello {
    
    
    public static void main(String[] args) {
    
    
        //将整数的奇偶位互换
        int c = 6;
        int a = 0xaaaaaaaa;//写成十六进制形式 这是1010 1010 ... 1010
        int b = 0x55555555;//同理           这是0101 0101 ... 0101
        c = ((c&a)>>1)^((c&b)<<1);
        System.out.println(c);
    }

}

Fourth, the binary representation of floating-point real numbers between 0-1

  • Given a real number between 0-1, such as 0.625, type double, print his binary representation 0.101,
  • Because the binary after the decimal point means 0.5, 0.25, 0.125...
  • If the number cannot be accurately represented in binary within 32 bits, an Error will be output

Idea: Multiply this number by 2. If it is greater than 1, then fill in the digit with 1, and then subtract 1, otherwise fill in 0.

public class Hello {
    
    
    public static void main(String[] args) {
    
    
        double a = 0.625;
        int count = 0;
        double b;
        String two = "0.";
        while(count < 32){
    
     //超过位数就跳出并打印ERROR
            if((a * 2) >= 1){
    
      //即是上述操作
                a = a * 2 - 1;
                two += "1";
            }else {
    
    
                a = a * 2;
                two += "0";
            }
            if(a<=0){
    
    
                break;
            }
            count++;
        }
        if(count<32){
    
    
        System.out.println(two);
        }else{
    
    
            System.out.println("ERROR");
        }
    }

}

Five, k times and once

Only one number appears once in the array, and all other numbers appear k times. Please output the number that appears once:

Ideas

  • The result of adding k identical k-ary numbers without carry is 0.
  • First, change the numbers into k-ary numbers (take k=3 as an example below)
  • No carry addition
  • Then turn the numbers back
public class Hello {
    
    
    public static void main(String[] args) {
    
    
        int [] arr = {
    
    2,2,2,9,7,7,7,3,3,3,6,6,6,0,0,0};
        int len = arr.length;//计算整个数组的长度,以便开辟二维数组
        char [][] kRadix = new char[len][];//开辟二维数组存储3进制的形式
        int k = 3;//设定为3进制

        int maxlen = 0;//不知道每个数转成对应的三进制具体有多长
        //转成k进制字符数组,对于每个数字
        for(int i = 0;i<len;i++){
    
    
            kRadix[i] = new StringBuilder(Integer.toString(arr[i],k)).reverse().toString().toCharArray();
            if (kRadix[i].length > maxlen)
                maxlen = kRadix[i].length;//要最长的三进制,以便好进行不进位加法
        }
        int [] resArr = new int[maxlen];
        for(int i = 0;i<len;i++) {
    
    //每个数字都要进行
            for (int j = 0; j < maxlen; j++) {
    
    
                if (j >= kRadix[i].length)
                    resArr[j] += 0;//如果长了,那就加0啦
                else {
    
    
                    resArr[j] += (kRadix[i][j] - '0'); //从char转换成数字
                }
            }
        }
        int res = 0;
        for(int i = 0;i<maxlen;i++){
    
    
             res += (resArr[i] % k) * (int)(Math.pow(k,i));//3的多少次
        }
        System.out.println(res);
    }
}

Guess you like

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