Principles of Computer Composition - Chapter 2 Data Representation and Operation

1. Representation of data

This part will explain numeric data and character data
insert image description here

1. Representation of Numerical Data (Key Difficulties)

Computers use binary to represent data, but if binary is used to represent data, it will have a lot of digits, so it needs octal, hexadecimal and other binary digits to represent, which can simplify the representation.

1.1 Representation of Numerical Data – Carry System

Numerical representation of the three aspects of
insert image description here
the carry system
The carry system is also the carry counting system
For any kind of system - X system, it means that the number operation on each digit is carried by one digit every X. Decimal means every decimal one, hexadecimal means every hexadecimal one, binary means every two and one, and so on, X base means every X carry.
We can use a combination of several bits to represent a number to form a sequence of symbols, such as:
x_n−1x_n−2⋯x_0 A text with footnotes.
for example, 65 in decimal, we actually calculate it like this:
the base number represented by decimal is 10
, and the size it represents is 6×10^1 +5×10^0 =65The
computer uses the carry system

Carry system symbol cardinality Numbers that can be used to represent
binary B 2 0,1
Octal O 8 0 to 7
decimal D 10 0 to 9
hex H 16 0 to 9, A to F, A for 10, F for 15

insert image description here

insert image description here
BCD code is simply to separate the numbers in each position, not as a whole, for example, 10, if it is represented by BCD code, it will represent 1 and 0 respectively, just like we use the keyboard to input 10 on the computer , first knock out 1, then knock out 0, and then combine them to make 10

The first principle of conversion between various base systems - the integer part and the fractional part are converted separately


insert image description here
insert image description here

Example analysis
insert image description here
insert image description here
insert image description here

Others are similar, so I won't say more here, you can try it yourself.

1.2 Numerical data representation - code system

insert image description here

Digitization of symbols: use 0 to represent + and 1 to represent -, because computers use binary to store data,
there is no decimal point in the computer, the numbers we see with decimal points are just written by us for convenience. The decimal point
insert image description here
code shift is usually used to represent the order code of floating point numbers, the order code is a k-bit integer,
insert image description here
insert image description here

insert image description here
Question: Assuming that the compiler stipulates that the length of int and short types occupy 32 bits and 16 bits, the number of machines that get y after executing the following C language statement is ( B )
unsigned short x = 65530; unsigned int y = x;
A. 0000 7FFA B . 0000 FFFA C. FFFF 7FFA D. FFFF FFFA
insert image description here
Title: 8-bit complement consisting of 3 "1" and 5 "0", the smallest integer that can be represented is (B)
A. -126 B. -125 C .-32 D.-3

insert image description here
When choosing how to represent numbers on a computer, the following factors should be considered:
① the type of numbers to be represented (decimal, integer, real and complex)
② the range of possible numbers
③ numerical accuracy
④ data storage and processing Required hardware cost

insert image description here

1.3 Representation of numerical data - fixed-point numbers

The fixed-point format constrains the position of the decimal point of all data in the machine to be fixed. This position is fixed when the computer circuit is designed. Because it has been agreed to be at a fixed position, the decimal point is no longer marked. To indicate that the position of
the decimal point is fixed wherever Yes, but usually represent data as pure integers or pure decimals
insert image description here

insert image description here

insert image description here
insert image description here

1.4 Representation of Numerical Data - Floating Point Numbers

Floating-point numbers are numbers whose decimal point can be moved as needed.
Representation format
insert image description here
The order code of a machine is either shift code or complement code, and it is impossible to use both. insert image description here
Note: The absolute value of the numerical part of the floating-point number is less than 1
insert image description here
insert image description here
insert image description here
Note: Explain the smallest number represented by the above one: no matter how many bits of complement, the minimum number it can represent is -1, one bit is the sign bit, and the complement is Complement code +1, the maximum absolute value of the complement code is 11111111...1, and adding 1 becomes 10000000000000...0, but it should be noted that the numerical part of the floating point number is a decimal, so the maximum absolute value of the complement code is 1

We generally use fixed-point integers to represent the exponent, and fixed-point decimals to represent the mantissa.

1.5 IEEE 754 Standard for Floating Point Representation

