1. Bit
Bit represents a bit in a binary number. The value is 0 or 1. 0 represents false and 1 represents true.
It is often necessary to deal with the problem of binary bits in system software. In the modern framework, the operation speed is usually the same as that of addition and faster than multiplication.
c language provides the following six bit operators
- Bitwise AND&
- Bitwise OR|
- Bitwise XOR^
- Bitwise inversion~
- Shift left<<
- Move right > >
2. Bitwise AND
Bit and operation: perform logical and operation on the two data involved in the operation bit by bit according to the corresponding binary number.
The two numbers involved in the operation must be integer or character type, and the numbers involved in the operation must appear in the form of complement.
The result of logic and operation is 1 only when the corresponding two binary bits are 1, otherwise it is 0.
#include <stdio.h> int main(){ int a = 4;//4's complement 00000000 00000000 00000000 int b = 7;//7's complement 00000000 00000000 000000111 int c=a&b;//The result is 00000000 00000000 00000000 printf("c=%d\n", c);//Output 4 return 0; }
Bit and operation application: quick reset
Clear quickly. For example, to clear a variable, because the result of bit and operation is 1 only when both binary numbers are 1, the result of bitwise sum of any number and 0 is 0.
int a = 10; //do something with variable a .... //set variable a as 0 a = a&0;
Bit and operation application: keep the specified position
@todo
Bit and operation application: judging parity
#include <stdio.h> int main(){ int a = 4; a & 1;//If the result is 1, a is odd, and if the result is 0, a is even }
3. Bitwise OR
Perform logical or operation on the two data involved in the operation
#include <stdio.h> int main(){ int a = 9;//Binary: 00000000 00000000 00000000 00001001 int b = 5;//Binary: 00000000 00000000 00000001 int c=a|b;//Binary bit: 00000000 00000000 00000000 00001101 printf("c=%d\n",c);//Output 13 return 0; }
Bit by bit or purpose: set the specified position of data
If you want to set all the first bytes of 9 to 1, you can use the following writing method
int a = 9; //0xFF to hexadecimal 255 //The binary representation of 0xFF is 00000000 00000000 11111111 a = a|0xFF; printf("a=%d\n",a);//Output 255
4. Bitwise XOR
Perform logical XOR operation on the two data involved in the operation according to the corresponding binary number
#include <stdio.h> int main(){ int a = 9;//0000 1001 int b = 5;//0000 0101 int c=a^b;//0000 1100 printf("c=%d\n",c);//c=12 return 0; }
Bitwise XOR application: position reversal
int a = 9;//0000 1001 a=a^0xFF;// 1111 0110
Bitwise XOR application: value exchange
int a = 9; int b = 5; a = a^b; b = b^a; a = a^b; printf("a=%d\n,b=%d\n",a);//a=5,b=9, the values of the two variables are exchanged
5. Bitwise inversion
int a = 9;//0000 1001 a = ~a; //1111 0110
6. Move left move right
Shift left
Shift the binary value corresponding to the data bit by bit to the left.
To shift n bits to the left is to multiply his value to the nth power of 2.
When moving left, the high bit is discarded and the low bit is supplemented with 0.
int a = 3;//00000000 00000000 00000000 00000011 / / four zeros are discarded in the highest order a = a<<4; //00000000 00000000 00110000 / / four zeros are added to the lowest order
int i = 1;//0000 0001 i = i<<2; //0000 0100 / / the value is 4. Moving two bits left is equivalent to 1 * 2 ^ 2
be careful:
int is a signed type. In the process of moving, the leftmost sign bit is moved out
An overflow occurs.
The most common function of shift left is to realize double multiplication.
Since the operation speed of displacement operation is much higher than that of multiplication, this method can be adopted to obtain higher operation speed when processing data multiplication.
Shift right
Shift the binary value corresponding to the data bit by bit to the right.
Moving right is a process of dividing by 2. Moving n bits right is actually divided by 2 to the nth power.
be careful:
There is a difference between moving right and moving left. If the current data is a signed number, whether to supplement 0 or 1 on the left is determined according to the sign bit when moving right. If the sign bit is 0, the left will supplement 0. If the sign bit is 1, it will have different processing methods according to different computer systems.