Chapter 6 binary, octal, and hexadecimal
6.1 why do we need octal and hexadecimal?
6.2 decimal number conversion to decimal number
6.2.1 convert binary to decimal
6.2.2 convert octal to decimal
6.2.3 expression of the octal number
6.2.4 use of Octal numbers in escape characters
6.2.5 convert the hexadecimal number to the decimal number
6.2.6 expression of hexadecimal numbers
6.2.7 use of hexadecimal numbers in escape characters
6.3 decimal number to binary, octal, and hexadecimal number
6.3.1 convert a 10-digit number to a 2-digit number
6.3.2 convert 10 hexadecimal numbers to 8 and 16 hexadecimal numbers
6.4 hexadecimal Conversions
6.5 original code, reverse code, and supplemental code
6.6 view variable values through debugging
6.7 summary of this Chapter
This is a course titled "no village or no store. The conversion between different numerics is purely a mathematical calculation. However, you don't have to worry about the complexity. It's nothing more than multiplication or Division calculation.
In fact, the counting methods in many places in life are somewhat different in hexadecimal notation.
For example, the most commonly used 10-digit system actually originated from people with 10 fingers. If our ancestors never get rid of this situation, I think we must be using a 20-digit system.
For binary ...... No sock is called 0, and one sock is called 1. But if there are two sock, we often say: 1 pair of sock.
In life, there are also: 7 hexadecimal, such as weeks. Hexadecimal format, such as hour or "A Dozen", 60 bytes, such as minute or angle ......
6.1 why do we need octal and hexadecimal?
In programming, we usually use 10 hexadecimal notation ...... C/C ++ is a high-level language.
For example:
Int A = 100, B = 99;
However, because the representation of data in a computer exists in binary format, sometimes binary can be used to solve the problem more intuitively.
However, the binary number is too long. For example, the int type occupies 4 bytes and 32 bits. For example, if the value is 100, the value expressed in the binary number of the int type is:
0000 0000 0000 0000 0110 0100
No one would like to think about or operate on such a long number. Therefore, C and C ++ do not provide a method to directly write binary numbers in code.
This problem can be solved in hexadecimal or octal. Because,The larger the number, the shorter the expression length.. However, why is it 16 or 8 hexadecimal instead of 9 or 20?
2, 8, and 16 are respectively the power 1 of 2, the Power 3, and the Power 4. This allows the three hexadecimal systems to directly convert each other. In octal or hexadecimal mode, the binary number is shortened, but the expression of binary number is maintained. In the following course about hexadecimal conversion, you can find this point.
6.2 convert binary number to decimal number 6.2.1 convert to decimal number
0th bits in binary are 0 to the power of 2, and 1st bits are 1 to the power of 2 ......
Therefore, there is a binary number: 0110 0100, which is converted to a 10-digit system:
The following is a vertical layout:
0110 0100 to decimal
0th-Bit 0*20 = 0
1st-Bit 0*21 = 0
2nd-bit 1*22 = 4
3rd-Bit 0*23 = 0
4th-Bit 0*24 = 0
5th-bit 1*25 = 32
6th-bit 1*26 = 64
7th-Bit 0*27 = 0 +
---------------------------
100
The horizontal calculation is as follows:
0*20 + 0*21 + 1*22 + 1*23 + 0*24 + 1*25 + 1*26 + 0*27 = 100
The number multiplied by 0 is 0, so we can skip the bit with a value of 0:
1*22 + 1*23 + 1*25 + 1*26 = 100
6.2.2 convert octal to decimal
In October, it means every 8 to 1.
The eight-digit number ranges from 0 ~ 7.
The 0th bits in eight dimensions are 0 to the power of 8, the 1st bits are 1 to the power of 8, and the 2nd bits are 2 to the power of 8 ......
Therefore, there is an octal digit: 1507, which is converted to decimal:
Vertical representation:
1507 to decimal.
0th-bit 7*80 = 7
1st-Bit 0*81 = 0
2nd-bit 5*82 = 320
3rd-bit 1*83 = 512 +
--------------------------
839
Similarly, we can use horizontal calculation:
7*80 + 0*81 + 5*82 + 1*83 = 839
The result is that the number of octal digits is 1507 to 839 in decimal format.
6.2.3 expression of the octal number
In C and C ++, how does one express an octal number? If the number is 876, we can conclude that it is not an octal number, because the octal number cannot contain more than 7 Arabic numbers. However, if the number is 123, 567, or 12345670, it is possible to set the number to octal or decimal.
Therefore, C and C ++ rules,If you want to specify that a number uses an octal value, you must add a value of 0 before it.For example, 123 is in decimal format, but 0123 indicates octal. This is the expression of Octal numbers in C and C ++.
Since neither c nor C ++ provides the expression of binary numbers, we learned octal here. The second method of expressing the value of CTC ++ is used.
Now, for the same number, for example, 100, we can use the normal 10 hexadecimal expression in the Code, for example, during variable initialization:
Int A = 100;
We can also write:
Int A = 0144; // 0144 is 100 of the octal value. We will learn how to convert a 10-digit number into an octal value.
Remember, you cannot lose the first zero when expressing it in octal. Otherwise, the computer is regarded as a 10-digit system. However, when the number of octal characters is used, the value 0 cannot be used, which is the "Escape Character" expression we learned before for expressing characters.
6.2.4 use of Octal numbers in escape characters
We have learned how to use an escape character '/' and a special letter to represent a character. For example, '/N' indicates line ), '/t' indicates the Tab character, and'/'indicates the single quotation mark. Today, we have learned another method to use escape characters: The Escape Character '/' is followed by an octal number to indicate characters whose ASCII code is equal to this value.
For example, check the ASCII code table in Chapter 5th and find the question mark character (?) The ASCII value of is 63, so we can convert it into an octal value: 77, and then use '/77' To Represent '? '. Because it is octal, it should be written as '/077', but because C, C ++ does not allow the use of a slash plus a 10-digit number to represent characters, so here 0 can be left empty.
In fact, we seldom use escape characters and Octal numbers to represent a character in actual programming. Therefore, we only need to understand the content in section 6.2.4.
6.2.5 convert the hexadecimal number to the decimal number
In decimal format, two Arabic numbers are used: 0 and 1;
8 digits: 0, 1, 2, 3, 4, 5, 6, and 7;
10 in hexadecimal notation, with 10 Arabic numerals: 0 to 9;
In hexadecimal notation, sixteen Arabic numerals ...... Wait, the Arab, or the Indian, only invented 10 numbers?
In hexadecimal notation, the number is 16 to 1, but we only have 0 ~ 9 These 10 numbers, so weUse the letters A, B, C, D, E, and F to represent 10, 11, 12, 13, and 15 respectively.. Uppercase letters are not case sensitive.
The 0th bits in hexadecimal notation are 0 to the power of 16, 1st bits are 1 to the power of 16, and 2nd bits are 2 to the power of 16 ......
Therefore, in the Nth (N starts from 0) bit, if it is a number x (x is greater than or equal to 0, and X is less than or equal to 15, that is, F) it indicates the N power of x * 16.
Suppose there is a hexadecimal number 2af5, how can we convert it into a hexadecimal number?
Use vertical calculation:
2af5 to 10:
0th bits: 5*160 = 5
1st bits: F * 161 = 240
2nd bits: A * 162 = 2560
3rd bits: 2*163 = 8192 +
-------------------------------------
10997
Direct calculation is:
5*160 + F * 161 + A * 162 + 2*163 = 10997
(Remember, in the above calculation, a represents 10, and f Represents 15)
Now we can see that the key to converting all hexadecimal values into a decimal value is that their respective weights are different.
Suppose someone asks you, why is the ten-in-one number 1234 one thousand two hundred and thirty-four? You can give him the following formula:
1234 = 1*103 + 2*102 + 3*101 + 4*100
6.2.6 expression of hexadecimal numbers
If no special writing format is used, the hexadecimal number will be mixed with the hexadecimal number. A random number: 9876, it cannot be seen that it is in hexadecimal or 10 hexadecimal format.
C, C ++,Hexadecimal number must start with 0x. For example, 0x1 indicates a hexadecimal number. 1 indicates a decimal number. For example, 0xff, 0xff, 0x102a, and so on. X is not case sensitive. (Note: 0 in 0x is the number 0, not the letter O)
Some examples are as follows:
Int A = 0x100f;
Int B = 0x70 +;
So far, we have learned all the hexadecimal expressions: 10 hexadecimal, 8 hexadecimal, and 16 hexadecimal numbers. The last point is very important. In C/C ++, there are positive and negative numbers in hexadecimal notation. For example, 12 indicates positive 12, while-12 indicates negative 12.The octal and hexadecimal values can only be unsigned positive integers.If you are in the Code:-078, or write:-0xf2, C, C ++, it is not treated as a negative number.
6.2.7 use of hexadecimal numbers in escape characters
An escape character can also be expressed as a hexadecimal number. As mentioned in section 6.2.4 '? 'Character, which can be expressed as follows:
'? '// Enter the character Directly
'/77' // use the octal sequence. The value 0 at the beginning can be omitted.
'/0x3f' // hexadecimal format
Similarly, this section is only used for understanding. Except for null characters expressed by octal numerals '/0', we rarely use the last two methods to represent a single character.
6.3 decimal number to binary, octal, and hexadecimal number 6.3.1 10 hexadecimal number to binary number
Give you a decimal number, for example, 6. What if I convert it into a binary number?
The 10th hexadecimal number is converted to the binary number, which is a process of consecutive division of 2:
Divide the number to be converted by 2 to obtain the quotient and remainder,
Divide the quotient by 2 until the quotient is 0. Finally, sort all the remainder in reverse order. The result is the conversion result.
Sounds confused? We will illustrate it with examples. For example, convert 6 to the binary number.
"Divide the number to be converted by 2 to get the quotient and remainder ".
So:
The number to be converted is 6, 6 then 2.The quotient is 3, and the remainder is 0.. (Don't tell me you won't calculate 6 then 3 !)
"Divide the merchant by 2 until the merchant is 0 ......"
The current business is 3, not 0, so continue to divide by 2.
So: 3 then 2, getThe quotient is 1, and the remainder is 1..
"Divide the merchant by 2 until the merchant is 0 ......"
The current business is 1, not 0, so continue to divide by 2.
Then: 1 second 2, getThe quotient is 0, and the remainder is 1.(Take a pen and paper to calculate it. 1, 2, is it 0, more than 1 !)
"Divide the merchant by 2 until the merchant is 0 ...... Finally, sort all the remainder in reverse order"
Excellent! The current business is 0.
After three computations, the remainder is 0, 1, and 1 respectively. All the remainder is sorted in reverse order, that is, 110!
6. Convert to binary. The result is 110.
Change the preceding section to a table:
Divisor |
Computing process |
Vendors |
Remainder |
6 |
6/2 |
3 |
0 |
3 |
3/2 |
1 |
1 |
1 |
1/2 |
0 |
1 |
(In a computer, tokens are expressed)
During the examination, it would be a little time-consuming to draw such a table. The more common conversion process is the Division:
(Figure: 1)
Compare the graph, table, and text description, and calculate how to convert 6 to binary number.
After talking about it for half a day, are our conversion Results Correct? Is the binary number 110 6? You have learned how to convert a binary number to a 10-digit number. Therefore, calculate whether the value of 110 is 6 if it is 10.
6.3.2 convert 10 hexadecimal numbers to 8 and 16 hexadecimal numbers
Very happy. The method for converting a 10-digit number to an 8-digit number is similar to the method for converting to a 2-digit number. The only change is that the divisor is changed from 2 to 8.
Let's look at an example of how to convert the decimal number 120 to the octal number.
Table representation:
Divisor |
Computing process |
Vendors |
Remainder |
120 |
120/8 |
15 |
0 |
15 |
15/8 |
1 |
7 |
1 |
1/8 |
0 |
1 |
120 is converted to octal, and the result is: 170.
Very happy. The method for converting a 10-digit number to a 16-digit number is similar to the method for converting to a 2-digit number. The only change is that the divisor is changed from 2 to 16.
It is also 120, and The hexadecimal format is:
Divisor |
Computing process |
Vendors |
Remainder |
120 |
120/16 |
7 |
8 |
7 |
7/16 |
0 |
7 |
120 is converted to hexadecimal with the result of 78.
Take pen paper and use the form (figure: 1) to calculate the process of the above two tables.
6.4 hexadecimal Conversions
The conversion between binary and hexadecimal is important. However, there is no need to calculate the conversion between the two. Every C, C ++ programmer can see the binary number and convert it directly to the hexadecimal number.
The same is true if we finish this section.
First, let's look at a binary number: 1111. What is it?
You may need to calculate as follows: 1*20 + 1*21 + 1*22 + 1*23 = 1*1 + 1*2 + 1*4 + 1*8 = 15.
However, since 1111 is only 4 bits, we must remember the weights of each bits, and remember them from high to low: 8, 4, 2, and 1. That is, the maximum bit is 23 = 8, followed by 22 = 4, 21 = 2, 20 = 1.
Remember 8421. For any 4-bit binary number, we can quickly calculate the corresponding 10-digit value.
The following lists all possible values of the four-digit binary xxxx (skipped in the middle)
A four-digit binary number is used to calculate the hexadecimal value of a decimal number.
1111 = 8 + 4 + 2 + 1 = 15 f
1110 = 8 + 4 + 2 + 0 = 14 E
1101 = 8 + 4 + 0 + 1 = 13 d
1100 = 8 + 4 + 0 + 0 = 12 C
1011 = 8 + 4 + 0 + 1 = 11 B
1010 = 8 + 0 + 2 + 0 = 10
1001 = 8 + 0 + 0 + 1 = 10 9
....
0001 = 0 + 0 + 0 + 1 = 1 1
0000 = 0 + 0 + 0 + 0 = 0 0
To convert a binary number to a hexadecimal value, the binary number is converted to a hexadecimal value in four digits.
For example ):
1111 1101,101 0 0101,100 1 1011
F d, a 5, 9 B
When we see FD, how can we quickly convert it to a binary number?
First convert F:
When we see F, we need to know that it is 15 (maybe you are not familiar with ~ F). How can we use 8421 for 15? It should be 8 + 4 + 2 + 1, so the four digits are all 1: 1111.
Then convert D:
When I see D, how can I use 8421 to get it together? It should be: 8 + 2 + 1, that is: 1011.
Therefore, FD is converted to the binary number, which is 1111 1011.
Because the hexadecimal conversion to binary is quite direct, when we need to convert a decimal number to a binary number, we can also convert it to a hexadecimal number first, then convert it to a binary system.
For example, to convert a decimal number 1234 to a binary number, if you want to divide it by 2 all the time, you need to calculate a large number of times. So we can divide it by 16 to get the hexadecimal number:
Divisor |
Computing process |
Vendors |
Remainder |
1234 |
1234/16 |
77 |
2 |
77 |
77/16 |
4 |
13 (d) |
4 |
4/16 |
0 |
4 |
Result: The hexadecimal value is 0x4d2.
Then we can directly write the binary format 0x4d2: 0100 1011 0010.
The ing relationship is:
0100 -- 4
1011 -- d
0010 -- 2
Similarly, if a binary number is very long, we need to convert it into a 10-digit number, except for the previous method, we can also convert the binary to hexadecimal and then to hexadecimal.
The following is an example of the binary number of the int type:
01101101 11100101 10101111 00011011
We convert the four-digit group to hexadecimal: 6d E5 af 1b
6.5 original code, reverse code, and supplemental code
After the conversion of various hexadecimal formats, let's talk about another topic: source code, reverse code, and complement code.
We already know that all data in a computer is expressed in binary.
We have also learned how to convert a decimal number to a binary number.
However, we still haven't learned how to express a negative number in binary.
For example, suppose there is a number of int type, the value is 5, then we know that it is represented in the computer:
00000000 00000000 00000000 00000101
5 is converted to a binary system of 101, but the number of int types occupies 4 bytes (32 bits), so a bunch of 0 values are filled in front.
Now I want to know how-5 is represented in a computer?
In a computer, a negative number is expressed as a positive complement..
What is a supplemental code? This should start with the original code and the reverse code.
Original code: an integer that is converted into binary numbers based on the absolute value. It is called the original code.
For example, 00000000 00000000 00000000 00000101 is the source code of 5.
Reverse code: returns the bitwise result of the binary number. The new binary number obtained is called the reverse code of the original binary number.
The reverse operation indicates that the original value is 1, and the value is 0. The original value is 0, and the value is 1. (1 to 0; 0 to 1)
For example, if the bitwise of 00000000 00000000 00000000 is reversed, 00000101 11111111 11111111 is obtained.
Said: 11111111 11111111 11111111 11111010 00000000 is an anti-code of 00000000 00000000 00000101.
The anti-code is mutual, so it can also be called:
11111111, 11111111, 11111111, 11111010, 00000000, 00000000, 00000000, 00000101, and are mutually inverse codes.
Complement: the anti-code plus 1 is called a complement.
That is to say, to get a complement of a number, first obtain the reverse code, and then add 1 to the reverse code. The resulting number is called a complement.
For example, the 00000000 00000000 00000000 00000101 anti-code is: 11111111 11111111 11111111 11111010.
Then, the complement code is:
11111111 11111111 11111111 11111010 + 1 = 11111111 11111111 11111111
Therefore,-5 is expressed in the computer as 11111111 11111111 11111111 11111011. Convert to hexadecimal: 0 xfffffffb.
Let's look at how integer-1 is represented in a computer.
Suppose this is also an int type, then:
1. first obtain the original code of 1: 00000000 00000000 00000000 00000001
2. Reverse code: 11111111 11111111 11111111 11111110
3. Supplemental code: 11111111 11111111 11111111 11111111
It can be seen that the Binary Expression of-1 in a computer is full 1. Hexadecimal: 0 xffffff.
Everything is on paper ...... -1 is expressed as 0 xffffff in the computer. Can I see it with my own eyes? Of course. With the debugging function of C ++ builder, we can see the hexadecimal value of each variable.
6.6 view variable values through debugging
Next, let's complete a small experiment. Through debugging, we can observe the value of the variable.
We declare two int variables in the Code and initialize them to 5 and-5 respectively. Then, we can see the decimal and hexadecimal values of the two variables when the program is running through the debugging methods provided by CB.
Create a console project. Add the following simhei part (just one line ):
//---------------------------------------------------------------------------
# Pragma hdrstop
//---------------------------------------------------------------------------
# Pragma argsused
Int main (INT argc, char * argv [])
{
Int AAAA = 5, bbbbb =-5;
Return 0;
}
//---------------------------------------------------------------------------
We are not familiar with the line:
Getchar ();
Therefore, if you run the program at full speed, the DOS window will flash. However, today we will setBreakpointTo use the program to stop where we need it.
Set breakpoints: One of the most common debugging methods. When a program is running, it is paused at a certain code location,
In CB, you can set a breakpoint by pressing F5 on a line of code or clicking the mouse in the first line.
For example:
In, we set a breakpoint on the return 0; line. The row where the breakpoint is located is displayed in red by CB.
Then, run the program (F9) and the program will stop at the breakpoint.
(Note the differences between the two images. The preceding figure is before running, and the following figure is running. the arrow on the left indicates the row to which the operation is running)
When the program stops at the breakpoint, we can observe the visible variables in the current code snippet. There are many ways to observe variables. Here we will learn to use DEBUG inspector to fully observe a variable.
The following describes how to call up the debug Inspector window to observe a variable:
Make sure that the code window is an active window. (Click the code window with the mouse)
Press ctrl and move the mouse over the variable AAAA. You will find that the AAAA in the Code turns blue and the underline appears. The effect is like a hyperlink on the webpage, And the mouse turns into a small hand:
Click the mouse to display the AAAA check window:
(The operating system used by the author is Windows XP, and the window looks different from Win9x)
From this window, I can see:
Aaaa: variable name
INT: Data Type of the Variable
0012ff88: Memory Address of the variable. For details, see 5.2 variables and memory address. The address is always expressed in hexadecimal format.
5: This is the value of the variable, that is, AAAA = 5;
0x00000005: it is also the value of the variable, but expressed in hexadecimal notation. Because it is of the int type, it occupies 4 bytes.
First, close the debug Inspector window used to observe the AAAA variable.
Now, we use the same method to observe the variable Bbbb. Its value is-5, and the negative number is represented by a supplementary code in the computer.
As we think, the complement of-5 is: 0 xfffffffb.
Press F9 again, and the program continues to run from the breakpoint and ends.
6.7 summary of this Chapter
A difficult chapter?
Let's take a look at what we have learned:
1) We learned how to convert hexadecimal numbers to decimal numbers.
The three conversion methods are the same, and multiplication is used.
2) We learned how to convert a decimal number to a binary, octal, or hexadecimal number.
The methods are the same. Division is used.
3) We learned how to swap binary numbers and hexadecimal numbers quickly.
The trick is to convert binary numbers into hexadecimal numbers in a group of four digits.
After learning the hexadecimal number, we will use the hexadecimal number in many places to replace the binary number.
4) We learned the original code, reverse code, and complement code.
Change the value of the original code 0 to 1, 1, and 0 to obtain the reverse code. To obtain the complement code, you must first obtain the reverse code and then add 1.
In the past, we only knew how a positive integer is expressed in a computer, and now we know that a negative number is expressed using the complement code of its absolute value in a computer.
For example, how does-5 express in a computer? The answer is: 5.
5) Finally, In the hands-on experiment, we learned how to set breakpoints and how to call up the debug Inspector window to observe variables.
We will learn more debugging methods later.