Basic knowledge
For bit operations , we are familiar with the basic bit operations with (&&), or (| |), NON (!), XOR (&), and so on. In the interview often appear bit arithmetic related problems, so I did a simple collation, refer to a lot of written good blog and books, in this together thanked.
Now briefly, move the bitwise operation .
Left shift operation: x << y. Move x to the left Y-bit, discard X's leftmost y-bit, and fill y 0 on the right.
Right shift operation: x >> y. Move x to the right Y-bit, which requires a distinction between whether X is a signed number or an unsigned number. When x is an unsigned number, simply discard the rightmost y bit of x and fill y 0 on the left. When X is a signed number, it is divided by whether X is positive or negative. Positive numbers, with the same number of unsigned processing, negative, the x's rightmost y-bit is discarded, on the left side of the Y 1.
bit arithmeticTip 1, calculate the number of 1 in a binary
Determine whether the lowest bit is 1 by using the flag bit with the initial value of 1, and then shift the flag bit to the left to determine if the sub-low is 1; Keep counting until you have judged each one.
/* Calculate the number of binary 1 in the number of */int countOf1 (int num) {int count = 0;unsigned int flag = 1;while (flag) {if (num & flag) {count++;} Flag = Flag << 1;} return count;}
Another method, an integer minus one, can get the rightmost 1 of the integer to 0, and this 1 to the right of 0 becomes 1. With this integer and integer minus one, the rightmost 1 of the integer is changed to 0, and the remaining bits remain unchanged. Until the integer becomes 0, the number of times the operation is performed is the number of 1 in the integer.
/* Calculates the number of binary 1 in the number of */int countof1_2 (int num) {int count = 0;while (num) {num = num & (num-1); count++;} return count;}
2, judge whether a number is 2 of the n-th square
A number is 2 of the N-square, then the highest bit of this number is 1, the remaining bit is 0. It is easy to get a solution based on the second solution of the previous question. This integer and integer minus one and the operation, if the resulting result is zero, can prove that the number is 2 of the N-square.
/* Determines whether a number is 2 N (a number of 2 N, the highest bit is 1, the remaining bits are 0) */bool is2power (int num) {BOOL flag = True;num = num & (num-1);//Count num and num- 1 with the result if (num)//If the result is 0, then it is not 2 of the n-th party {flag = false;} return flag;}
3, Integer n How many steps can become an integer m
N and m differences or results can be learned two number of different bits of the number, and then call the calculation of a number of 1 of the method, you can get the results.
/* Solve n change to M, required steps */int countchange (int n,int m) {n = n ^ m;//For XOR of N and M, and 1 in the result of return countof1_2 (n);}
4, get the largest int value
/* Get the largest int to get the result: 2147483647*/int Getmaxint () {return (1 << 31)-1;} /* compile with g++, Warning:left shift count is Negative*/int getmaxint_2 () {return (1 <<-1)-1;} int Getmaxint_3 () {return ~ (1 << 31);} /* Use */int Getmaxint_4 () {return ((unsigned int)-1) >> 1 without knowing the length of int:}
5, get the smallest int value
is similar to getting the largest int method.
/* Minimum int to get result: -2147483648*/int Getminint () {return 1 << 31;} /* Also compiled under g++, appears warning:left shift count is Negative*/int getminint_2 () {return 1 <<-1;}
6, get the largest long
/* Ask for maximum long results: 9223372036854775807*/long Getmaxlong () {return ((unsigned long)-1) >> 1;}
7. Determine the parity of a number
The essence of judging parity is whether the last one is 1.
/* Determines the parity of a number. Returns 1, odd; returns 0, even */bool isodd (int num) {return num & 1 = = 1;}
8, Exchange two number (without the help of the third variable)
There are several ways to exchange two numbers without a third variable, such as a = a + B; b = a A; A = a-B. The following method can be implemented based on a number m with another number n xor, again with N xor, the resulting result is M.
/* does not apply temporary variable, exchange two number a = a ^ bb = b ^ aa = a ^ b*/void Myswap (int* a,int* b) {(*a) ^= (*b) ^= (*a) ^= (*b);}
9. To find the absolute value of a number
The base of the following method implementation is to shift n to the right by 31 bits to get the symbol N.
/* Take absolute N and move 31 bits to the right to get the n symbol. If n is positive, get 0, if n is negative, get -1*/int myabs (int n) {return (n ^ n >>)-(n >> 31);}
10, the average of two numbers
The first method is more general and simple, not much to say. The second method, need to know is that (m ^ n) >> 1 results are M and n one of the number of bits is half of the value of 1, M & n results are those bits of M and N are 1, two results are added to the average of M and N.
/* For the average of M and n */int getaverage (int m,int n) {return (M + N) >> 1;} /* For the average of M and N (m ^ n) >> 1, to get m and n two numbers in a number of bits 1 of the half M & N to get m and n two numbers of some bits */int getaverage_2 (int m,int n) {re Turn ((m ^ n) >> 1) + (M & n);}
11. Solving the relative problem of the inverse m-position
/* Gets the value of the penultimate m bit of n (counting from 1) */int getmthbytail (int n,int m) {return (n >> (m-1)) & 1;} /* Set the penultimate m bit of N to 1*/int setMthByTail21 (int n,int m) {return n | (1 << (m-1));} /* Set the penultimate m bit of N to 0*/int setMthByTail20 (int n,int m) {return N & ~ (1 << (m-1));}
General tips for bit arithmetic summary (01)