Bit computing beauty (collection)

A topic

An array in addition to some - but one of the figures, other figures appear twice. Please write a program to find the numbers that appear only.

Violence ideas: the use of digital recording number Map occur, occur once the last to find out.

Ingenious idea: using XOR symbol (^), exclusive OR symbol not carry adder, two identical numbers XOR to zero, it is possible to use this property to XOR all numbers again, the final answer is the answer.

	public static void main(String[] args) {
        int nums[] = {1,1,2,2,3,4,5,3,4};
        System.out.println(Arrays.toString(nums));
        int res = 0;
        for (int i = 0; i <nums.length ; i++) {
            res = res^nums[i];
        }
        System.out.println(res);
    }

Topic two

This number 1-1000 1000 in the array 1001 containing element, there is only one element value is repeated, all others only once. Each array element can only be accessed once, to design an algorithm to find it out; without an auxiliary storage space, the ability to design an algorithm?

Violence ideas: Use the number Map record number appears, and finally find two occurrences.

Ingenious idea: the first question and the like, using an exclusive or symbol (^), exclusive OR symbol not carry adder, two identical numbers XOR to zero, it is possible to use this property to find the answer.

    public static void main(String[] args) {
        int size = 5;
        int nums[] = new int[size+1];
        for(int i=0;i<size;i++){
            nums[i]=i+1;
        }
        nums[size]=(int)(Math.random()*size);
        System.out.println(Arrays.toString(nums));
        int tmp = 0;
        // 先对 tmp 从 1-size 做一下异或操作
        for (int i = 1; i <= size; i++) {
            tmp = tmp^i;
        }
        // 在对每一个数进行异或,相同的数字将变为,最终剩下的数字为答案
        for (int i = 0; i <= size ; i++) {
            tmp = tmp^nums[i];
        }
        System.out.println(tmp);
    }

Topic three

Please implement a function, an integer input, the output binary number represents the number 1.
Example: 9 is represented by binary 1001, there is a 2 1

Violence idea: converting numbers to binary number 1 of statistics by traversing the way.

	public static void main(String[] args) {
        int n = 9;
        String s = Integer.toBinaryString(n);
        System.out.println(s);
        char[] chars = s.toCharArray();
        int count=0;
        for (int i = 0; i < chars.length; i++) {
            if(chars[i]=='1'){
                count++;
            }
        }
        System.out.println(count);
    }

Clever idea: can use left (<<) to quickly sign the statistics, every movement left a binary number, add 0 at the end, if the int data type can get the answer by moving 32 times.

    public static void main(String[] args) {
        int n = 9;
        String s = Integer.toBinaryString(n);
        System.out.println(s);
        int count = 0;
        for (int i = 1; i <= 32 ; i++) {
            if((n&(1<<i)) == (1<<i)){
                count++;
            }
        }
        System.out.println(count);
    }

Title four

Analyzing a number is not an integer power of 2

Violence idea: Every time divided by 2, to determine whether the number of bits is an integer.

Ingenious idea: to this number will be a number with a result whether a Save operation is obtained is determined to know whether both 0 and is an integer.
For example: 8 (1000) ^ 7 (0111) = 0

	public static void main(String[] args) {
        int n1 = 8;
        int n2 = 9;
        System.out.println((n1&(n1-1))==0);
        System.out.println((n2&(n2-1))==0);
    }

Title five

The binary parity bit integer exchange

Violence idea: converting numbers to binary, and then pairwise exchange operations.

Ingenious idea: the original number (010101 ... 01) performs calculation to obtain a; and in the original array (1010 ... 10) performs calculation to obtain b; a left and then a right one for b XOR operation, it is finally obtained the results figures.

    public static void main(String[] args) {
        int n = 6;
        int a = n&0x55555555;//0101....01
        int b = n&0xaaaaaaaa;//1010....10
        System.out.println((a<<1)^(b>>1));
    }
发布了39 篇原创文章 · 获赞 20 · 访问量 1万+

Guess you like

Origin blog.csdn.net/cong____cong/article/details/104240916