In the early days, the floating-point numbers of various computer systems used different machine codes to represent the order and mantissa, which made the exchange and comparison of data very troublesome, because their representations were different, so the current computers use the unified IEEE 754 standard format. to represent floating point numbers.
base 2 is a fixed constant, so there is no need to explicitly represent it
insert image description here

ps: The code shift is an unsigned number, so the order code and the order symbol together
in the IEEE 754 standard, the order code is
the definition of the code shift represented by the code shift: code shift = true value + offset value, the offset value is 2 the k power.
In the IEEE 754 standard, the offset value is +127
insert image description here
. It was said before that for a floating point number, if its mantissa is represented by the original code, we want its most significant bit to be 1, and we can hide the highest bit 1
insert image description here
shift code = true value + offset value
order code true value = code shift - offset value
insert image description here
insert image description here
insert image description here

2. Character data representation

insert image description here

Second, the operation method of data

1. Basic Concepts

insert image description here

2. Fixed-point arithmetic

Fixed-point addition and subtraction in computers are performed using complement code
insert image description here

3. Overflow judgment

The value of the data exceeds the range represented by the number of machines

For addition: overflow occurs only when two numbers with the same sign are added.
For subtraction: overflow occurs only when two numbers with the same sign are subtracted.
insert image description here
insert image description here
insert image description here

4. Rounding processing

insert image description here
注意:原码是0舍1入,补码是1舍0入

5.定点乘法运算

在做乘法运算的时候,我们从小学开始,就是一位一位的相乘,然后把每一步的乘积相加就是最后的结果,但是用计算机来实现的时候,是会出现问题的

insert image description here

1.原码一位乘法

符号单独处理,两个操作数绝对值相乘,每次把一位乘数对应的部分积和原来部分积的累加和进行相加,并右移。
insert image description here

2.补码一位乘法

insert image description here

3.原码两位乘法

insert image description here

4.定点除法运算

insert image description here

6.浮点四则运算

insert image description here

三、数据的校验

1.奇偶校验码

insert image description here
校验是为了检测我们读取的信息是否正确,当信息符号某种规律的时候,我们就可以验证信息是正确的。
码距是用来判断一个码制的冗余度,评估其差错和纠错能力。

冗余校验
在信息写入时,增加部分代码(校验位/冗余位),将有效信息与校验位一起按约定的校验规律进行编码(校验码)存储;读出时,对读到的校验码进行校验(译码),看是否仍符合约定的校验规律
校验规律选择得当,不仅能够查错,而且可以纠错
比如说,根据上面的图片,第一种方案,码距为1,当信息某些位发生错误,由于码距为1,所以获得的信息也是正确的,我们就无法判断之前的信息是什么。方案2,码距为2,如果信息只有一位发生改变,那么得到的信息就不符号我们的校验规则,我们就能判断这个信息是错误的,所以码距可以用来评估纠错能力。
奇校验——使完整的校验码(有效位+校验位)中“1”的个数为奇数个
偶校验——使完整的校验码(有效位+校验位)中“1”的个数为偶数个
奇偶校验的缺陷:如果数据中有偶数个代码位错误,那么我们是无法检测出错误的,奇偶校验只能检测出奇数位错误,没有纠错能力。校验位只能携带2种状态信息,只能反馈是对或错两种状态,而不能反馈是哪一位错误
校验位的形成在数电中有学过,就是通过异或运算, 把所有有效位进行异或运算,得到的数据就是校验位。(不清楚的可以去看看数电)

2.海明校验码

海明校验方法
采用多重奇偶校验的思想
将有效位按照一定规律组织成若干组,分组进行奇偶校验,每一个分组对应一个校验位
各组校验位组成一个指示错误的字,用于检测是否出错并能纠正一位错误。
这种方法除了能够反馈对错这样的状态信息,甚至还能指明是哪一个位置发生错误。
insert image description here
insert image description here
如果异或之后,得到的数据是010,说明出错的位置是2,也就是H2,如果得到的数据是110,说明出错的位置是6,也就是H6这个位置

insert image description here

insert image description here

3.循环冗余校验码(CRC)

Idea:
Let the check code be divisible by a certain agreed code. If it can be divided, the check code is correct, otherwise, the remainder indicates the error position.
insert image description here
insert image description here
insert image description here
insert image description here
Note: Modulo 2 subtraction is also the exclusive OR operation
.

Guess you like

Origin blog.csdn.net/qq_52797170/article/details/123750173