Chapter 2 basics-5th. hexadecimal. 2

Source: Internet
Author: User
Tags power of power

5.8. hexadecimal. 2

5.8.1 hexadecimal

In hexadecimal notation, we will first ask: there are only 10 Arabic numerals in total: 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9, how can we express the concept of "every 16 to 1? The method is to use English letters (uppercase and lowercase)
Can be): expression a, B, c, d, e, f 10 ~ 15. So if I tell you: this is a number: "17fca0", you can certainly guess it is a hexadecimal number, but if I say
Yes: "12390", it is difficult to distinguish what the hexadecimal format is.

In C ++, writing a binary number directly in the Code is not supported (why is the post-interview question), but directly writing a hexadecimal number is supported, to distinguish it from a decimal number, C ++ requires a prefix "0x" (numbers 0 and lowercase letters X) for the hexadecimal number. For example:

Int A = 0x17fca0;

What is there already a decimal number, and we need to create another hexadecimal number? Is there a need in real life? For example, is there anything that is particularly suitable for the use of hexadecimal notation? There are actually more than 10
Base. For example, "sock" is like a binary counter: no sock is 0, 1 sock is 1, 2 sock ...... Into two: into a pair of socks, really only need 0 and 1.


TIPS: various improvements in life

The time is in hexadecimal notation and the angle is also in hexadecimal notation. When it comes to "a dozen things", it uses a 12-digit system. After class, you can think about the operating system.


The existence of a real hexadecimal system is purely binary. Binary is certainly and must exist, because it is used by machines. However, the binary number is too "occupied". Although it is the most intuitive, it does not support reading and writing.
. In decimal format -- of course, we usually use decimal to compile programs. Only when we need to express machine-related data and some memory addresses, to consider the binary-decimal number and binary conversion ratio.
Complicated: The hexadecimal format is different. The conversion between binary and binary is very simple and fast.

A number is expressed in binary format. It is actually composed of multiple "Numbers of the nth power of 2. Because 16 is the 4 power of 2, it is very easy to convert between the two hexadecimal systems (think about how many power of 10 is 2 ?).

Xu will also ask: there are still many integer sub-operators whose numbers are 2. For example, 8 is the 3rd power of 2 and 32 is the 5th power of 2. Why is it 16? Original. One byte is eight bits, but the value of "2 8" is used.
The power of power is obviously too big as the base system. In addition, we will divide a byte into "high byte (4-bit high)" and "low byte (4-bit low)", so we use 16 (4 power of 2) as the hexadecimal most
Convenience. Let's give it a try. It will be clearer: Let's perform some swap operations between binary and hexadecimal numbers.

  • Binary-> hexadecimal

We will first take "half a byte (4 bits)" as an example to see how to convert the binary number of 4 bits to hexadecimal. Because it was just the beginning, we kept the intermediate steps to convert to decimal, but slowly we had to learn to convert directly.

1000 (2) => 8 + 0 + 0 + 0 = 8 => 0x8

1001 (2) => 8 + 0 + 0 + 1 = 9 => 0x9

1010 (2) => 8 + 0 + 2 + 0 = 10 => 0xa

1111 (2) => 8 + 4 + 2 + 1 = 15 => 0xf

The last example illustrates the problem: to convert the binary number, take four digits as a group, and then see the low value from the high position. The weights are as follows: "8, 4, 2, 1 ". If this bit is 1, add the weight value. Otherwise, you can get a decimal number and then quickly convert it to hexadecimal (you must note ~ F ).

It can truly reflect the convenience of the hexadecimal system, because there are more digits, for example, when facing a complete byte, for example (to avoid vertigo, we add a space for every 4 bits ): 0101 1110. How can I convert the binary number into a hexadecimal number?

Method 1: convert to a decimal number and then to a hexadecimal number:

First convert to hexadecimal: 0101 1110 = 0 + (6 to the power of 2) + 0 + (4 to the power of 2) + (3 to the power of 2) + (2 to the power of 2) + (power 1 of 2: 2) + 0 = 94.

How can we convert 94 in decimal format to hexadecimal format? We haven't taught it yet. Here we will first give the answer: 94 = 0x5e.


[Classroom assignment]: A "stupid way" to convert the binary number into a 10-digit System"

