A summary of computer number generation and operation.

Source: Internet
Author: User

I.Why is there 10?Hexadecimal, 2Base, 8Hexadecimal, 16Base?

10 is a very important number for us. 10 is the number of fingers or toes that most of us possess, So we humans have adapted to a 10-based digital system. the commonly used numeric system is called a 10-based numeric system or decimal system. This digital system is very natural for us, so it is hard to imagine that there are other digital systems. If humans don't have so many fingers, the way we count will be different. The number 10 may represent something else. When we understand that 10 can mean only two ducks, we can also explain how switches, wires, bulbs, relays (or simply computers) represent numbers.

 

What if humans have only four fingers on each hand like a crab? We may never think of a 10-based digital system. Instead, we may think that the 8-based digital system is normal, natural, reasonable, and inevitable, it is undoubtedly appropriate. In this case, it cannot be called decimal. It must be called as an 8-based digital system or octal.

 

If the digital system is 8-based, this symbol is not needed: 9

If you show this symbol to any crab, the same reaction will occur: "What is that? What is it ?" If you think about it for a while, you will find that such a character is not required: 8

In the decimal number system, there is no symbol specifically used to represent 10. In the octal digit system, there is no symbol specifically used to represent 10.

 

In a decimal number system, the numbers are 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9, followed by 10. In the octal Digital System, the number is 0, 1, 2, 3, 4, 5, 6, and 7. What then? We have no symbols available. The only meaningful available symbol is 10, indeed. In October 8, the number after 7 is 10, but 10 is not the number of human fingers.

 

Assume that we are a dolphin and we must use two fins to count, then this digital system is based on two digital systems or binary systems. It seems that only two numbers are needed, namely 0 and 1. Now, 0 and 1 are all the problems you need to solve. You need to practice to get used to binary numbers. The biggest problem with the binary number is that the number is used up quickly.

 

Yes. In binary, the number after 1 is 10. This is surprising, but it is not surprising. No matter which digital system is used, when a single digit is used up, the first two digits are 10. In a binary system, you can calculate the number as follows:

0 1 10 11 100 101 110 111

 

These numbers seem very large, but they are not. To be more accurate, the length of a binary number increases faster than the length of a binary number increases. This is a bit like eating with a toothpick. The thorn block is very small and laborious, it takes a long time to eat a meal.

 

When we write a relatively large number in decimal, there is usually a gap between every three numbers. In this way, we can see the approximate value of this number at a glance. For example, if you see the number 1 2 0 0 0 0 0 0, you may have to count the number 0, but if you see 120 000, then we can immediately know that it is 0.1 billion 20 million.

The length of the bit in the binary number increases extremely fast. For example, the binary representation of 0.1 billion 20 million is:

1 0 11 0 111 0 0 0 11 0 0 0 0 0 0 0 0. To make it easier to read, it is usually separated by a hyphen or space between every four digits. For example, 1 0 11-0 111-0 0 0 1-1 0 11-0 0 0 0-0 0 0.

 

It is impossible to find a digital system that is simpler than a binary system. The binary digital system bridges the gap between arithmetic and electricity. All the switches, wires, bulbs, relays, and other objects we see can represent binary numbers 0 and 1:

 

A wire can be a binary number. If there is a current flowing through the wire, it indicates the binary number 1; if there is no, it indicates the binary number 0.

The switch can represent binary numbers. If the switch is closed, the value 1 indicates the binary number. If the switch is closed, the value 0 indicates the binary number.

The light bulb can represent binary numbers. If the light bulb is on, it indicates the binary number 1; if it is not on, it indicates the binary number 0.

Telegraph relays can represent binary numbers. The relay is closed, indicating the binary number 1; the relay is disconnected, indicating the binary number 0.

 

It can be seen that the binary number is closely related to the computer!

 

About 4 8 years ago, the American mathematician John Wilder Tukey (born in 1 5 years) realized in advance that binary numbers will play a greater role with the prevalence of computers in the next few years. He decided to create a new, shorter word to replace binary digit, which is not flexible to use. He once considered using B I g I t or B I n I t, but finally he chose the short, simple, exquisite and very cute word bit) to replace binary digit.

 

Why do we introduce Octal numbers and hexadecimal numbers?

The binary number is lengthy, error-prone, and hard to remember. The conversion process between the decimal number and the binary number is complex. Therefore, the hexadecimal number or octal number is generally used as the abbreviation of the binary number.

 

Ii. 2Hexadecimal operation

1. Binary addition, subtraction, multiplication, division

In binary, there are only two numbers 0 and 1, which have the following addition and multiplication

0 + 0 = 0

0 + 1 = 1

1 + 0 = 1

1 + 1 = 10

 

0*0 = 0

0*1 = 0

1*0 = 0

1*1 = 1

 

The rule is similar to the operation rule of the decimal number (the difference is 1 + 1 = 10), but it is much simpler than the addition and multiplication tables of the decimal number.

 

