InC ProgrammingIn, the bit of data is the smallest unit of data that can be operated. In theory, you can use the bitwise operation to complete all the operations and operations. AverageBit operationIt is used to control hardware or perform data transformation. However, flexible bit operations can effectively improve the efficiency of program running.
1. bitwise operators in C Language
Because the C language is designed to replace the assembly language, it must support the computing power of the assembly language, so the C language supports all Bitwise Operators ). Bit operations are used to test, set, or shift the bit (bit) in a byte or word. In programming a microprocessor, bit operations are especially suitable for registers and I/O Ports. Therefore, this section will give a more detailed introduction to this.
The form and meaning of the six bit operators are as follows:
&: AND );
|: Bitwise "OR" (OR );
^: The bitwise "XOR" (XOR );
~ : "Reverse" (NOT );
": Right Shift of data;
: Shift Data left;
1) bitwise "and" Operation
The bitwise AND operator is used to perform the bitwise AND operation on both sides of the operator expressed in binary. This operation is the same bit in the number). The operation rule is: when both operands are 1, the output result is 1; otherwise, it is 0.
For example:
A = 0x88, B = 0x81, then the calculation result of a & B is as follows:
0x88 1000 1000 a count
& 0x81 1000 0001 B count
= 1000 0000
The & operator allows the numbers 0x88 and numbers 0x81 to have one and one, two, and two digits ...... The seven and the seven are respectively "and ". The operation rule of the "and" operation is that if one of the two operands is 0, the corresponding bit in the result is 0. In the number a and number B, only the highest bit (7th bits) is 1, so the result of this bit is 1, and all other results are 0.
Usually we can use the bitwise "and" operation as a means to close a certain (that is to say 0), for example, we want to disable the 3rd bits in number, without affecting the status of other bits, you can use a number 0xF7, that is, the binary number 1111 0111 to perform the bitwise "and" operation with the number:
0x88 1000 1000 a count
& 0xF7 1111 0111 shielding count
= 1000 0000
Note: except that the number of 3rd bits is 0, all others are 1. The operation result only sets the 3rd position in the number of a to 0, while the other bits in the number of a are not affected. That is to say, if you want to disable the nth digit Of a certain number, you only need to set the nth digit to the other digit. The other digit is 1 except that the nth digit is 0, to block others.
The preceding operation can be expressed by a = a & (0xF7) or a & = (0xF7. The functions of these two expressions are the same (see the "Composite assignment operator" section in the previous section), but the second form is often seen in the source code.
2) bitwise "or" Operation
Bitwise "or" operator | performs bitwise "or" operations on both sides of the operator expressed in binary. This operation is the same bit in the number). The operation rule is: when both operands are 0, the output result is 0; otherwise, the result is 1.
For example:
A = 0x88, B = 0x81, then the calculation result of a | B is as follows:
0x88 1000 1000 a count
| 0x81 1000 0001 B count
= 1000 1001
Usually we can use the bitwise "and" operation as the means to set the position (I .e., the position 1). For example, we want to set the 0th bits in the number of a and the 1 position 1, without affecting the status of other bits, you can use a number 0x03, that is, the binary number 00000011 to perform the bitwise "or" operation with the number:
0x88 1000 1000 a count
| 0x03 0000 0011 shields
= 1000 1011
Note that, except for 0th and 1, all others are 0. The operation result only sets the 0th and 1 positions in, the other bits of the number a are not affected. That is to say, if the nth position of a certain number is 1, only the number and the other number need to be in the "or" phase, and the other number must be in addition to the corresponding nth position as 1, all others are 0 to block others. The preceding operation can be expressed by a = a | (0xF7) or a | = (0xF7.
3) bitwise "XOR" Operation
The bitwise "XOR" operator ^ is used to perform the "XOR" operation on both sides of the operator using the bitwise operands expressed in binary, this calculation is based on the same bit in the number. The rule for an exclusive or operation is: if two operands are different, the corresponding output result is 1; otherwise, the result is 0.
For example:
A = 0x88, B = 0x81, then the calculation result of a ^ B is as follows:
0x88 1000 1000 a count
^ 0x81 1000 0001 shielding count
= 0000 1001
The bitwise "XOR" operation ^ has some special applications, which are described as follows:
① Bitwise "XOR" operation can reverse a specific bitwise
For example, if we want to reverse the bitwise AND highest bits of the number of a, we only need to use 0x81, that is, 10000001 of the binary number, to perform bitwise "XOR" operations with it, the calculation result is the same as above. After the operation, the value of the highest bit has changed from 1 to 0, and the value of the second bit has changed from 0 to 1, which has the effect of turning the two. The status of other bits remains unchanged.
As you can see, except for the lowest Bit and the highest bit, all others are 0. The operation result will only take the 0th and 7 digits in the number as the inverse, the other bits of the number a are not affected. That is to say, if you need to reverse the nth digit of a number, you only need to "XOR" the number and the other digit, except that the nth digit is 1, all others are 0 to block others. The preceding operation can be expressed by a = a ^ (0x81) or a ^ = (0x81.
② Directly exchange the values of two variables
For example, if the variables a = 3 and B = 4 want to exchange their values, you can perform the following operations:
A ^ = B
B ^ =
A ^ = B
First, a ^ = B:
A 0000 0011
^ B 0000 0100
A = 0000 0111
Second, B ^ =:
B 0000 0100
^ A 0000 0111
B = 0000 0011
Finally, a ^ = B:
A 0000 0111
^ B 0000 0011
A = 0000 0100
In this way, the values in the variables a and B are reversed.
4) "inverse" Operation
Inverse Operator ~ The function is to reverse all the numbers: Set all 0 to 1, 1, and 0. For example:
1001 0110 is reversed to 0110 1001.
5) Right Shift of data
The right-shift operator of data moves several places to the right of each variable as required. The normal form of the right shift statement is:
Variable
For example, a = 1111 0000; after a = a, 2, a = 0011 1100.
6) Shift Data left
The left-Shift Data operator "moves the variables to the left several places as required. The normal form of the Left shift statement is:
Variable
For example, a = 1111 0000; after performing operation a = a "2", a = 1100 0000.
Whether it is left or right shift, when a person moves from one end, the blank space on the other end will be migrated from outside 0 (some computers send 1, for more information, see the user manual of the C compiler. This indicates that the shift is different from the loop. The bit removed from one end is not sent back to the other end. The shifted bit is lost forever. At the same time, only the corresponding digit 0 can be added to the other end.
The shift operation can be used for fast multiplication and division of integers. The left shift is equivalent to multiplication 2, while the right shift is equivalent to dividing by 2.
For example, x = 7, the Binary Expression is 0000 0111,
X 0000 1110, equivalent to: x = 2*7 = 14,
X 3 0111 0000, equivalent to: x = 14*2*2*2 = 112
X 2 1100 0000, x = 192
In the third left shift, one of the first shifts to the outside, while the left can only be filled with 0, so it is not equal to 112*2*2 = 448, it is equivalent to 192. When x moves back following the previous steps, it cannot return to the original value, because the loss of 1 on the Left cannot be recovered:
X "2 0011 0000, x = 48
X "3 0000 0110 x = 48/8 = 6
X "1 0000 0011 x = 6/2 = 3
The shift operation can also be used with other bitwise operators to set and detect each bit of the register or data I/O interface. For details, refer to the next section.