Please pick up the pen and immediately use step 1 in the above method to convert the following binary numbers into decimal numbers:

8 digits: 11011100, 01110110, 10010101, 11011101;

16-digit: 1101101000110101


Method 2: convert each 4 bits to a hexadecimal number. The result is as follows:

4-digit Height: 0101 (2)-> 4 + 1-> 0x5

4 lower bits: 1110 (2)-> 8 + 4 + 2-> 0xe

The result is 0x5e.

But how can we convert a 16-bit binary number to 1101101000110101 to a hexadecimal number?

First, each 4 digits is split into a group: 1101 1010 0011 0101

1101-> 13-> 0xe

1010-> 10-> 0xa

0011-> 0x3

0101-> 0x6

The result is 0xea36.

  • Hexadecimal number> binary number

There are two ways to convert hexadecimal numbers into binary numbers. However, we can directly learn the "smart" method and the "stupid" method, it will be provided later when "hexadecimal conversion" is used.

The above section provides the "8, 4, 2, and 1" formula. Now you only need to learn to give a number within 16. You can spell out its binary number, for example:

0xa-> 10 = 8 + 2-> 1010 (2)

0xd-> 13 = 8 + 4 + 1-> 1101 (2)

0xf-> 15 = 8 + 4 + 2 + 1-> 1111 (2)

Another example is:

0xc9d5 should be converted to binary, similar:

0xc-> 12 = 8 + 4-> 1100 (2)

0x9-> 8 + 1-> 1001 (2)

0 x D-> 1101 (2)

0x5-> 4 + 1-> 0101

Result: 11001001 11010101. Pay special attention to the final 0x5 conversion. If the number is less than 4 digits, add 0.


We have been talking about how to swap the hexadecimal content with binary? That's right, because when we program, the vast majority of us need to regard hexadecimal as a binary vest-put the binary number on this vest, the figure is to make humans look more comfortable, that's all.

5.8.2. Gossip

Babu, another small-size vest of binary. But we do not use much. In the C ++ program, When a number starts with 0, it is octal. This is not intuitive.

Int A = 010; // sorry, this is not 4, it is not 10, it is 8.

Octal, 0 ~ 7. These 8 Arabic numerals. If we see the "10" of the octal value, you can calculate that it is actually a decimal value of 8. The weights of all October users are 8-based. The formula below demonstrates how to convert 173 of the octal to 123 in decimal format.

173 (8)-> 1 x (power 2 of 8: 64) + 7 x (power 1 of 8: 8) + 3-> 123 (10)

Let's take a look: how to replace a binary number with an octal value: "11101111"
Is an 8-bit binary number. From the low position to the high position, every three digits are divided into one group (think about why every three digits), get: 11, 101, 111, and then replace each simulation with a decimal (but three-bit binary)
The number must not exceed 7. Therefore, it may be better understood to convert it into an octal value ):

11 (2)-> 3

101 (2)-> 5

111 (2)-> 7

Result: 357 (8 ).

The octal number is converted to the binary number. Note the octal number, the binary value of each number:

7-> 111 (2) 3-> 011 (2)
6-> 110 (2) 2-> 010 (2)
5-> 101 (2) 1-> 001 (2)
4-> 100 (2) 0 000 (2)

(Table 4 octal and binary speed change tables)

Maybe, you have a flash in your mind. Isn't there a way to swap the octal number with the hexadecimal number?

5.8.3. hexadecimal conversion

Let's talk about the answer to the previous section. One of the ways to swap the octal number with the hexadecimal number is to use binary as a bridge.

The conversion process we have learned is as follows:

Binary-> octal and hexadecimal

Hexadecimal, octal-> binary

Binary, octal, hexadecimal-> decimal

In this section, we will mainly learn how to convert the decimal number into other hexadecimal values. They all have a unified method called "division ".

  • Decimal number> binary number

Divide the decimal number by 2, get the quotient and the remainder, divide the quotient by 2 until the quotient is 0, and then divide the remainder obtained in this process (either 0 or 1 ), the result is the result.

Example: convert 6 to binary:

Expression Vendors Remainder
6 limit 2 3 0
3 limit 2 1 1
1 limit 2 0 1

Table 5: Convert decimal number 6 to binary Process Table

Sort the remainder in descending order to get "110", which is the Binary Expression of 6.

