Computer knowledge that programmers should understand (2) -- A Number System

Source: Internet
Author: User
Tags binary to decimal decimal to binary

Introduction

As we all know, a computer system uses a binary number system, and the machine code and command code it can recognize are all sequences composed of 0 and 1. In our daily life, we use a decimal number system. Why can't computers only use binary systems? This is related to its composition structure. We know that computers are made up of electronic components, the states of electronic components, such as power-on and power-off, usually have two sides. Binary Systems are easier to explain the states and features of electronic components.

When it comes to a number system, what is a number system? In daily life, what number system is most used? What happened to the binary system? See the breakdown below.

Number System

The number system is purely a mathematical theory. Here I will introduce it in a simple way.

1. About the number system

The number system uses a fixed symbol and a unified rule to represent the value. It is a counting rule. Common numeric systems include binary, octal, decimal, and hexadecimal. The computer uses a binary number system, which is commonly used in real life as a decimal number system. As a matter of fact, we can see that they all use a carry-in method for counting. For example, binary goes into two places and decimal goes into ten places. This method is called the carry-in system and also becomes a carry-in notation, it is a counting method that can use a limited number to represent all numeric values. The number of available numeric symbols is referred to as the base number or base number. If the base number is n, it can be referred to as the n-carry system or the n-base system.

Digital, base, bit and factor terms are often used to describe a number system.

Digital is a number symbol that represents the size of a base value. For example, there are ten digits in decimal format: 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. The binary type has two digits, the values are 0 and 1 respectively.

The base number, also known as the base number, is the number of digits used in the number system. Generally, the base number is used to define the number system. For example, the base of decimal is 10, which is called a decimal number system. The base of binary is 2, which is called a binary number system.

Bitwise is the rate value of a digital number at different positions, that is, the value of the value indicated by 1 in a digit system ). For example, the bitwise of and 1 in decimal format is, the bitwise of 2 is 10, and the bitwise of 3 is 1; the bitwise of 10 and 1 in binary format is 2, and the bitwise of 0 is 1. For an n-base value, the I-th digit of the integer is given the right to the I-1 of n, that is, n (I-1 ), the decimal part is the right place of the j-digit with the decimal point as the origin. the decimal point is the-j power of n, that is, n (-j ). N indicates that the decimal point is the origin point and is derived from both sides .) Decimal 2... 5.0... 5. The analysis is as follows... Number omitted)

650) this. length = 650; "src =" http://www.bkjia.com/uploads/allimg/131227/162FW3b-0.png "title =" "width =" 500 "height =" 182 "border =" 0 "hspace =" 0 "vspace =" 0 "style =" width: 500px; height: 182px; "alt =" 2152192.16.png"/>

In fact, the value of each digit is equal to the value of the digit itself multiplied by the bit right. For example, a single digit, ten digits, hundreds of digits, thousands of digits, and tens of thousands of digits can be used in decimal notation ..., Very bit, percentile, kilobytes, ten thousandths... To indicate bitwise. The bitwise is 100, and the 10th is 101 ,... The bitwise is 10-1.

A factor is a multiple relationship. If an integer n is divided by m and the result is an integer without a remainder, m is a factor of n and n is a multiple of m. Only when the divisor, divisor, and quotient are integers, the remainder is zero.

After the above introduction, you can simply sort out the numerical expression methods in the numerical system. For an I-digit integer, the n-base values of the j-decimal places can be expressed:

K = (AiAi-1... A2A1. A1A2... Aj-1Aj) n

= Ai × ni-1 + Ai-1 × ni-2 +... + A2 × n1 + A1 × n0 +. + A1 × n-1 + A2 × n-2 +... + Aj-1 × n-(J-1) + Aj × n-j

I indicates the number of digits of the integer. It is calculated from the first digit on the left of the decimal point, which is I = 1, I = 2 ,...; J indicates the number of decimal places. It is calculated from the first place on the right of the decimal point, which is j = 1, j = 2 ,...; A Indicates the digital value in the hexadecimal number; Ai indicates the I-digit digital value in the integer part. For example, I = 1 indicates the first digit on the left of the decimal point; aj indicates the j-digit number of the fractional part. For example, j = 1 indicates the first digit on the right of the decimal point, and n indicates the base number.

2. Introduction to common Number System
  • Binary

Binary) is a Binary system that uses 2 as the base and 0 and 1 as the digital base, abbreviated as B or BIN. Binary is composed of two basic numbers 0 and 1. The computing feature is "every two to one" and "every one to two ". Binary Systems are often used to transmit or store data in electronic devices such as computers. Binary Systems only contain two characters, 0 and 1, which indicate different stable states of components. Binary computation is simple.

  • Octal

