Simply put, the bits are 1 and 0, and everything in the computer is made up of them. All data in a computer are represented by bits. A byte consists of 8 bits; A word consists of two bytes, namely 16 bits; A doubleword consists of four bytes or 32 bits.
0 1 0 0 0 1 1 1 1 0 0 0 0 1 1 1 0 1 1 1 0 1 0 0 0 1 1 1 1 0 0 0
||..............|...............|...............|..............||
|+-bit 31... | ............... | ............... | ... bit 0 -+|
|...............|...............|...............|...............|
+-Byte 3-+-Byte 2-+-Byte 1-+-Byte 0-+
|...............................|...............................|
+ - WORD 1 - + - WORD 0 - +
|...............................................................|
+ - DWORD - +
Using bytes, words or doublewords for bit manipulation is beautiful, just like using a small array or structure. Using bit operations, you can check or set the value of a single bit or a group of bits.
The relationship between hexadecimal numbers and bits
It is difficult to express the comparison of numbers by binary counting. In order to avoid this problem, hexadecimal notation (radix 16) is adopted. A number in hexadecimal is represented by four numbers in binary, ranging from 0 to 15. A set of four bits, that is, nibbles. If a byte has two and a half bytes, the value of a byte can be represented by two hexadecimal digits.
Half-byte hexadecimal number
====== =========
0000........0
000 1........ 1
00 10........2
00 1 1........3
0 100 ... four
0 10 1........5
0 1 10........6
0 1 1 1 ... seven
1000........8
100 1........9
10 10........A
10 1 1........B
1 100........C
1 10 1........D
1 1 10........E
1 1 1 1........F
If there is a byte with the letter' r' (ASCII code 1 14), its meaning is as follows:
01110010 binary number
7 2 Hexadecimal Numbers
Note: 0x72
Position operator
* * * has six bit operators, as follows:
& logical product operator
| or operator
XOR operator
~ negation operator
& gt& gt right shift operator
& lt& lt left shift operator
& operator
The&AND operation requires two operands and then returns the value. If and only if both operands are 1, the return value is 1. Table below:
1。 1 == 1
1。 0 == 0
0 & amp 1 == 0
0 & amp0 == 0
A byte can contain a bit flag, and the AND operation can check the value of the bit by setting a mask. The algorithm is as follows: It is used to judge whether the fourth bit in the byte is 1.
Byte b = 50
If (b & amp0x 10)
Cout & lt& lt "the fourth bit has been set"<& ltendl
other
Cout & lt& lt "the fourth place is clear"<& ltendl
The result can be obtained by the following calculation:
....00 1 100 10 - b
& amp000 10000-& amp; 0x 10
-
... 000 10000-result
So the fourth place is 1.
| operator
The | (or) operator takes two operands and then returns a value. If and only if one of the two operands is 1 or both are 1, the return value is 1. Table below:
1 | 1 == 1
1 | 0 == 1
0 | 1 == 1
0 | 0 == 0
Using OR operation can ensure that one bit in the byte is 1. The algorithm is as follows: It is used to ensure that the second bit is always 1.
Byte b = 50
Byte c = b | 0x04
cout & lt& lt" c = " & lt& ltc & lt& ltendl
The result can be obtained by the following calculation:
....00 1 100 10 - b
| 00000 100 - | 0x04
-
... 001101/kloc-0-results ...
XOR operator
The XOR operator takes two operands and then returns the value. If and only if one of the two operands is 1 instead of 1, the return value is 1. Table below:
1 ^ 1 == 0
1 ^ 0 == 1
0 ^ 1 == 1
0 ^ 0 == 0
You can use XOR to flip specific bits. That is, 0 becomes 1 and 1 becomes 0. The algorithm is as follows: flip the third and fourth bits
Byte b = 50
cout & lt& lt" b = " & lt& ltb & lt& ltendl
b = b ^ 0x 18;
cout & lt& lt" b = " & lt& ltb & lt& ltendl
b = b ^ 0x 18;
cout & lt& lt" b = " & lt& ltb & lt& ltendl
The result can be obtained by the following calculation:
....00 1 100 10 - b
^ 000 1 1000-^ 0x 18
-
... 00 10 10 10-results.
00 10 10 10 - b
^ 000 1 1000-^ 0x 18
-
... 00 1 100 10-result.
~ negation operator
The ~ (negation) operator only needs one operand, and then 1 becomes all zeros, and all zeros become 1. Using the inversion operation, some bytes can be set to 0, ensuring that other bytes are set to 1, regardless of it.
The size of the data. The algorithm is as follows: all positions 1, and the first and 0th positions are 0.
Byte b = ~ 0x03
cout & lt& lt" b = " & lt& ltb & lt& ltendl
Word w = ~ 0x03
cout & lt& lt" w = " & lt& ltw & lt& ltendl
The result can be obtained by the following calculation:
000000 1 1 - 0x03
1 1 1 1 1 100-~ 0x 03 b
00000000000000 1 1-0x 03
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 100-~ 0x 03 w
With the (with) operator, you can set any position to 0. The algorithm is as follows: Set the fourth position 0.
Byte b = 50
cout & lt& lt" b = " & lt& ltb & lt& ltendl
Byte c = b&~ 0x10;
cout & lt& lt" c = " & lt& ltc & lt& ltendl
The result can be obtained by the following calculation:
....00 1 100 10 - b
& amp 1 1 10 1 1 1 1-~ 0x 10
-
... 00 1000 10-result.
& gt& gt and
& gt& gt (shift right) operator and
Byte b =12;
cout & lt& lt" b = " & lt& ltb & lt& ltendl
Byte c = b < < lt2;
cout & lt& lt" c = " & lt& ltc & lt& ltendl
c = b & gt& gt2;
cout & lt& lt" c = " & lt& ltc & lt& ltendl
The result can be obtained by the following calculation:
0000 1 100 - b
00 1 10000-b & lt; & lt2
000000 1 1-b & gt; & gt2
Bit segment
Bit segment is an interesting part of bit operation. Using a bit field, you can set a small structure in a byte, word or doubleword. For example, in order to record the date and require as little memory as possible, you can adopt the following structural statement:
Structure Date _ Structure {
Byte days: 5,/1to 3 1
Month: April,/1to 12
Year:14; // 0 to 9999
} date;
In the above example, the day occupies the lowest 5 places, the month occupies the next 4 places, and the year is the next 14 places. The whole date is stored in 23 bits of three bytes. The 24th place was ignored. If plastic declaration is used, 12 bytes will be occupied.
|0 0 0 0 0 0 0 0|0 0 0 0 0 0 0 0|0 0 0 0 0 0 0 0|
| | | |
+-Year-+Month+-Day-+
As mentioned above, the date type uses a segment structure. BYTE is used here. A byte is 8 bits. When it is used, the compiler will apply for a byte to store it. If the structure exceeds 8 bits, the compiler will request another byte until it is enough to store the structure. If one word or two words are used, the compiler will apply a total of 32 bits to store the structure.
How to declare a paragraph? First declare the segment variable, followed by a colon, and then the number of digits assigned to the variable; Each paragraph is separated by a comma, and finally a semicolon indicates the end of the declaration.
After completing the structure declaration, you can use the structure conveniently by accessing the tag, or you can use the address operator to manipulate the structure. As follows:
date . day = 12;
Dateprotr = & date;
dateptr-& gt; Year =1852;