Fun of the N-bit computing practical tips

EDITORIAL

If you think this article helpful to remember the point of a concern and a point like oh, thank you very much support.
For bit operations we should not be unfamiliar (Of course, if you are still unfamiliar, you can look at before I wrote articles on bit operation , very detailed), this one is mainly for the usual in our daily lives, encounter bit operation on some issues, and summed up some practical tips that can help us to quickly solve some issues, helps operate Fun-bit computing. This article will be updated in the future will continue to update you met a lot of whimsy to come, we also welcome message updates.

N strip practical skills

  • The bit operation, if we want the right-most bits 1 to 0, we only need ton & (n-1)

This is actually not difficult to understand, we know that, in binary, n - 1will lead to nthe far right 1to retire 1, as we have over a decade in decimal, ten back a little. Then this time with n & (n-1)computes, you can successfully remove the rightmost1

  • Calculating an integer number of binary 1
    since 1 can continue by x&(x-1)this erasing operation, so when the value becomes final 0when it is determined in the binary 1number

  • If converted into an integer of the integer A B, the number of bits required to change
    Consideration integer Aconvert B, if Aand Bat its i( 0<=i<32equal in) bits, there is no need to change the BIT is, if a first inot equal the bit, it is necessary bIT change this position. Therefore A and B is transformed to the number of different bits BIT. Legend has a bit XOR operation, is the same 0, is different 1, the problem then transformed into the number of A or B is calculated exclusive 1number

  • To consider the question, a number greater than a number of what it means? Or, how to judge a number greater than a number?

Whether in decimal or binary, there is the fact that we just looked from high to low, until a bit different, the size of the judgment out of it. Decimal, such as 6489...and 6486..., since 9 > 6, so no matter what position behind 6489...will be greater than 6486.... For binary, the same reason, but only because the binary number two 0and 1so when there is a bit larger than another, it must be 1 > 0. The image of a little more below,

m -> S S S 0 X X X X
n -> S S S 1 X X X X

Several front are the same, then a bit from the beginning 0to become 1. With this understanding, we can sometimes shift operation with the completion of many ingenious ideas.

  • Determine whether the number is not a power-of-two, we only need to (n & (n - 1))carry out operations, if the result is zero, so is the number two power

  • Determining a number of parity, we only need to i & 1carry out operations, if the result is 1compared with an even number, odd or vice versa

  • The same number of two exclusive-OR (^) 0

We can use this feature to XOR eliminate duplicate number, for example, to find out the number of a single array of Falling (for a number of odd number), on the contrary, we can also use this feature to find an array of unique pairs number.

  • Implemented using XOR exchange number
a = a ^ b;
b = a ^ b;
a = a ^ b;
  • Using the displacement implement a multiply / divide power of 2 times, i.e., to the right a number equivalent to a divided by 2, 2 bits to the right by 4, to the left is the multiplication, the same rule of addition.
int a = 3>>1; // a =1,注意此除法是整除, 不保留小数位
int a = 3<<1; // a = 6
int a = 3<<2; // a = 12
  • Modulo n-th power of 2 can be used m & (n - 1)operations, because, if it is 2a power, nmust 000100..., n-1 is 000011...., so do the calculation result retained in the n-m bits of non-zero range
  • We can ~n + 1to get nthe number of the opposite, of course, also be written as(n ^ -1) + 1
  • If the hit if(x == a) x = b, or if(x == b) x = amay be replaced by suchx = a ^ b ^ x
  • n multiple completion, when nfor the 2power of the time, (x + n - 1) & ~(n - 1)will find greater than the first xnumber, and it happens to be nan integer multiple.
  • Calculation n+1and n-1, -~n == n + 1, ~nits inverted negative sign '-' thereof and then negated and added 1. ~-n == n - 1, The idea is to find the least significant bit first 1, and it negated all the bits of the bit after also negated, that 01001000becomes 01000111.
  • Analyzing binary parity 1
x = x ^ (x >> 1);
x = x ^ (x >> 2);
x = x ^ (x >> 4);
x = x ^ (x >> 8);
x = x ^ (x >> 16);

cout << (x & 1) << endl; // 输出 1 为奇数

Decimal number 1,314,520, for example, its binary 0001 0,100,000,011,101,101 1000 Result of the first exclusive OR operation is as follows:

  0001 0100 0000 1110 1101 1000
^ 0000 1010 0000 0111 0110 1100
= 0001 1110 0000 1001 1011 0100

The result is a new binary number, wherein the number from the right in the i-th bit represents the number of the original has an odd number or an even number 1 on the i and i + 1 bits. For example, 0 indicates that the rightmost end of the two original numbers even number of 1, 1 from the right position on the third position would indicate that a number of the previous original has an odd number 1 position. The results of this number a second exclusive OR is as follows:

  0001 1110 0000 1001 1011 0100
^ 0000 0111 1000 0010 0110 1101
= 0001 1001 1000 1011 1101 1001

Lane 1 indicates the results of each of the three positions in front of the location and number of the original total of odd number 1, each of a total of an even number of 0 to 1 indicates four positions corresponding to the original number.

It has been done after the fifth XOR end, the last bit of the binary representation obtained on the entire 32-bit number in parity 1.

  • Remove the rightmost 1
int quyu(int pos){
    return pos & (~pos + 1);
} 

form

Features Examples Bit computing
Get rid of the last bit (101101->10110) x >> 1
In the last plus a 0 (101101->1011010) x << 1
In the last plus a 1 (101101->1011011) x << 1+1
The last bit becomes 1 (101100->101101) x | 1
The last bit becomes 0 (101101->101100) x | 1-1
Last negated (101101->101100) x ^ 1
The right number of the k-th bit becomes 1 (101001->101101,k=3) x
The right number of the k-th bit becomes 0 (101101->101001,k=3) x & ~(1 << (k-1))
From the right bit inversion k (101001->101101,k=3) x ^ (1 << (k-1))
Take the end of the three (1101101->101) x & 7
Taken at the end of k bits (1101101->1101,k=5) x & (1 << k-1)
K taken from the right place (1101101->1,k=4) x >> (k-1) & 1
The end of k bits into 1 (101001->101111,k=4) x | (1 << k-1)
K bit inversion end (101001->100110,k=4) x ^ (1 << k-1)
The right to become a continuous 0 (100101111->100100000) x & (x+1)
Right from the first 0 to 1 (100101111->100111111) x | (x+1)
The right side of a continuous 0 to 1 (11011000->11011111) x | (x-1)
1 take the right side of successive (100101111->1111) (x ^ (x+1)) >> 1
Remove from the right to the left of the first one (100101000->1000) x ^ (x ^ (x-1))
Published 131 original articles · won praise 685 · Views 1.19 million +

Guess you like

Origin blog.csdn.net/DBC_121/article/details/105175502