Octal Al) is a digital base system based on 8, 0, 1, 2, 3, 4, 5, 6, and 7, abbreviated as O or OCT. The computing feature of gossip is "every eight times" and "every eight times ". The eight digit system is often used in computer computing. It is suitable for computer systems with 12-bit, 36-bit, or a multiple of three other digits.

  • Decimal

Decimal) is a digital Number System Based on 10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. Decimal calculation is characterized by "every ten to one" and "every ten ". Decimal is the most commonly used number system.

  • Hexadecimal

Hex in hexadecimal format) is based on 16, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F are digital hexadecimal systems, the abbreviation is H or HEX. The hexadecimal computation feature is "hex-Hex ". Hexadecimal systems are commonly used in advanced computer programming languages and are often used to represent big data.

We will not introduce each hexadecimal operation here. Remember to "next N" or "borrow one as N. You only need to know its addition and addition operations. Other operations can be divided into addition and subtraction operations.

3. Mutual conversion between numeric Systems

Mutual conversion between numeric systems may be a key point. But it is also purely mathematical knowledge. If you do not understand it, you should study mathematics well. Here we will briefly summarize the common methods. The specific example is omitted. Please install the Rules yourself .)

3. 1. Low-to-high conversion

You can use the weighted sum method to convert any n-hexadecimal value to m> n, after the n-base value is expanded based on the n-base bitwise weight, the values are summed according to the m-base addition Rules, the n-base value is represented by m-base. Conversion Method

650) this. length = 650; "src =" http://www.bkjia.com/uploads/allimg/131227/162FU2C-1.png "title =" low to high "width =" 500 "height =" 119 "border =" 0 "hspace =" 0 "vspace =" 0" style = "width: 500px; height: 119px; "alt =" 223605281.png"/>

This method is suitable for converting from a low-income to a high-income value. In the figure, m must be an integer greater than n. This method can be expressed:

(AiA (I-1 )... A2A1. A1A2... Aj-1Aj) n = Ai X ni-1 + Ai-1 X ni-2 +... + A2 × n1 + A1 × n0 +. + A1 × n-1 + A2 × n-2 +... + Aj-1 × n-(J-1) + Aj × n-j = (...) M

. Convert high to low

If any n-base value is converted to m-base m <n,), the n-base value can be converted to a decimal value, then convert the decimal result value to the m-base value. The base division Multiplication can be used to convert a decimal value to any m-base value. The integer part and the decimal part must be converted separately. The integer part uses the base division method to divide the m-decimal part ), the fractional part uses the basic multiplication m to take the integer), and then merges the obtained results according to the regular law.

1. base Division except m to take the remainder method) -- similar to short division, that is, remove the decimal integer part using m until the end of the quotient is zero, and sort the remainder of each step in reverse direction, the m-base representation of the integer is obtained. Let's look at the idea of dividing expression extraction by m:

(K) 10 = m0 × (Ai × mi-1 + Ai-1 × mi-2 +... + A2 × m1 + A1 × m0)

= M1 × (Ai × mi-2 + Ai-1 × mi-3 +... + A2 × m0) + A1

= M2 × (Ai × mi-3 + Ai-1 × mi-4 +... + A3 × m0) + A2 + A1

......

= Mi-1 X (Ai x m0) + Ai-1 +... + A2 + A1

= Ai + Ai-1 +... + A2 + A1

This is just a simulation process. Each step is divided by one m to extract one m) to obtain the remainder Ai and splice the remaining integers. In the expression, "+" indicates the Concatenation symbol.

2. base multiplication multiplied by m to take the integer method) -- that is, use m to take the decimal part, until the product has no decimal point, that is, the decimal part is zero, or the cyclic body or the expected precision position appears, sort the integer parts of the product obtained by each step in the positive direction. Then, the m-base representation of the fractional part is obtained. Let's take a look at the following expression multiplied by m:

(K) 10 = m0 × (A1 × m-1 + A2 × m-2 +... + Aj-1 x m-(J-1) + Aj x m-j)

= A1 + m1 × (A2 × m-1 +... + Aj-1 x m-(J-1) + 1 + Aj x m-j + 1)

= A1 + A2 + m2 × (A3 × m-1 +... + Aj-1 x m-(J-1) + 2 + Aj x m-j + 2)

......

= A1 + A2 +... + Aj-1 + MJ-1 × (Aj × m0)

= A1 + A2 +... + Aj-1 + Aj + mj × 1

......

This is just a simulation process. Every step is multiplied by a m to extract one m. Then the integer Aj is obtained. The fractional part of the product is calculated. The final integer is spliced. In the expression, "+" indicates the Concatenation symbol.

Conversion Method for converting n-base to m-base m <n)

650) this. length = 650; "src =" http://www.bkjia.com/uploads/allimg/131227/162FS095-2.png "title =" high to low "width =" 500 "height =" 467 "border =" 0 "hspace =" 0 "vspace =" 0" style = "width: 500px; height: 467px; "alt =" 233416964.png"/>

