1. Determine whether it is 2 ^ n
! (X & (x-1 ))
2. Calculate the average value of two numbers
(X & y) + (x ^ y)> 1)
3. count the number of 1 contained in a number (converted to binary)
While (x) {cout ++; x = x & (x-1 );}
4. Compare the two numbers
Int c = a-B; char * strs [2] = {"a", "B"}; c = unsigned (c)> (sizeof (int) * 8-1); // 1B = 8b // convert the result to an unsigned type. If the result is negative, then 32nd bits // that is, the number of signed symbols is 1, and the opposite is 0. therefore, you can shift the slash (/) to the right to obtain the symbol bit and use it as the subscript of the result array. Cout <
5. Exchange the values of a and B without any intermediate variables.
(1) General Practice
Int a = 0x7ffffffe; int B = 0x7fffffff; cout <"a =" <
In the programmer interview book, one drawback is that when a = a + B, if a and B are large, they will be super-qualified! However, I don't know what super world can do. You can still get the correct result. Therefore, I think this method is good and has no disadvantages.
(2) bitwise operations
Int a = 0x7ffffffe; int B = 0x7fffffff; cout <"a =" <
In general, it is a good choice to use bitwise operations, and it is easy to remember. The problem comes from the programmer interview book