Number system and its conversion

1.1 The number system commonly used in computers

In the computer, the common base are binary, octal, decimal, hexadecimal.

1.1.1 decimal

The base is 10, every decimal, the number of digits from 0 to 9, the base symbol is D, such as “12.3” according to the bit weight expansion :(12.3) 10 = 1×10^1+2 ×10^0+3×10^-1 = 12.3d.

Using the position counting method, the expansion law of decimal bitwise weight is as follows:

  • The integer part

The place weight corresponding to a certain digit is the current place value multiplied by the power based on the base of the base, and the exponent in the power is the number of values between the current digit and the decimal point (positive and not including the leading digit), that is, the place weight = place value × the number of digits between the base and the decimal point. For example, the integer partial place weight in (634.57) 10 is: 6 times 10^2, 3 times 10^1, 4 times 10^0.

  • The decimal part

The corresponding bit weight of a certain digit is the current bit value multiplied by the power based on the base, and the exponent in the power is the number of values between the current digit and the decimal point (negative and including the current digit), that is, the bit weight = place value × base – the number of digits away from the decimal point, such as (634.57) 10 small part of the bit weight is in turn: 5×10^-1, 7×10^-2.

  • merge

Add the place weights of the integer part and the decimal part to get the place weight expansion of the original number, such as (634.57) 10 place weight expansion is 6×10^2+3×10^1+4×10^0+5 ×10^-1+7×10^-2, (634.57) 10 = 6 x 10 + 3 x 10 ^ 1 ^ 2 + 4 x 10 ^ 0 + 5 * 10 + 7 x 10 ^ ^ – 1-2 = 634.57 D.

Positional counting works not only for decimal numbers, but also for binary, octal, and hexadecimal numbers by changing the base of the power to the corresponding base of the number system.

1.1.2 binary

The base number is 2, and the digit digit symbol is only 0 and 1, every two into one, the base symbol is B, such as “11.1” according to the bit weight expansion :(11.1) 2 = 1×2^1+1×2^0+1×2^-1 = 11.1b.

1.1.3 octal

The base number is 8, every eight into one, the number is represented from 0 to 7, and the symbol of the base is O, such as “11.1” according to the bit weight expansion :(11.1) 8 = 1×8^1+1×8^0+1×8^-1 = 11.1o

1.1.4 Hexadecimal

The base is 16, every hexadecimal, the number of bits from 0 to 9,10 is represented by A, and so on 11 is B, 12 is C, 13 is D, 14 is E, 15 is F, the base symbol is H, such as “4FD” according to the bit weight expansion: (4FD) 16=4×16^2+15×16^1+13×16^0 = 4FDH

1.2 Conversion between different number systems

1.2.1 Converting two, eight, and hexadecimal numbers to decimal numbers

Other base conversion to decimal is usedPosition counting method. As shown below:

1.2.2 Converting from Decimal to Other Bases

Converting a decimal number to another base, divide the decimal number and its quotient into the base of the target base (stop when the divisor is 0) continuously, and finally write out the remainder from bottom to top to form the other base corresponding to the decimal number, as shown in the figure. 127D=1111111B, 127D=177O, 127D=7FH.

1.2.3 Converting octal to binary

Divide one digit into three digits, e.g. (7.2) 8= 111.010b

1.2.4 Converting hexadecimal to Binary

Split one digit into four digits, for example, (7.2) 16= 111.0010h

1.2.5 Binary to octal and hexadecimal

Octal is converted to three – digit one

Convert hexadecimal to one – digit

2. Representation of data

A variety of numerical values in the form of computer called machine number, its characteristics is the use of binary counting system, the number of symbols with 0,1, the decimal point is implied and does not occupy the position. The actual value corresponding to the number of machines is called the truth value of the number.

The number of machines is divided into unsigned and signed numbers. An unsigned number represents a positive number, with no sign bits in the machine number. For unsigned numbers, it is a pure integer if the position of the decimal point is agreed to be after the lowest machine number; If the position of the decimal point is agreed before the highest number of machines, it is a pure decimal. For signed numbers, the highest bit of the machine number is the sign bit representing the positive and negative bits, and the remaining bits represent the number. If the position of the decimal point is agreed after the lowest number of machines, it is a pure integer; If the position of the decimal point is agreed to be before the highest number of machine digits (after the sign digits), it is a pure decimal.

In order to facilitate operation, the signed machine number can adopt different coding methods such as source code, inverse code and complement code, and these coding methods of machine number are called code system.

2.1 Machine code representation of numbers

  • Here, the machine word length is assumed to be N bits, that is, the signed number is represented by n binary bits, the highest bit is the sign bit (0 represents a positive number, 1 represents a negative number), and the remaining n-1 bits are the absolute value of the number. The following instructions assume that the machine word is 8 bits long.

  • Machine word length: Machine word length is the word length of the fixed point number operation, usually the width of the CPU’s internal data path. That is, the longer the word length, the greater the range of representation of the number, the higher the accuracy. The machine’s word length also affects the speed of the machine. If the CPU word length is short, but also to operate more data, then need to go through two or more operations to complete, so is bound to affect the speed of the machine

2.1.1 the original code
  • Characteristic: A method of binary fixed-point representation in which the sign bit 0 represents a positive number, 1 represents a negative number, and the remaining bits represent the magnitude of the number

The source code is the sign bit plus the absolute value of the truth value, that is, the first bit represents the sign and the remaining bits represent the value. For example, if it is 8-bit binary:

[+1] = 0000 0001

[-1] Original = 1000 0001

The first is the sign bit. Since the first bit is a sign bit, the range of 8-bit binary numbers is:

[1111 1111, 0111 1111]

The [- 127, 127]

