C-The bitwise operation of a bit is the operation of a bit (bit). In the section "Binary thinking and data storage", the bit is an electronic component with 8 bits constituting a byte (byte), which is already the smallest operational unit of granularity.
The C language provides six bit operators:
Operator |
& |
| |
^ |
~ |
<< |
>> |
Description |
Bitwise-AND |
Bitwise OR |
Bitwise XOR OR |
Take counter |
Move left |
Move right |
Bitwise AND operation of a bit (bit) bit only 0 and 12 values, only participate in
&
The two bits of the operation are 1 o'clock, the result is 1, otherwise 0. For example 1&1 for 1,0&0 for 0,1&0 for 0.
Values exist in memory in binary form,
9&5
The writable formula is as follows:
00001001 (9 binary)
&00000101 (5 binary)
00000001 (1 binary)
So
9&5=1
。
strictly speaking, the value in memory in the complement form, the complement of integers and its binary form is the same, negative numbers are not the same, do not understand the reader can self-complement.
Bitwise-AND operator
&
All bits of two numbers participating in the operation are
&
Operation.
Bitwise AND operations are usually used to clear some bits by 0 or to preserve certain bits. For example, C's high 16-bit clear 0, reserved low 16-bit, can be used as
a&65535
Operation (65536 takes 4 bytes and the binary number is 00000000000000001111111111111111).
Example bit operation example.
- #include <stdio.h>
- int main(){
- unsigned a=9; //binary number 00001001
- unsigned b=5; //binary number 00000101
- unsigned c=0xde09a32b; //decimal number 3725173547
- unsigned d=0x0000ffff; //decimal number 65535
- printf("a=%u, b=%u, a&b=%u\ n", a, b, a&b);
- printf("c=%u, d=%u, C&d (%%d) =%u, C&d (%%x) =%x\ n", C, D, C&d , C&d);
- return 0;
- }
Operation Result:
A=9, B=5, a&b=1
c=3725173547, d=65535, C&d (%d) =41771, C&d (%x) =a32b
bitwise OR operation participation or operation
|
Of the two bits have one for 1 o'clock, the result is 1, two are 0 o'clock the result is 0. For example 1|1 for 1,0|0 for 0,1|0 for 1.
9|5
The writable formula is as follows:
00001001 (9 binary)
|00000101 (5 binary)
00001101 (13 binary)
So
9|5=13
。
A bitwise OR operation can be used to place some binary positions 1, while some bits are reserved.
examples, or examples of operations.
- #include <stdio.h>
- int main(){
- unsigned a=9; //binary number 00001001
- unsigned b=5; //binary number 00000101
- Unsigned c=0xde09a30b; //decimal number 3725173547
- Unsigned d=0xffff0000; //decimal number 65535
- printf("a=%u, b=%u, a|b=%u\ n", a, b, a|b);
- printf("c=%u, d=%u, C|d (%%d) =%u, C|d (%%x) =%x\ n", C, D, C|d, C| D);
- return 0;
- }
Operation Result:
A=9, B=5, a|b=13
c=3725173515, d=4294901760, C|d (%d) =4294943499, C|d (%x) =ffffa30b
Bitwise XOR OR operation participating in an XOR operation
^
The two bits are not the same, the result is 1, the same result is 0. That is, 0^1 is 1,0^0 for 0,1^1 0.
9^5
Can be written as follows:
00001001 (9 binary)
^00000101 (5 binary)
00001100 (12 binary)
So
9^5=12
。
Bitwise XOR or operations can be used to reverse certain bits.
Example XOR operation example.
- #include <stdio.h>
- int main(){
- unsigned a=9; //binary number 00001001
- unsigned b=5; //binary number 00000101
- unsigned c=0x00ffff00; //decimal number 3725173547
- unsigned d=0xffff0000; //decimal number 65535
- printf("a=%u, b=%u, a^b=%u\ n", a, b, a^b);
- printf("c=%u, d=%u, C^d (%%d) =%u, C^d (%%x) =%x\ n", C, D, C^d, c^d);
- return 0;
- }
Operation Result:
A=9, B=5, a^b=12
c=16776960, d=4294901760, C^d (%d) =4278255360, C^d (%x) =ff00ff00
Take inverse operation to negation operator
~
As a single-mesh operator, right-associative, the function is to take the number of participating operations of the binary bitwise negation. For example, the 0,~0 is 1.
~9
The operation is:
~0000000000001001
1111111111110110
So
~9=65526
。
Left shift operator left shift
<<
It is used to shift all the operands of the operand to the left several bits, the high drop, the low 0.
<<
On the left is the operand to shift,<< to the right is the number of bits to move. For example:
a=9;a<<3;
The above code means moving the binary of a to the left by 3 bits. a=00001001 (binary of 9), 01001000 (decimal 72) After moving left 3 bits.
Right-shift operation right-shift operator
>>
It is used to shift all the binary of the operand to the right of a number of bits, drop discard, high 0 (or 1). For example:
a=9;a>>3;
Represents moving the binary of a to the right by 3 bits. A=00001001 (9 binary), 3 digits to the right and 00000001 (decimal 1).
It is important to note that for signed numbers, when you move right, the symbol bits are moved. When positive, the highest bit is 0, while negative, the sign bit is 1, the highest bit is 0 or the complement 1 depends on the compiler's provisions.
The sample bit operation synthesizes the example.
- #include <stdio.h>
- int main(){
- unsigned c=0x00ffff00; //decimal number 3725173547
- unsigned d=0xffff0000; //decimal number 65535
- printf("c=%x, d=%x, C^d (%%x) =%x, C|d (%%x) =%x, c>>4=%x, c<<8=%x\ n", C, D, C ^d, c|d, c>>4, c<<8);
- return 0;
- }
Operation Result:
C=FFFF00, d=ffff0000, C^d (%x) =ff00ff00, C|d (%x) =ffffff00, c>>4=ffff0, c<<8=ffff0000
C--bit arithmetic