We know that in decimal format, the rule "every ten to one" and "every ten" is used. in binary, we can perform addition, subtraction, multiplication, division, and arithmetic operations on binary numbers based on the "when two goes into one" and "when two.

 

11011

* 101

--------------

11011

11011

--------------

10000111

 

Using binary, the multiplication speed is improved, because if the multiplier is 1, you can write the multiplier at the corresponding position without changing it, and you can skip several zeros in the multiplier.

 

2. binary logic multiplication and non-Calculation

In addition to arithmetic operations, there is also a logical operation, namely logical addition, multiplication, and non.

Logical addition is bitwise, but not carry-in. Logical multiplication is bitwise. Logical addition is bitwise inversion.

 

For these three logical operations, there are three basic logic lines in the computer, "and" Door, "or" door and "Non" door, usually called door circuit.

The "or" gate is to complete logical addition operations, "and" gate is to complete logical multiplication operations, and "Non" is to complete logical non-calculation.

If there is a signal indicating 1, and no signal indicates 0, then or, And, non-gate can complete logic addition, multiplication, and not three operations.

In addition, there are some derived logical operations, such as an operation that implements a logic that must be different, otherwise there will be no such operation.

For example, if an exclusive or operation is performed on 1010 and 0110, the result is 1100.

1010

0110

-------

1100

 

In the computer, binary addition, subtraction, multiplication, division, and arithmetic operations can be implemented using logical lines composed of gate circuits.

 

III.How to implement binary addition, subtraction, multiplication, division, and arithmetic operations on logical lines of a door circuit

All binary operations can be implemented using the and, or, instead of the three basic gates. the logical circuit is a combination of the three basic doors used to calculate specific functions. Different operations can be achieved by selecting different functional logical circuits.

 

Now the addition is used as an example to describe:

Binary addition carry table

+ Carry 0 1

0 0 0

1 0 1

The calculation result is exactly the same as that of the gate. How is it? We can use a and gate to represent the binary addition of the carry, and use or gate to express the binary addition, or the door can not express the carry is easy to use and the door to make up, such a perfect addition is born. the addition that can calculate two 1-bit bits and the carry-in bits are called the half-processors. The addition that can calculate two 3-bit bits and the carry-in bits are called the full processors.

 

4. Conversion between different hexadecimal formats

When we use a computer to solve the problem, the numbers we input are generally decimal numbers, but the computer does not know decimal. Therefore, we must first convert the numbers into binary numbers to be accepted by the computer. Likewise, the results calculated by the computer are binary numbers, which are inconvenient for humans to read. Therefore, we also need to convert binary numbers into decimal numbers that people are used, there is a conversion between 10-> binary, and 2-> decimal. Although we do not have to understand the principle above, we can still use computers, but for those who want to understand the principle behind the computer, it is necessary to understand it. Let's look at the conversion problem between different hexadecimal numbers.

 

1. Conversion between decimal number and binary, octal, and hexadecimal number

1.1 decimal number to binary, octal, and hexadecimal number

Integer, except for the base, such as (, 16.

1.2 binary, octal, and hexadecimal conversion to decimal

Expand by right.

 

2. Conversion between binary and octal

2.1 binary to octal

Three digits in a group, expand according to the permission, and add them together.

 

2.2 octal to binary

Each octal number is represented by a three-digit, two-digit number, and then connected together.

 

3. Conversion between binary and hexadecimal

3.1 convert binary to hexadecimal

A group of four digits, expand according to the permission, and add them together.

 

3.2 hexadecimal number converted to binary number

Each number is represented by a 4-digit, 2-digit number, and then connected together.

 

4. decimal part Conversion

The base number R (r = 2, 8, or 16) is used to constantly multiply n decimal places, until the fractional part of the product is zero (or until the required number of digits), the integers multiplied each time are sequentially arranged as numbers in the corresponding hexadecimal format. The first obtained is the highest valid number, and the last obtained is the lowest valid number.

 

References:

The mysteries of coding.

 

Appendix: Examples of Digital Conversion

1. r base → decimal

(1) method: people are used to decimal numbers. If the r base number is converted to the equivalent decimal number, as long as the r base number is expanded with the bitwise permission, then, the decimal number is obtained, which is expanded according to the power series.

(2) Example:

For example, 2.1.3 convert binary number (11011.101) 2 to a decimal number.

Solution: (11011.101) 2 = 1x24 + 1x23 + 0x22 + 1x21 + 1x20 + 1X2-1 + 0x2-2 + 1X2-3

= 16 + 8 + 0 + 2 + 1 + 0.5 + 0 + 0.125

= (27.625) 10

Example 2.1.4 convert the octal number (136.524) 8 to the decimal number.

Solution: (136.524) 8 = 1x82 + 3x81 + 6x80 + 5x8-1 + 2x8-2 + 4x8-3

= 64 + 24 + 6 + 0.625 + 0.03125 + 0.0078125

= (94.6640625) 10

 

For example, 2.1.5 converts a hexadecimal number (13df. B8) 16 [= (13df. B8) H] to a decimal number.

Solution: (13df. B8) 16 = 1 × 163 + 3 × 162 + 13 × 161 + 15 × 160 + 11 × 16-1 + 8 × 16-2

= 4096 + 768 + 208 + 15 + 0.6875 + 0.03125

= (5087.71875) 10

2. Decimal → R hexadecimal

(1) method: Convert the decimal number to the R-base number. Convert the integer part and the decimal part respectively, and then combine them.

To convert a decimal integer to a decimal number, divide the number by the base number r to obtain the remainder:

(A) divide the given decimal integer by R, and the remainder is used as the decimal digit (LSB) of R ).

