Here are some common applications of bit operations to make a summary, there are judgment parity, exchange two number, transform symbols and seek absolute value. These tips are easy to remember and should be mastered.
1. Judging odd and even
As long as the least bit is 0 or one decision, 0 is an even number, 1 is an odd number. You can therefore use if (A & 1) = = 0) instead of if (a% 2 = = 0) To determine if a is an even number.
The following program will output all odd numbers between 0 and 100.
for 0 ; + +i )if1) printf ("", i); Putchar ('\ n');
2. Exchange two number
The general wording is:
void Swap (intint &b) { if (a = b) { int c = a;< c11/>= b; = c; }}
You can use a bitwise operation to implement a two number exchange without a third-party variable:
void Swap (intint &b) { if (A! = b ) {^= b; ^= A; ^= b; }}
This can be understood as:
The first step a^=b namely a= (A^B);
The second step b^=a namely b=b^ (a^b), because ^ arithmetic satisfies the commutative law, b^ (A^B) =b^b^a. Because a number and its own XOR result is 0 and any number with 0 XOR will not change, so at this point B is assigned a value.
The third step a^=b is a=a^b, because the previous two steps to know A= (a^b), b=a, so A=a^b is a= (a^b) ^a. So a will be assigned the value of B.
Let's take another example to deepen the impression. int a = +, B = 6;
The binary of A is 13=8+4+1=1101 (binary)
b binary is 6=4+2=110 (binary)
The first step a^=b a = 1101 ^ 110 = 1011;
The second step b^=a b = 110 ^ 1011 = 1101; i.e. b=13
The third step a^=b a = 1011 ^ 1101 = 110; i.e. a=6
3. Transform symbols
A transform symbol is a positive number and a negative number becomes a positive number.
For 11 and 11, you can change-11 to 11 by the following transformation method
1111 0101 (binary) – Reverse, 0000 1010 (binary) – Add 1-> 0000 1011 (binary)
It's also possible to turn 11 into a-11
0000 1011 (binary) – Reverse, 1111 0100 (binary) – Add 1-> 1111 0101 (binary)
So the transform symbol only needs to be reversed and added 1. The complete code is as follows:
#include <stdio.h>intSignreversal (inta) { return~a +1;}intMain () {printf ("the integer transform symbol---by morewindows (http://blog.csdn.net/MoreWindows)---\ n"); intA =7, B =-12345; printf ("%d%d\n", Signreversal (a), signreversal (b)); return 0;}
4. Seek absolute value
Bit operations can also be used to find the absolute value, for negative numbers can be reversed by adding one to get a positive number. Yes-6 can do this:
1111 1010 (binary) – Take reverse->0000 0101 (binary)-plus 1-> 0000 0110 (binary)
To get 6.
So first shift to take the sign bit, int i = a >> 31, note If a is positive, I equals 0, is negative, I equals-1. Then I is judged-if I equals 0, return directly. No, return to ~a+1. The complete code is as follows:
int my_abs (int a) { int; return 0 1 );}
Now analyze it again. For any number, with 0 XOR will remain unchanged, and-1 i.e. 0xFFFFFFFF XOR is equivalent to inversion. Therefore, a and I xor minus I (because I is 0 or-1, so minus I is either plus 0 or 1) can also get the absolute value. So you can optimize the above code:
int my_abs (int a) { int; return ((a ^ i)- i);}
Note that this method does not use any judgment expression, and some of the interview questions are required to do so, so it is recommended that the reader remember the method (^_^ should be better remembered later).
Common bit Operation tips