C speech position operation
The so-called bit operation is to stop the operation on a bit (Bit). As mentioned in the section "binary thinking and data storage", Bit is an electronic component with eight bits forming a Byte, which used to be the smallest operational unit of granularity.
C language provides six bit operators:
Operator & | ^ ~ clarify the difference between bitwise and bitwise or reverse left shift right bitwise and operation (&)
As long as there are two values of 0 and 1 for a Bit bit, as long as both bits of the operation are 1, the consequence is 1, otherwise it is 0. For example, the logical operator & & is very similar to the logical operator, which is similar to the logical operator.
Binary can not be directly used in C language & the operands of both sides can be decimal octal and hexadecimal and most of them are stored in binary mode in memory & that is to stop the operation of these binary bits in memory. Other bit operators are the opposite.
For example, 9-5 can be converted to the following operation:
0000 0000-0000 0000-0000 0000-0000 1001 (9 storage in memory)
& 0000 0000-0000 0000-0000 0000-0000 0101 (5 storage in memory)
0000 0000-0000 0000-0000 0000-0000 0001 (1 storage in memory)
In other words, bitwise and operation will stop all binary bits of the two numbers involved in the operation, and the consequence of 9-5 is 1.
For another example,-9-5 can be converted into the following operations:
1111 1111-1111 1111-1111 1111-1111 0111 (- 9 storage in memory)
& 0000 0000-0000 0000-0000 0000-0000 0101 (5 storage in memory)
0000 0000-0000 0000-0000 0000-0000 0101 (5 storage in memory)
The consequence of-9-5 is 5.
We have stopped learning about how negative and positive numbers are stored in memory in the VIP tutorial "how integers are stored in memory."
Again, & the operation is stopped based on the binary bits in memory, not the binary way of the data; the same is true of other bit operators. Take-9 for example, the memory of-9 is exactly the same as the binary of-9:
1111 1111-1111 1111-1111 1111-1111 0111 (- 9 storage in memory)
-0000 0000-0000 0000-0000 0000-0000 1001 (binary mode of-9, the excess zeros can be erased)
Bitwise and arithmetic is usually used to clear some bits or save some bits. For example, to clear the high 16 bits of n and save the low 16 bits, you can stop the n & 0XFFFF operation (the storage mode of 0XFFFF in memory is 0000 0000-0000 0000-1111 1111-1111 1111).
[example] stop honing the following analysis.
# include int main () {int n = 0X8FA6002D; printf ("% d,% d,% X\ n", 9 & 5,-9 & 5, n & 0XFFFF); return 0;}
Operational consequences:
1, 5, 2D
Bitwise or operational (|)
Step in | when one of the two binary bits of the operation is 1, the consequence is 1, and the consequence is 0 when both are 0. For example, 1 | 1 is 1, 0 is 0, 0 is 0, and 0 is 1, which is very similar to the "|" in logical operations.
For example, 9 | 5 can be converted to the following operation:
0000 0000-0000 0000-0000 0000-0000 1001 (9 storage in memory)
| | 0000 0000-0000 0000-0000 0000-0000 0101 (5 storage in memory) |
0000 0000-0000 0000-0000 0000-0000 1101 (13 storage in memory)
9 | the consequence of 5 is 13.
For example,-9 | 5 can be converted into the following operations:
1111 1111-1111 1111-1111 1111-1111 0111 (- 9 storage in memory)
| | 0000 0000-0000 0000-0000 0000-0000 0101 (5 storage in memory) |
1111 1111-1111 1111-1111 1111-1111 0111 (- 9 storage in memory)
-9 | the consequence of 5 is-9.
Bitwise or operation can be used to change some status 1, or to save some bits. For example, to set the high 16 position 1 of n and save the low 16 bits, you can stop the n | 0XFFFF0000 operation (the storage mode of 0XFFFF0000 in memory is 1111 1111-1111 1111-0000 0000-0000 0000).
[example] stop checking the following analysis.
# include int main () {int n = 0X2D; printf ("% d,% d,% X\ n", 9 | 5,-9 | 5, n | 0XFFFF0000); return 0;}
Operational consequences:
13.-9, FFFF002D.
Bitwise XOR (^)
When you get involved in the difference between the two binary bits of ^, the consequence is 1, and on the contrary, the consequence is 0. For example, 0 ^ 1 is 1, 0 ^ 0 is 0, and 1 ^ 1 is 0.
For example, 9 | 5 can be converted to the following operation:
0000 0000-0000 0000-0000 0000-0000 1001 (9 storage in memory)
^ 0000 0000-0000 0000-0000 0000-0000 0101 (5 storage in memory)
0000 0000-0000 0000-0000 0000-0000 1100 (12 storage in memory)
9 | the consequence of 5 is 12.
For example,-9 | 5 can be converted into the following operations:
1111 1111-1111 1111-1111 1111-1111 0111 (- 9 storage in memory)
^ 0000 0000-0000 0000-0000 0000-0000 0101 (5 storage in memory)
1111 1111-1111 1111-1111 1111-1111 0010 (- 1 storage in memory)
-9 | the consequence of 5 is-14.
Bitwise XOR operations can be used to reverse some binary bits. For example, to reverse the high 16 bits of n and save the low 16 bits, you can stop the n ^ 0XFFFF0000 operation (0XFFFF0000 is stored in memory as 1111 1111-1111 1111-0000 0000-0000 0000).
[example] stop checking the following analysis.
# include int main () {unsigned n = 0X0A07002D; printf ("% d,% d,% X\ n", 9 ^ 5,-9 ^ 5, n ^ 0XFFFF0000); return 0;}
Operational consequences:
12.-14, F5F8002D.
Reverse operation (~)
Take the inverse operator ~ as the monocular operator, right association, influence is the binary bit inversion of the intervention operation. For example, ~ 1 is 0 and 0 is 1, which is the same as in logical operation! Very similar.
For example, ~ 9 can be converted to the following operation:
~ 0000 0000-0000 0000-0000 0000-0000 1001 (9 storage in memory)
1111 1111-1111 1111-1111 1111-1111 0110 (- 10 storage in memory)
So the consequence of ~ 9 is-10.
For example, ~-9 can be converted to the following operation:
~ 1111 1111-1111 1111-1111 1111-1111 0111 (- 9 storage in memory)
0000 0000-0000 0000-0000 0000-0000 1000 (9 storage in memory)
So the consequence of ~-9 is 8.
[example] stop checking the following analysis.
# include int main () {printf ("% d,% d\ n", ~ 9, ~-9); return 0;}
Operational consequences:
-10, 8
Left shift operation (3, (- 9) > > 3); return 0;}
Operational consequences:
1.-2.