In many system programs, calculations or processing are often required at the bit level. C language provides the function of bit operations, which allows C language to write system programs like assembly language.
━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Operator Function
────────────────────────────
& bit logic and
| bit logic or
^ bit logic exorbitant
- bit logic inverse
>> Move right
<< Move left
━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Bitwise operation is to detect, set or shift the actual bits in a byte or word. It is only applicable to character and integer variables and their variants, and is not applicable to other data types.
We should pay attention to distinguishing between bit operations and logical operations.
1. Bitwise and operation The bitwise and operator "&" is a binocular operator. Its function is to reconcile the two numbers participating in the operation of binary digits. Only when the corresponding two binary bits are 1, the result bit is 1, otherwise it is 0. The numbers participating in the calculation appear in the complementary form.
For example: 9&5 can be written as follows: 00001001 (9's two's complement code) &00000101 (5's two's complement code) 000000001 (1's two's complement code) See 9&5=1.
Bitwise and operation are usually used to clear or retain certain bits. For example, clear the high eight bits of a to 0 and keep the low eight bits, and a&255 operation can be performed (the binary number of 255 is 00000000111111111).
main(){
int a=9,b=5,c;
c=a&b;
printf("a=%d\nb=%d\nc=%d\n",a,b,c);
}
2. Bitwise or operation The bitwise or operator "|" is a binocular operator. Its function is the binary phase corresponding to the two numbers participating in the operation. As long as one of the corresponding two binary bits is 1, the result bit is 1. Both numbers participating in the calculation appear in complement.
For example: 9|5 is writable as follows: 00001001|00000101
00001101 (decimal is 13) can be seen 9|5=13
main(){
int a=9,b=5,c;
c=a|b;
printf("a=%d/nb=%d/nc=%d/n",a,b,c);
}
3. Bitwise XOR operation The bitwise XOR operator "^" is a binocular operator. Its function is that the two numbers participating in the operation have different binary digits or the binary digits. When the two corresponding binary digits are different, the result is 1. The participating operations still appear in complement codes. For example, 9^5 can be written as the formula as follows: 00001001^00000101 00001100 (decimal is 12)
main(){
int a=9;
a=a^15;
printf("a=%d/n",a);
}
4. Find the inverse operation Find the inverse operator~ is a monocular operator, which has right-bound nature. Its function is to inverse bits of each binary bit of the number participating in the operation. For example, the calculation of ~9 is: ~(0000000000001001) The result is: 111111111111111111110110
5. Left shift operation The left shift operator "<<" is a binocular operator. Its function shifts all binary bits of the operation number on the left of "<<" by several bits left, and specifies the number of bits to move by the number on the right of "<<", discards the high position, and fills the low position with zero. For example: a<<4 refers to moving each binary bit of a to the left by 4 bits. For example, a=00000011 (decimal 3), after shifting left by 4 bits, it is 00110000 (decimal 48).
6. Right shift operation The right shift operator ">>" is a binocular operator. Its function is to shift all binary bits of the operation number on the left of ">>" right several bits, and the number on the right of ">>" specifies the number of bits to move. For example: Let a=15, a>>2 means to shift 000001111 right to 00000011 (decimal 3). It should be noted that for signed numbers, the sign bits will move with each other when shifting right. When it is a positive number, the highest bit is supplemented with 0, and when it is a negative number, the sign bit is 1, and the highest bit is supplemented with 0 or 1 depends on the regulations of the compilation system.
main(){
unsigned a,b;
printf("input a number: ");
scanf("%d",&a);
b=a>>5;
b=b&15;
printf("a=%d\tb=%d\n",a,b);
}
Please see another example!
main(){
char a='a',b='b';
int p,c,d;
p=a;
p=(p<<8)|b;
d=p&0xff;
c=(p&0xff00)>>8;
printf("a=%d\nb=%d\nc=%d\nd=%d\n",a,b,c,d);
}
When performing bitwise and or, it is best to use hexadecimal system, which is expressed in the program as follows: 0x01 means 0000 0001
Therefore, the highest bit of character type a is forced to 1 like this: a=a|0x80. Others can be compared to the same!