first, let's take a look at the related concepts. On our current PC, 32-bit machines are commonly used, and an integer Int Is 4 bytes, a char is 1 byte, and a byte is 8 bits. The concept of bit here is the main character of today. It is widely used in embedded fields and system software. C/C ++ is a very special high-level language. It can operate bitwise directly, or even the concept of bitfield. It can be defined for bitwise in a byte. What are the advantages of using bits? I think it can save memory and provide high performance. As a flag, the significance is very clear. The flag bit in the Win32 API is usually completed using this, and a byte can be used to represent 32 flags. It looks perfect to use | or & Operations. For example, we used to determine whether the path is a folder:
Win32_find_data fileinfo;// File Information StructureHandlehfile;// File handleHfile = findfirstfile (sdir, & fileinfo );// If the file exists and it is a directoryIf(Fileinfo. dwfileattributes & file_attribute_directory ){// Directory exists close fileFindclose (hfile );}Else{If(! Createdirectory (sdir, null) {afxmessagebox ("Create a local directory ["+ Sdir +"] Failed.");ReturnFalse ;}}
...
C bit operations mainly include
| Operation |
Operator |
Equations and results |
Comments |
| Or |
| |
0000 0000 | 0000 1001 0000 = 1001 |
For each digit, the value of 1 is 1. |
| And |
& |
0010 1111 & 1000 0000 = 0000 0000 |
For each calculation, the value of 0 indicates 0, and the value of 1 indicates 1. |
| Invert |
~ |
~ 1010 0111 = 01011000 |
It is best to understand that every 1 is 0, and every 0 is 1. |
| Exclusive or |
^ |
0000 1001 ^ 1010 1010 = 1010 0111 |
If the two operation bits are different, the value is 1. If the two operation bits are the same, the value is 0. |
| Move left |
< |
0101 1000 <2 = 0110 0000 |
Move all bits to the left, discard the left bits, and add 0 to the right |
| Right Shift |
> |
01100000> 4 = 0000 0110 |
Move all bits to the right, discard the right bits, and add 0 to the left |
For these operators, we try
C bit operations # Include <stdlib. h> # include <string. h> # include <assert. h> Void Print_bit (Unsigned Char B ){ /* Because the bit value cannot be directly accessed, you can only use the & operation to test whether a bit is equal to or */ /* Of course, this method is stupid now, because you have to calculate these hexadecimal values, and you will try a better method later */ Printf (" % C ", (B & 0x80 )? '1': '0 '); /* 1000 0000 */ Printf (" % C ", (B & 0x40 )? '1': '0 '); /* 0100 0000 */ Printf (" % C ", (B & 0x20 )? '1': '0 ');/* 0010 0000 */ Printf (" % C ", (B & 0x10 )? '1': '0 '); /* 0001 0000 */ Printf (" % C ", (B & 0x08 )? '1': '0 '); /* 0000 1000 */ Printf (" % C ", (B & 0x04 )? '1': '0 '); /* 0000 0100 */ Printf (" % C ", (B & 0x02 )? '1': '0 '); /* 0000 0010 */ Printf (" % C ", (B & 0x01 )? '1': '0 '); /* 0000 0001 */ } Int Main (){ /X 00000000 */ Unsigned Char B = 0; Printf (" At initialization: \ n "); Print_bit (B ); Printf (" \ N ---------------------------------- \ n "); /* Set the fourth digit to 00000000 | 00001001 = 00001001 */ Print_bit (B); B | = 0x9;Printf (" | 00001001 = "); Print_bit (B ); Printf (" \ N ---------------------------------- \ n "); /* XOR or 00001001 ^ 10101010 = 10100111 */ Printf (" XOR ^ operation: "); Print_bit (B ); Printf (" ^ 10101010 = "); B ^ = 0xaa; print_bit (B ); Printf (" \ N ---------------------------------- \ n ");/* Reverse retrieval ~ 10100111 = 01011000 */ Printf (" Reverse retrieval ~ Operation :~ "); Print_bit (B ); Printf (" = "); B = ~ B; print_bit (B ); Printf (" \ N ---------------------------------- \ n "); /* Shift 01011000 left <2 = 01100000 */ Printf (" Remove left <2 Operation: "); Print_bit (B); B = B <2; Printf (" <2 = "); Print_bit (B ); Printf (" \ N ---------------------------------- \ n "); /* Right shift */ Printf (" Move right> 4 operation: "); Print_bit (B); B = B> 4; Printf (" > 4 = "); Print_bit (B ); Printf (" \ N ---------------------------------- \ n "); Return 0 ;}
Next, let's take a look at the bit operations of C ++. C ++ bit operations, of course, are the STL bitset.
The following table is taken from cppprimer version 2 4.12.
| Operation |
Function |
Usage |
| Test (POS) |
Is the POs bit 1? |
A. Test (4) |
| Any () |
Is any bit 1? |
A. Any () |
| None () |
Is no bit 1? |
A. None () |
| Count () |
The number of bits whose value is 1 |
A. Count () |
| Size () |
Number of bit Elements |
A. Size () |
| [POS] |
Access POS |
A [4] |
| Flip () |
Flip all bits |
A. Flip () |
| Set () |
Set all positions to 1 |
A. Set () |
| Set (POS) |
Set POS position to 1 |
A. Set (4) |
| Reset () |
Set all positions to 0 |
A. Reset () |
| Reset (POS) |
Set POS position to 0 |
A. Reset (4) |
| |
|
|
For the above CProgram, We can write the corresponding C ++ version, and we will not write it here. After all, bitset operations do not need to perform the troublesome hexadecimal computing like C.
Summary:
Bitset is more intuitive than bitwise operations of original C,CodeIt is clear and easy to understand. It does not require a program to perform mechanical binary and hexadecimal conversion. For operations with less contact bits
It is not a good news for people, but for Embedded programmers who often perform bit operations, it is estimated that it is easier to directly use the bit operators. Maybe this difference is different from that of UNIX
CLI (command line mode) is the same as Win's Gui (Window Interface). From the preliminary perspective, the mouse is easy to use, but for skilled personnel (such as programmers ), it is often more efficient to use command lines, and
Batch Processing is convenient. Far away. In fact, can the two be used in combination from the perspective of syndrome differentiation? For example, what if windows focuses on clients and Unix focuses on servers? Once again!
Technorati Tag: Bit operation, bitset, C, C ++, comparison