[Computer composition principle] Representation and calculation of fixed-point numbers

Fixed-point representation

  1. Unsigned and signed numbers
    无符号数 and 有符号数are 机器数
    1) unsigned number
    refers to the whole 机器字长of the bits are all binary bit values, without a sign, corresponding to the number 绝对值.
    If the machine word length is 8 bits, the number representation range is 0~2 8 -1 , that is, 0~255 .
    2) Signed numbers
    in the machine, the "positive" and "negative" signs of numbers are not recognized. Signed numbers use "0" for "positive" signs, and "1" for "negative" signs. It is also digitized, and it is usually agreed that the highest bit of a binary number is the sign bit, that is, the sign bit is placed in front of the effective number to form a signed number.
    有符号数The 机器表示have 原码, 补码, 反码and 移码.
    In order to be able to distinguish correctly 真值and variously 机器数, it is agreed to use X to represent the truth value

[X] Original means 原码
[X] complement means 补码
[X] reverse means 反码
[X] shift means移码

  1. Fixed-point representation of machine numbers
    According to whether the position of the decimal point is fixed or not, there are two data formats in the computer:
    fixed-point representation and floating-point representation, which
    定点 means that the position of the decimal point in the machine number is fixed, and the decimal point is no longer used “.”. Arrange its location. In theory, the position of the decimal point can be fixed anywhere, but two simple conventions are usually used in computers:
    fix the position of the decimal point before the highest digit of the data, or fix it after the lowest digit. Usually called the former 定点小数and the latter 定点整数.

1) Fixed-point decimals
Fixed-point decimals are pure decimals. It is agreed that the position of the decimal point is after the sign bit and before the highest digit of the valid value part. If the data is in the form of X is X = X 0 . X . 1 ...... X n- , (wherein X 0 is the sign bit, X . 1 --X n- effective part of a number, also called the mantissa, X . 1 is the most significant bit), The representation in the computer is shown in the figure (set the machine word length n+1 bits).

When X 0 = 0 , X . 1 --X n- are both 1, x for the maximum positive number that can be represented, is equal to the true value . 1 - 2 -n .

When X 0 =. 1 , X . 1 --X n- are both 1, X its minimum negative (original code) can be represented, the true value is equal to 2 -n -. 1 .
Insert picture description here
2) Fixed-point integer

Fixed-point integers are pure integers, and it is agreed that the position of the decimal point is after the lowest digit of the valid value part. If the data is in the form of X is X = X 0 X . 1 ...... X n-
(wherein X 0 is the sign bit, X . 1 --X n- is the mantissa, X n- least significant bit), then in the computer in the form of FIG. Shown:

When X 0 = 0 , X . 1 --X n- are both 1, x for the maximum positive number that can be represented by the true value is equal to 2 n- -1 .

When X 0 =. 1 , X . 1 --X n- are both 1, X its minimum negative (original code) can be represented, is equal to the true value . 1 - 2 n- .
Insert picture description here

  1. Original code, complement, inverse code, shift code
    1) Original code representation The
    original code is a relatively simple and intuitive machine number representation method, the highest bit of the machine number is used to represent the symbol of the number, and the rest of the bits represent the number Absolute value. The original code is defined as follows:
  • The definition of the original code of pure decimal
    Insert picture description here
    If the word length is n+1, the representation range of the original code decimal is: 2 -n -1 ≤ x ≤ 1-2 -n
  • Definition of pure integer original code
    Insert picture description here
    If the word length is n+1, the representation range of the original code decimal is: 1-2 n ≤ x ≤ 2 n -1

2) Complementary code representation
Addition and subtraction adopt a unified addition operation

  • The definition of pure decimal number's complement
    Insert picture description here
    If the word length is n+1, the representation range of the original code decimal is: -1 ≤ x ≤ 1-2 n
  • Definition of Pure Integer's Complement Code
    Insert picture description here
    If the word length is n+1, the representation range of the original code decimal is: -2 n ≤ x ≤ 2 n -1

0 is 补码、移码unique, 原码、反码not unique

Insert picture description here

Fixed-point operations

  1. Fixed-point shift operation
    移位运算 according to the operation target divided into 算术移位and 逻辑移位.
    The shift of a signed number is called an arithmetic shift, and the operation object of a logical shift is a logic code, which can be regarded as an unsigned number.
    1) Arithmetic shift
    The object of arithmetic shift is a signed number, and the sign bit remains unchanged during the shift.
    For positive numbers, the vacancies appearing after shifting are all filled with 0.
    For negative numbers, because the original code, complement code, and inverse code have different representations, when the machine number is shifted, the rules for filling the space are also different.
    Note: Regardless of whether it is a positive number or a negative number, the sign bit remains the same after shifting, and after shifting, it is equivalent to zeroing the true value. According to the characteristics of complement and inverse, the padding code is different when it is negative.
    For the original code, if the left shift does not cause overflow, it is equivalent to multiplying by 2 (similar to the decimal shifting by one bit to the left is equivalent to multiplying by 10), and shifting to the right by one bit. , Which is equivalent to dividing by 2.

The original code, complement and inverse code of a positive number are all the same, so the spaces appearing after shifting are all filled with 0.
For negative numbers, since the original code, complement code, and inverse code have different representations, when the machine number is shifted, the rules for filling the space are also different.

①The original code value part of the negative number is the same as the true value, so as long as the sign bit remains unchanged when shifting, the vacant bits are all added to 0.
②The inverted bit of the negative number is opposite to the original code of the negative number except for the sign bit, so shift The code added after the bit should be opposite to the original code, that is, add all 1s.

③Analyzing the process of getting the complement from the original code, it is found that when the first "1" is found from the low bit to the high bit, the bits to the left of the "1" are the same as the corresponding inverse code, and here "1" The bits on the right (including this "1") are the same as the corresponding original code. Therefore, when the complement of a negative number is shifted to the left, because the space appears in the low position, the complement code is the same as the original code, that is, 0 is added; when the space is shifted to the right, the complement code should be the same as the inverse code, that is, add 1 .

(2) Logic shift

Logical shift treats the operand as an unsigned number, and the shift rules:

逻辑左移时,高位移丢,低位添0;
逻辑右移时,低位移丢,高位添0。

Note: Regardless of whether the logical shift is left or right, add 0.

Guess you like

Origin blog.csdn.net/qq_43511405/article/details/107871535