Bitwise AND Operator (&)
To participate in the operation of the two data, press bits for the "and" operation.
Operation Rules:0&0=0; 0&1=0; 1&0=0; 1&1=1;
That is: two digits at the same time "1", the result is "1", otherwise 0
For example: 3&5 is 0000 0011& 0000 0101 = 00000001 Therefore, 3&5 is worth 1.
In addition, negative numbers participate in bitwise AND operation in the form of complement.
Special uses for "and operations":
(1) Clear zero. If you want to zero a unit, even if all of its bits is 0, the result is zero as long as it is zero with one of you.
(2) To take a number of the middle finger positioning
Method: Find a number, corresponding to the x to take the bit, the number of the corresponding bit is 1, the rest of the zero, this number and X to "and operation" to get X in the position of the finger.
Example: Set x=10101110,
Take the lower 4 digits of x, with x & 0000 1111 = 00001110;
It can also be used to take 2, 4, 6 bits of x.
Bitwise OR operator (|)
Two objects that participate in the operation, press bits for the OR operation.
Operation rules: 0|0=0; 0|1=1; 1|0=1; 1|1=1;
That is, the two objects that participate in the operation have a value of 1, as long as they have one 1.
For example: 3|5 00000011 | 0000 0101 = 00000111 Therefore, the 3|5 is worth 7.
In addition, negative numbers participate in bitwise OR operations in the form of complement.
"OR operation" special effects:
(1) often used for a data of some position 1.
Method: Find a number that corresponds to the bit where x is to be placed 1, the corresponding bit is 1, and the remaining bits are zero. This number is in the X phase or can make some position in X 1.
Example: the lower 4 position of the x=10100000 1, with X | 0000 1111 = 1010 1111 can be obtained.
xor operator (^)
To participate in the operation of the two data, press bits for the "XOR" operation.
Operation rules: 0^0=0; 0^1=1; 1^0=1; 1^1=0;
That is, two objects that participate in the operation, if two corresponding bits are "different" (the value is not the same), the bit result is 1, otherwise 0.
Special effects of "XOR or operation":
(1) to make a specific bit of flip to find a number, corresponding to the X to flip the people, the number of the corresponding bit is 1, the rest of the bit is zero, this number and x corresponds to a bit different or can.
Example: x=10101110, so that x low 4-bit flip, with x ^0000 1111 = 1010 0001 can be obtained.
(2) differ from 0 or, retain the original value, X ^ 00000000 = 1010 1110.
The next point is to say a bit different or, different or actually is not carry addition, such as 1+1=0,,0+0=0,1+0=1.
A few of the properties of an XOR:
1. Exchange Law
2. Binding law (IE (a^b) ^c = = a^ (b^c))
3, for any number x, there are x^x=0,x^0=x 4, reflexive: a^b^b=a^0=a;
XOR is most common in polynomial division, but its most important property is reflexivity: A xor b XOR B = A, that is, the given number a, with the same operation Factor (B) two times the XOR, and still get a itself. This is a magical property that utilizes this property to get many interesting applications. For example, all of the program textbooks will point out to beginners that in order to exchange the values of two variables, an intermediate variable must be introduced. But if you use XOR, you can save a variable's storage space: with A,B Two variables and stored values of A,B, the following three-line expressions interchange their value expressions (values):
A=a^b;
B=b^a;
A=a^b; left-shift operator (<<)
Shifts the bits of an operation object all left several bits (bits on the left, the right 0).
Example: a = a<< 2 moves the bits of a to the left 2 digits, the right complement 0,
Left 1 digits after a = a *2;
If the left-move high does not contain 1, then move each left one bit, which is the equivalent of multiplying by 2. Right shift operator (>>)
The bits of one number is shifted all the right several digits, the positive left is 0, the negative is 1, and the right is discarded.
The operand is shifted one bit to the right, equal to the number divided by 2.
For example: a = a>> 2 moves the bits of a to the right 2 digits,
A left complement of 0 or 1 depends on whether the number of shifts is positive or negative.
Compound assignment operator
bitwise operators, combined with assignment operators, form a new composite assignment operator, which is:
&= Example: a &=b equivalent to a=a& b
|= Example: a |=b equivalent to A=a |b
>>= Example: a >>=b equivalent to a=a>> b
<<= Example: a<<=b equivalent to a=a<< b
^= Example: a ^= b equals a=a^ b
Rule of operation: similar to the operation rules for the composite assignment operator mentioned earlier. bitwise operation of data with different lengths
If two different lengths of data are bitwise-aligned, the system aligns the two at the right end and then bitwise operations.
Take the "and" operation as an example to illustrate the following: We know that in the C language, Long is 4 bytes, int is 2 bytes, if a long data and an int data is "and", after the right side is aligned, the left insufficient bit is made up in the following three cases,
(1) If the integer data is positive, the left is 16 0.
(2) If the integer data is negative, fill 16 1 on the left.
(3) If the shaping data is unsigned, the left also complements 16 0.
such as: Long A=123;int b=1, calculated a& B.
such as: Long A=123;int b=-1, calculated a& B.
such as: Long a=123;unsigned intb=1, calculate A & B.