2. Operation rule: 0&0 = 0; 0 & amp 1=0; 1。 0=0; 1。 1= 1; That is, if both digits are "1", the result is "1", otherwise it is 0, and if there is 0, it is 0.
For example: 3&; 5= 1, that is, 00000011&; 0000 0 10 1 = 0000 000 1
3. Special purpose and operation:
(1) reset. If you want to clear a cell, even if all its binary bits are 0, as long as it is combined with a value of 0, the result is 0.
(2) take a number to locate. Find a number corresponding to the bit to be taken by X. The corresponding bit of this number is 1, and the rest bits are zero. The bit specified in X can be obtained by AND operation of this number and X. ..
Example: let x =101110, take the lower four digits of x and use x & 000011/. It can also be used to take the 2nd, 4th and 6th bits of x.
Second, according to the bit or (|)
1. Concept: Two objects participating in the operation are OR-operated in binary bits, and negative numbers participate in bitwise AND-operated in the form of complement.
2. Operation rule: 0 | 0 = 0; 0| 1= 1; 1|0= 1; 1| 1= 1; That is, as long as one of the two objects involved in the operation is 1 and its value is 1, then 1 is 1.
For example: 3|5=7, that is, 00000011| 0000 01= 0000 01.
3. The special function of 3.OR operation:
(1) is often used to check some positions of a data. Find a number 1 corresponding to x, the corresponding bit of this number is 1, and the rest bits are zero. This number is in phase with x or can be placed in x 1.
Example: take the low order of X= 10 100000 as 1, and use x | 00001165438 =10.
Three. XOR operation ()
1. Concept: Two data involved in the operation are XOR-ed according to binary bits.
2. Operation rule: 0 0 = 0; 0^ 1= 1; 1^0= 1; 1^ 1=0; That is, if two corresponding bits of two objects participating in the operation are XOR (with different values), the result of the bit is 1, otherwise it is 0 and 0 and 1.
For example, 3 5 = 6, that is, 0000001kloc-01= 0000010.
3. The special function of XOR operation:
(1) Flip a specific bit and find the number corresponding to each bit to flip X. The corresponding bit of this number is 1 and the rest bits are zero. This number can only be XOR with the corresponding bit of x.
(2) If it is different from 0, keep the original value, x00000000 =1011165438.
Example: x =1010110, flip the lower four digits of x and use x000011=/kloc.
(3) Based on the XOR operation, the values of two variables are exchanged, and the new variable is not referenced.
a = a ^ b; b = a ^ b; a = a ^ b;
Also based on addition and subtraction, there are: a = a+b; b = a-b; a = a-b;
Fourth, perform bit operations on data with different lengths.
If two data with different lengths are bitwise operated, the system will align them at the right end and then bitwise operated.
Taking AND operation as an example, the explanation is as follows: We know that in C language, long type takes up 4 bytes and int type takes up 2 bytes. If data of type long and data of type int are AND-operated, after the right end is aligned, the insufficient bits on the left side are supplemented according to the following three situations.
(1) If the integer data is positive, add 16 zeros to the left.
(2) If the integer data is negative, add 16 1 to the left.
(3) If the plastic data is unsigned, 16 zeros are added to the left.
Such as: Dragon A =123; int b = 1; Calculate a &;; B.
Such as: Dragon A =123; int b =- 1; Calculate a &;; B.
Such as: Dragon A =123; Unsigned int b =1; Calculate a &;; B.