For example, there is an 8-bit binary integer. 1 in each bit represents power on and 0 represents power off. If we want to turn 8 switches on and off one by one, we need to use bit operations. You can look at the code of the microcontroller
If you use displacement to calculate, it is best to leave it to the compiler for optimization, such as x<<12, for example, directly x*1024. One of the commonly used bits is the flag bit. For example, what are the characteristics of a function? You can use bits to represent. eg flag1=0x1 flag2=0x2 flag3=0x4 flag4=0x8 if(flag&flag3){ .... }
The significance of bitwise operations is that the operating efficiency is usually several to dozens of times higher than addition, subtraction, multiplication and division. It is often used in algorithms that are very concerned about speed, such as many low-level algorithms. The disadvantage is poor readability and maintainability. For an example, please see this webpage: https://graphics.stanford.edu/~seander/bithacks.html
In addition, using integers to represent sets and using bit operations to perform set operations is also a common usage.
In general, it saves space for calculations and is fast. Examples of applications include 1. Bitmap index. For example, deduplication and query of QQ numbers or phone numbers; 2. Bit mask. Typical ones are linux file permissions; 3. Generate pseudo-random numbers; 4. Gray code
For example, when writing code for a microcontroller, you need to assign a value to a certain register. It is obviously simpler and more efficient to use bitwise operations.
For example, in a thirty-two-bit number, each bit represents an interrupt. To clear a certain bit, use AND. If you want to set a certain bit, use OR.
You can use shift operations to optimize efficiency, and you can use 1 bit space for flags
For example, there is an 8-bit binary integer. 1 in each bit represents power on and 0 represents power off. If we want to turn 8 switches on and off one by one, we need to use bit operations. You can look at the code of the microcontroller
If you use displacement to calculate, it is best to leave it to the compiler for optimization, such as x<<12, for example, directly x*1024. One of the commonly used
bits is the flag bit. For example, what are the characteristics of a function? You can use bits to represent.
eg
flag1=0x1
flag2=0x2
flag3=0x4
flag4=0x8
if(flag&flag3){
....
}
The significance of bitwise operations is that the operating efficiency is usually several to dozens of times higher than addition, subtraction, multiplication and division. It is often used in algorithms that are very concerned about speed, such as many low-level algorithms. The disadvantage is poor readability and maintainability. For an example, please see this webpage: https://graphics.stanford.edu/~seander/bithacks.html
In addition, using integers to represent sets and using bit operations to perform set operations is also a common usage.
I agree with @__simple, the setting (1) and clearing (0) in the microcontroller can be obtained by operating with another quantity
In general, it saves space for calculations and is fast. Examples of applications include
1. Bitmap index. For example, deduplication and query of QQ numbers or phone numbers;
2. Bit mask. Typical ones are linux file permissions;
3. Generate pseudo-random numbers;
4. Gray code
Used to set the properties of objects
For example, when writing code for a microcontroller, you need to assign a value to a certain register. It is obviously simpler and more efficient to use bitwise operations.