2.1.2 radix-minus-one complement
  • Features: the inverse of a positive number is itself, and the inverse of a negative number is the inverse of its source code (except the sign bit).
[+1] = [00000001] Original = [00000001] Inverse [-1] = [10000001] Original = [11111110] InverseCopy the code
2.1.3 complement
  • Features: The complement of a positive number is the same as the original code, and the complement of a negative number is its inverse tail plus 1
[+1] = [00000001] Original = [00000001] Inverse = [00000001] Complement [-1] = [10000001] Original = [11111110] Inverse = [11111111] ComplementCopy the code
2.1.4 frameshift
  • Features: Shift representation is defined by adding an offset to the number x, usually used to represent the order code of a floating point number. If the machine word length is N and the specified offset is 2^n-1, then the shift definition is as follows:
(3) The value of [X] shift is 1+X (-1≤X < 2N-1) and the value of [X] shift is 1+X (-1≤X < 1).Copy the code

Such as:

[+1] Move =28-1+1=129=1 0000001 [-1] Move =28-1-1=127=0 1111111 [+127] Move =28-1+127=255=1 1111111 [-127] Move =28-1-127=1=0 0000001 [45] + 45 = 173 = = 28-1 + 1 move [45] - 0101101 = 28-1-45 = 83 = 0 1010011 move] [+ 0.5 = 1 + 0.5 = 1.5 = 1, 1000000 [0.5] move = 1-0.5 = 0.5 = 0 1000000 [+0] Shift =[-0] Shift =1 0000000Copy the code
2.1.5 Sources and functions of source code, inverse code and complement code
  • Source code: for computers, addition, subtraction, multiplication and division is the most basic operation, to design as simple as possible. Computer recognition of “sign bits” obviously complicates the basic circuit design of a computer! So scientists came up with a sign bit to participate in the operation. We know that according to the algorithm subtracting a positive number is equal to adding a negative number, i.e., 1-1=1+(-1)=0, so the machine can only add without subtracting, making the design of computer operations much easier. In the operation, the original code for addition operation is no problem, but in the subtraction operation will be a problem, such as: 1D-1D=1D+(-1)D=[0 0000001] original +[1 0000001] original =[1 0000010] original = -2d, this result is obviously wrong! If the original code is used, and the sign bit is also involved in the calculation, obviously for the subtraction, the result is not correct. This is why computers don’t use source code internally to represent a number. In order to solve the problem of subtracting the original code, inverse code appeared.

  • Inverse code: Use inverse code to calculate 1-1 process: 1-1 =[0000 0001] original +[1000 0001] Original =[0000 0001] Inverse +[1111 1110] Inverse =[1111 1111] Inverse =[1000 0000] original = -0. Even though plus 0 is the same thing as minus 0, it doesn’t make any sense to have a zero with a sign and there’s going to be two plus 0’s and minus 0’s for 0’s. So people came up with the complement.

  • Complement: 1D-1D=1D+(-1)D=[0000 0001] original +[1000 0001] Original =[0000 0001] complement +[1111 1111] complement =[0000 0000] inverse =[0000 0000] original =0D, so 0 can be expressed as 00000000, The previous problem with -0 is gone, and there is only one unique representation of 0 in the complement. Given a machine word length of 8 bits, using the complement can be represented by [1 0000000] complement -128d: (-1)D+(-127)D=[1000 0001] original +[1 111 1111] Original =[1 111 1111] supplement +[1 000 0001] supplement =[1000 0000] supplement, the final calculation result should be “[1 1000 0000] supplement”, But because the machine word is 8 bits long, the leftmost “1” is lost, so only [1 0000000] is left. The result of -1-127 should be -128. In the result of the complement operation, [1 0000000] complement is -128. Note, however, that since -128 is actually represented by the complement of the previous -0, there is no source and inverse representation of -128 (the complement of -128 means that the source of [1 0000000] is the source of [0 0000000], which is incorrect). By using complement code, not only the problem of zero symbol and two codes of zero is fixed, but also one more lowest number can be represented. This is why 8-bit binaries are represented in a range of [-127, +127] using source or inverse, and in a range of [-128,127] using complement. [-2^31, 2^31-1] : [-2^31, 2^31] : [-2^31, 2^31] : [-2^31, 2^31] : [-2^31, 2^31] Given n bits of machine word length, the smallest number represented by a complement is -2^n-1.

In a word, inverse code is used to solve the problem of negative number addition, which transforms the subtraction operation into addition operation, thus simplifying the operation rules; Complement code solves the problem of positive and negative zero of negative number addition operation and makes up for the deficiency of inverse code. Both inverse and complement are used to solve the problem of negative numbers, and have nothing to do with positive numbers. Therefore, whether it is a positive integer or a positive decimal, the source code, inverse code, and complement are all the same. The corresponding shift code is obtained by inverting the complement symbol bit.

2.2 Fixed-point and floating point numbers

Fixed point format is the convention that all data in the machine is fixed decimal position. Fixed-point data is usually expressed as a pure decimal or a pure integer. In order to express a number as a pure decimal, the decimal point is usually fixed before the highest digit of the numeric part; To represent a number as a pure integer, the decimal point is fixed at the end of the numeric part, as shown below:

If the decimal point in a number is fixed, it is a fixed point. A number is a floating point number if the position of the decimal point is floating. In general, the range of values that can be represented by fixed-point format is limited, but the processing hardware required is relatively simple. Floating point format can represent a large range of values, but requires more complex processing hardware.

Floating point numbers represent:

N= mantissa * base ^ exponent

The number of bits of the order code determines the range of the number, the more bits, the greater the range

The mantissa number determines the effective precision of the number. The more digits, the higher the precision

The range of values that floating point numbers can represent is mainly determined by the order code, and the precision of the representation is determined by the mantissa length.