If you want to take the test, drawing the table slowly will obviously be in a hurry, so it is generally calculated on the draft paper as shown in:

Figure 5-22 diagram of the Division calculation process


  • Decimal number-> octal number

The method is similar to converting it to the binary number, except that the divisor is changed to 8.

Example: Convert 289 to octal:

Expression Vendors Remainder
289 listen 8 36 1
36 then 8 4 4
4 then 8 0 4

Table 5-6 decimal to octal conversion example

Sort the remainder in descending order to get "441", which is exactly the octal expression of 289.

  • Decimal number-> hexadecimal number

The method is similar to converting it to the binary number, except that the divisor is changed to 16.

Example: Convert 872 to hexadecimal number:

Expression Vendors Remainder
874 usd16 36 1
54-16 4 4
3-16 0 4

Example of convert table 5 to 7 in decimal to hexadecimal format

Sort the remainder in descending order to get "0x36a", which is expressed in hexadecimal 874.

5.8.4. Floating Point Number

In C ++ programs, it is common to access an integer (especially an unsigned integer) in the form of "bits, for example, the "displacement" Operation (moving each bit in a binary number to the right at the same time), or bitwise "and" or "," exclusive or. However, we seldom perform such operations on a non-integer.

Floating Point/floating point
Numbers is a method used by computers to "Approximate" real numbers. The "scientific notation" for studying in middle school is a bit similar. In addition, neither the "Floating Point" nor "N-hexadecimal" can be a table.
There are two different entry points of the same number. In other words, there are decimal floating point numbers and binary floating point numbers. Otherwise, let's look at the floating point representation of a decimal number:

9.57 × (2nd power of 10)

What is this number? In fact, it is the floating point representation of the real number "957.0.

A standard floating point expression must meet the following requirements.