This method is applicable to the conversion from high to low. In the figure, m must be an integer smaller than n.

3. Introduction to special methods

A special bitwise method can be used to quickly convert the conversion between values in the n-base system and values in the positive integers in the nKK system. If m = nK is set, then the relationship between n and m is nK = m1, then the correspondence between n and m exists, that is, the K in n corresponds to the 1 in m.

If nKK is a positive integer, the NKS can be directly used as a positive integer when the hexadecimal value is converted to an n-hexadecimal value) each digit in the hexadecimal value is converted to the value corresponding to the nth value. The conversion process is essentially a multiplication operation that uses n as the divisor and each digit is the divisor, obtain the K-bit result of each digit corresponding to the n-base, and connect the result to a positive integer.

If an n-base value is converted to a positive integer (nKK), a K-bit division can be performed on the n-base value. The specific division rules are divided into the fractional part and the integer part: integer part: starts from the first digit on the left of the decimal point. Each digit is divided until less than K digits on the left are filled with zeros until K digits are satisfied. Then, convert each K-bit of the Division to the value of nK in hexadecimal notation. In essence, the conversion process is to use the bitwise weights of each K-bit in hexadecimal notation, then, based on the addition rule of nK, sum up to get the corresponding value of nK, and connect each result from left to right to a positive integer) the integer part of The hexadecimal value. Decimal part-start from the first digit on the right of the decimal point, divide each number of K digits until less than K digits on the right side are filled with zero until K digits are satisfied) or just ends with K digits. Then, convert each K-bit of the Division without the decimal point to the value of the nK hexadecimal system. In essence, the conversion process is to use each K-bit to carry out the bitwise right expansion in the n-hexadecimal system, then, based on the addition rule of nK, sum up to get the corresponding value of nK, and connect each result from left to right to a positive integer) decimal part of The hexadecimal value. Finally, the result is connected by the decimal point. It can be used to indicate this process:

650) this. length = 650; "src =" http://www.bkjia.com/uploads/allimg/131227/162FSN1-3.png "title =" multiplier conversion "width =" 500 "height =" 342 "border =" 0 "hspace =" 0 "vspace =" 0 "style = "width: 500px; height: 342px; "alt =" 234159691.png"/>

This method is only applicable to the conversion between a number with n-base and a positive integer with nKK.

Number System in computer-binary

The binary system is a digital system based on 0 and 1, and a digital system based on 2. The computer uses a binary number system. Because the binary number system only stores two digits 0 and 1, it can clearly represent the two states of electronic components, and the binary operation is simple, you can perform arithmetic operations or logical operations. In a computer, binary digital sequences and digits are used to represent different values. different numbers of the same digit can represent different values on different digits or on the same digit.

1. binary operations

In a computer, the binary number system supports two calculation methods: arithmetic operation and logical operation. Arithmetic Operations include addition, subtraction, multiplication, division, and logical operations include and or not. In arithmetic operations, the principle of "every two goes into one, borrow one when two" is followed, while logical operations simulate the operation of circuit components and are mainly used for the comparison of binary bit codes.

1. Arithmetic Operations
  • Addition rule: 0 + 0 = 0; 0 + 1 = 1 + 0 = 0; 1 + 1 = 10 carry)

  • Subtraction: 0-0 = 0; 10-1 = 1 borrow); 1-0 = 1; 1-1 = 0

  • Multiplication law: 0x0 = 0; 0x1 = 1x0 = 0; 1x1 = 0

  • Division rules: 0 limit 1 = 1; 1 limit 1 = 0

1. 2. logical operations
  • Logical non-NOT) operations are represented by the symbol "operator". Adding this symbol to the operand indicates that the operators of this number are NOT operated logically. Rule: 650) this. length = 650; "src =" http://www.bkjia.com/uploads/allimg/131227/162FV2Q-4.png "title =" non 0.png "width =" 28 "height =" 16 "border =" 0 "hspace =" 0 "vspace =" 0 "style =" width: 28px; height: 16px; "alt =" 2351311108png"/>; 650) this. length = 650; "src =" http://www.bkjia.com/uploads/allimg/131227/162FR0b-5.png "title =" non 1.png "width =" 28 "height =" 16 "border =" 0 "hspace =" 0 "vspace =" 0 "style =" width: 28px; height: 16px ; "Alt =" 235235243.png"/>. That is, if the value is not 0, it is 1, and if the value is not 1, it is 0.

  • Logical AND) operations are also called logical multiplication operations. They are represented by the symbol "×", "·", or "operator, use this symbol between two operands to perform logic and operation on the current two operands. The rule is as follows: 0 then 0 = 0; 0 then 1 = 0; 1 then 0 = 0; 1 then 1 = 1 a bit like multiplication ). That is, if one of the operands is 0, the result is 0. If the value is 1, the result is 1.

  • Logical OR) operations are also called logical addition operations. They are represented by the symbol "+" OR "operator". These symbols are used between two operands to represent the current two operands for logical OR operation. The rule is as follows: 0 then 0 = 0; 0 then 1 = 1; 1 then 0 = 1; 1 then 1 = 1 a bit like addition ). That is, if one of the operands is 1, the result is 1. If the value is 0, the result is 0.

  • Logical XOR or XOR) operations are represented by the "-equals" symbol. The "-" symbol is added before the first operand, the second operand is preceded by "XOR", which indicates logical exclusive or operation on the current two operands. The rule is as follows:-0 then 0 = 0;-0 then 1 = 1;-1 else 0 = 1;-1 else 1 = 0 a bit want to subtract ). That is, when the simultaneous result of the operand operation is 0, the result of the different operations is 1.

