Several common bit operation

1, determines the number of parity

If a number n represented as a binary number, we just need to determine the last bit is a binary 1 or 0 can be. If it is 1, it represents an odd number, otherwise even. code show as below:

if(n & 1 == 1){
    // n是奇数
}

2, the exchange of two numbers

x = x ^ y; // (1)
y = x ^ y; // (2)
x = x ^ y; // (3)

We all know the result after the same number of two XOR to zero, i.e. n ^ n = 0, and equal to itself, i.e. n ^ 0 = n 0 after any number of exclusive OR.

So we put (1) x is substituted into (2) the x, there is: y = x ^ y = (x ^ y) ^ y = x ^ (y ^ y) = x ^ 0 = x, so that x is value is assigned to the y.

For (3), derived as follows: x = x ^ y = (x ^ y) ^ x = (x ^ x) ^ y = 0 ^ y = y, so that the value of y assigned to the x.

Commutative law XOR operation support operations and associativity.

3, find no duplicate number

Give you a set of integer data, these data, which has a number only appears once, other numbers appeared twice, allowing you to find a number

This question many people may be stored with a hash table, each time stored record number of times a number appears next, and finally traversing the hash table only to find there have been times once. In this manner the time complexity is O (n), the spatial complexity is also O (n) a.

In fact, this question can also be bit arithmetic ,, we can put this group of integers or all different, because the same number of two XOR result is 0 0 XOR with a number of results for itself, so XOR result obtained will appear only as a number. This data is for example: 1, 2, 3, 4, 5, 1, 2, 3, 4. Of which five occurred once, others have suffered twice and took them all XOR, the result is as follows:

1 ^ 2 ^ 3 ^ 4 ^ 5 ^ 1 ^ 2 ^ 3 ^ 4 = (1 ^ 1) ^ (2 ^ 2) ^ (3 ^ 3) ^ (4 ^ 4) ^ 5 = 0 ^ 0 ^ 0 ^ 0 ^ 5 = 5

code show as below:

int find(int[] nums){
    int tmp = nums[0];
    for(int i = 1;i < nums.length; i++)
        tmp ^= arr[i];
    
    return tmp;
}

4,2 n-th power

Solution 2 ^ n, and can not use the system comes function pow

Many people see this problem may feel let multiplying the n 2 on the line, if to do so, time complexity is O (n) a. So how to do it with a bit operation?

For example n = 13, n is the binary number 1101, then the 13-th power of 2 can be disassembled: 2 ^ 1101 ^ 0001 = 2 * 2 * 2 ^ 0100 ^ 1000. We can >> 1 & 1 and 1101 read bit by bit, this bit is 1 multiplier that multiplies the representative of the final result. The final code is as follows:

int pow(int n) {
    int sum = 1;
    int tmp = 2;
    while(n != 0) {
        if(n & 1 == 1)
            sum *= tmp;
        
        temp *= temp;
        n >>= 1;
    }
    return sum;
}

5, not greater than N to find the maximum exponent 2

For example, N = 19, then is converted into binary 00010011 (For convenience, I use to represent 8-bit binary). So we are looking for is the number, the binary leftmost 1 is reserved, behind all of 1 to 0 . That is our target number is 00010000. So how do get this number? Solution corresponding follows:

1. Find the leftmost 1, and then put it on the right becomes all 0 1

2, the obtained value by 1, i.e., can be obtained 00100000 00011111 + 1 = 00100000.

3, to get a move to the right 00100000, 00010000 can be obtained, namely 1 = 00100000 >> 00010000.

So the question is, the first step to the back of the leftmost 0 1 1 into the how to get it?

code show as below:

n |= n >> 1;
n |= n >> 2;
n |= n >> 4;

And n is done by the right or the operation can be obtained. After I explain it, we assume that the leftmost 1 is the first k-bit binary bits (from left to right number), then the n right one, then the result of the first k + 1 bits are bound to 1 , and n are the result of the operation to the right or to do, then the result obtained in the k-th and k + 1 bit must be 1; the same token, the results again shifted two bits to n, then the obtained k-th + 2 and k + 3 Wei must be 1, and then again or do arithmetic, then we can get the first k, k + 1, k + 2, k + 3 are 1, and so forth down ....

The final code is as follows:

int findN(int n){
    n |= n >> 1;
    n |= n >> 2;
    n |= n >> 4;
    n |= n >> 8 // 整型一般是 32 位,上面我是假设 8 位。
    return (n + 1) >> 1;
}

The time complexity of this approach is approximately O (1).

Guess you like

Origin www.cnblogs.com/kyoner/p/10964181.html