(B) divide the quotient of the previous step by R, and the remainder as the secondary low.

(C) Repeat Step (B) and write down the remainder until the end operator is zero. The remainder is the highest digit (MSB) in the R-base system ).

Convert a decimal number to a decimal number in R. Multiply the decimal part by R to obtain an integer:

(A) Multiply the given decimal place by R. The integer part of the product is the highest digit of the decimal part of R;

(B) Multiply the decimal part of the product of the first step by R, and the integer part of the product is used as the decimal high of R;

(C) Repeat (B) until the fractional part of the product is 0 or reaches a certain precision.

Note: The method for determining the number of decimal places by precision

Because the precision of a decimal place in R is R-1, the precision of N decimal places is r-N, so the number of decimal places N should meet:

R-N ≤ given precision

N is the smallest integer that satisfies the preceding formula.

(2) Example

For example, 2.1.6 converts the decimal number (25) to the binary number.

Solution: Because the binary base is 2, divide by 2 and take the remaining number (0 or 1 ):

 

So

(25) 10 = (11001) 2

 

For example, 2.1.7 converts the decimal number (25) 10 to the octal number.

Solution: Because the base number is 8, divide the number by 8 to get the remainder:

 

So

(25) 10 = (31) 8

 

For example, 2.1.8 converts a decimal (0.375) 10 to a binary number.

Solution:

 

(0.375) 10 = (0.011) 2

 

For example, 2.1.9 converts a decimal (0.39) 10 to a binary number with a precision of 2 to 10.

Solution: Because the accuracy is required to reach 0.1%, 2-10 <0.1, so it must be accurate to 10 decimal places.

0.39x2 = 0.78 B-1 = 0

0.78x2 = 1.56 B-2 = 1

0.56x2 = 1.12 B-3 = 1

0.12x2 = 0.24 B-4 = 0

0.24x2 = 0.48 B-5 = 0

0.48x2 = 0.96 B-6 = 0

0.96x2 = 1.92 B-7 = 1

0.92x2 = 1.84 B-8 = 1

0.84x2 = 1.68 B-9 = 1

0.68x2 = 1.36 B-10 = 1

So

(0.39) 10 = (0.0110001111) 2

For example, 2.1.10 converts a decimal (0.39) 10 to an octal number, which must be accurate to 0.1%.

Solution: Because 8-4 <0.1, so the need to be precise to the octal decimal 4 decimal 0.39x8 = 3.12 B-1 = 3

0.12x8 = 0.96 B-2 = 0

0.96x8 = 7.68 B-3 = 7

0.68x8 = 5.44 B-4 = 5

So

(0.39) 10 = (0.3075) 8

To convert a decimal number with an integer or decimal number to an R-based number, convert the integer part and the decimal part respectively, and then combine the results. For example, convert the decimal number (25.375) 10 is converted to the binary number. The result is (11001.011) 2.

3. Conversion between different hexadecimal formats whose modulo is 2N

(1) method: because three binary numbers constitute one octal digital, four binary numbers constitute one hexadecimal digital. Therefore, the conversion between 2 m and 2n can convert the 2 m hexadecimal number to binary, and then convert the binary to 2N.

(A) 2 m hexadecimal conversion to 2 m only requires converting the 2 m hexadecimal digital into the corresponding binary number.

(B) When the binary value is converted to 2N, the integer part is converted from the low position to the n-bit binary value to the corresponding 2n hexadecimal number. If the number of digits is insufficient, zeros can be filled in front. For decimal parts, each n-bit binary is converted to the corresponding 2n hexadecimal number. If the number of digits is not enough, zeros can be filled in the back.

(2) Example:

For example, 2.1.11 converts binary number (110100.001000101) 2 to octal.

Binary Number 110 100 001 000 101

Eight shards 6 4 1 0 5

(110100.001000101) 2 = (64.105) 8

 

For example, 2.1.12 converts the binary number (110100.001000101) 2 = (64.105) 8 to hexadecimal.

 

Binary Number 11 0100 0010 0010 1000

Hexadecimal number 3 4 2 2 8

(110100.001000101) 2 = (34.228) 16

Related Article

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: info-contact@alibabacloud.com 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.