You may find that, corresponding to any binary number binary sequence), if the number at the right of the end is 1, the binary number represents an odd number and can be divisible by 2n-1; if the number at the right of the end is 0, the binary number indicates an even number, which can be divisible by 2n. If the number at the right of a binary number is shifted to 0) the result is 2 times of the number, that is, the result of multiplying the number by 2. The result of removing the right of an unsigned number by 0 on the left is 1/2 times of the Summary, that is, the result of dividing the number by 2. This feature is a manifestation of an algorithm, which is not explained in detail)

2. Binary Conversion 2. 1. Binary and decimal conversion

1. Convert binary to decimal

Expand the binary values by bit and sum them by decimal. The obtained result is a decimal value. The power of the integer part is positive, and the power of the fractional part is negative.

2. Convert decimal to binary

Divide the integer part of the decimal value by dividing by 2 and write down the remainder as the decimal digit. If the operator is not 0, divide it by 2, write down the remainder ......; Division is performed sequentially until the obtained quotient is 0. The remainder of each division is the integer part of the binary value. Multiply the decimal part of a decimal value by 2 and use the integer part of the result as the highest bit of the binary value. If the result still contains decimals, the obtained product does not reach the specified precision) multiply the fractional part by 2 and write down the integer part of the result ......; Perform multiplication in sequence until the obtained product does not contain the decimal part or reaches the specified precision. The integer part of each multiplication result is the decimal part of the binary value. At last, splice them with the decimal point.

2. Binary and other hexadecimal Conversions

Binary Conversion and other hexadecimal conversion can be used as a bridge. First, the binary value is converted to the corresponding decimal value, and then the decimal value is converted to the corresponding numeric system value. Alternatively, convert other hexadecimal values to the corresponding decimal values, and then convert the decimal values to the corresponding binary values.

Binary Conversion to 2KK is a positive integer. The bitwise division method can be used for hexadecimal conversion. The decimal point in the binary value is used as the origin, and the integer and fractional parts are divided by K digits, respectively, if the number is less than K, you can add 0. Then, convert each K bit into a 2 K hexadecimal value and perform the bitwise operation in binary format. Then, the summation is performed in 2K hexadecimal format ); finally, connect each digit in order to obtain the corresponding 2 k hexadecimal value. 2KK is a positive integer. The bitwise conversion method can be used for Binary Conversion, converts each digit of a 2 K base into a K-bit binary value using a base division of 2 ), then, the obtained values are connected in sequence as the corresponding binary values.

Appendix: conversion between binary, octal, decimal, and hexadecimal values
Binary Octal Decimal Hexadecimal
0000 00 0 0
0001 01 1 1
0010 02 2 2
0011 03 3 3
0100 04 4 4
0101 05 5 5
0110 06 6 6
0111 07 7 7
1000 10 8 8
1001 11 9 9
1010 12 10 A or
1011 13 11 B or B
1100 14 12 C or c
1101 15 13 D Or d
1110 16 14 E or e
1111 17 15 F or f
10000 20 16 10
Summary

A relative concept of a number system, and any number system has its unique use. Standard decimal is used in life. If you want to input IT, you must at least have a certain understanding of binary. If you want to program, you must have an understanding of octal and hexadecimal. The rule mentioned in this article is that the number of digits is different and the bit weights are different. You can use the bit weights summation method to convert a low-income value to a high-income value; base multiplication and division are used to convert a high-income value to a low-income value. In fact, this is a mathematical concept. The computer uses binary, which is relatively simple.

After that, the content of this article may be "shift-by-shift" for most people. I am a half-channel IT engineer, And I know little about high data and Computer Basics. You know ), here I want to express my summary knowledge in a popular way. So I hope you don't have to laugh ......

This article is from the "A cool" blog, please be sure to keep this source http://akudy.blog.51cto.com/2277883/1298406

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.