Expression: D. dd... d × (B's E Power ).

Among them, D. dd... d is called "tail number" (more commonly referred to as "valid number"), B is called "base number", and E is called "Index ".

For different hexadecimal values, the first result is the value of B. B is 2, which is binary, 10, or decimal. Of course, the value range of each d in the "tail" should also change with the hexadecimal value. If it is a decimal number, D is 0 ~ 9. If it is a binary number, D can only be 0 ~ 1.

In addition, the integer part of "tail" (that is, the left part of the decimal point) cannot be 0. Therefore, similar to the following numbers, they are not standard floating point numbers:

10.123 × (2nd power of 10)

2.0101 × (Power 3 of 2)

0.0134 × (10-1 power)

Classroom assignments: standard expression of Floating Point Numbers

Indicate the error of the three floating point expression and write it in the correct way (the value size must be consistent ).


If Binary Expression is used explicitly, it is: D. dd... d × (the power of e in 2 ). Each digit D can only be 0 or 1. Similarly, the integer part cannot be 0.

The first standard floating point number can be calculated using the following expressions:

+ (D0 + D1 × (power-1 of B) + D2 × (power-2 of B) + D3 × (power-3 of B) +... + DN × (B's-n) × (B's E Power)

Don't be intimidated by the length and formula! It just reminds us of elementary school time. Let's take an example. assume there is a decimal floating point: 3.456 × (10 to the power of 3)

When "0.1" is used to represent "10 to the power of 1", there are:

3.456 = 3 + 4x0.1 + 5x0.01 + 6x0.001

So: 3.456 × (the 3rd power of 10) = (3 + 4 × 0.1 + 5 × 0.01 + 6 × 0.001) × (the 3rd power of 10 ).

In elementary school, we all read: "the first digit after the decimal point represents 0.1, the second digit represents 0.01, the third digit represents 0.001 ......" What about binary? Assume that a decimal binary is also represented in the most intuitive way:


2 to the power of 0 (= 0), 1 to the power (= 1), 2 to the power (= 4), 3 to the power (= 8 )......

Decimal point to the right, it is --

-1 to the power of 2 (= 0.5),-2 to the power (= 0.25),-3 to the power (0.125),-4 to the power (= 0.0625 )......

Classroom assignments: 0.1 how to express it?

You do not need to write "tail" or "Index". For example, write the decimal part of decimal number 0.1 in binary format: 0.000 ______.


Obviously, the three digits after the decimal point must all be 0, because their weights are larger than 0.1, and then they can be pieced together like this:

First, let's make the-4 power of 2, plus the-5 power of 2:

0.0625 + 0.03125 = 0.09375.

It's close, but we can't add 2 to the power of 6 (0.015625). In addition, we can add more than 0.1. In addition, we can add 2 to the power of 7 (0.0078125 ...... Add 2 to the power of 8
(0.00390625), get 0.09765625, more and more approaching, but it is not 0.1 after all. It doesn't matter. If we keep piecing together, we may just be able to make a 0.1. If
Can't we just get it together? It doesn't matter. Let's use the mathematical knowledge of high school. If we are allowed to be infinitely -- Attention, infinitely -- together, then all numbers can be approached, until being recognized in Mathematics
Is equal.

TIPS: 0.9999999 ...... Is it equal to 1?

When I was studying "Limit" in high school, the math teacher must have told you that when I was 0.9999 ...... There is an infinite number of 9 hours behind, so 0.9999999 ...... It is equal to 1.


Unfortunately, the "Limit" exists in the world of mathematics. In reality, the memory capacity of a computer is not only "endless", but also precious. So, let's say again: "floating point numbers are a method that computers use to 'Approximation 'to express real numbers ". If it is stored in four bytes, the total length is 32 characters.

However, it seems that not all numbers cannot be accurately expressed using floating points. For example, 0.5 is the power of-1 of 2, which is very accurate. However, this simply means that when you write a 0.5 code,
It is accurate. If it is obtained through a "formula", there are some floating point numbers that cannot be accurately expressed in this formula, such as 0.01 × 10. 0 limit 0. 2. No one can guarantee it.
The computer gets an exact 0.5. This is the precision that needs to be paid attention to when comparing floating point numbers. Please refer to the following code:

    if (0.01f * 10.0f / 0.2f == 0.5f)
cout << "yes!" << endl;
cout << "no." << endl;

C ++
The program uses * to represent the multiplication number, And/to represent the division number. F at the end of "0.01f" indicates that this is a single-precision floating point number. When the above program runs, "No." will be output .". That is to say, at this time
"0.01*10. 0 limit 0. 2" is not equal to 0.5. How can this problem be solved? We are not eager to give an answer here. What matters is that you must remember this question and understand its causes.

Tip: is the computer untrusted?

Some students once asked me that I always thought that computer computing is much more precise than humans.
But today it seems that computer computing is not credible? Even a primary school student can make a mistake? Don't worry, although the floating point number is not accurate, it is also a kind of "very accurate and inaccurate", the problem is completely feasible.
Traceable query can solve the problem. In addition, I see some programmers who do not know why, I think to write such a judgment: "If (0.1 = 0.1)
... ", You have to worry about it for a long time. Don't worry. If such a judgment fails, the computer can really throw it.


Computers usually provide two precision floating-point Expression methods, one of which is not accurate, and the other is not accurate-oh, this is a bit negative, come back: "One of them is not accurate, the other is more accurate. ". Not accurate: "Single-precision floating point number", more accurate, is "double-precision floating point number ".

  • Single-precision floating point number

C ++ uses the float type to represent a single-precision floating point number. It occupies 4 bytes and combines 32 bits. The bit fields occupied by symbols, indexes, and tails are shown in:

Figure 5-23 Single-precision floating-point Fields


If you write a floating point number of a literal constant in the code, add the letter F at the end of the number, for example, 0.1f or 10f. By default, c ++ uses a small number as a double-precision floating point number.

  • Double-precision floating point number

C ++ uses the double type to represent double-precision floating-point numbers. It occupies 8 bytes and is 64-bit. The bit fields occupied by symbols, indexes, and tails are shown in:

Figure 5-24 double-precision floating-point Fields


If you write a number in the code without decimal digits, the default value is integer. For example, 10. If you add a decimal point, for example, 10.0, and no trailing letter F, c ++ treats it as a double type.

The above is not all about floating point expression. Many conventions are required to express a floating point number. When writing a C ++ program, we only perform bitwise operations on the data of the integer type (including int, long, Char, and so on, we will only talk about this for the time being.

Classroom assignments: floating point Precision exercises

Use the aforementioned "0.01x10. 0 limit 0. 2" code to determine whether it is equal to 0.5 code and write it into a console test project. Test the float and double types to see if the results are different.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.