数据之间的运算
数据之间的运算
这篇博文将跟随我的脚步去了解计算机数据之间的运算,依据《大学计算机基础教程(第3版)》内容进行学习
数据间运算
首先学习计算机运算我们得了解他的构成,计算机数据运算分为算术运算和逻辑运算这两大类
那么数据的运算是在那运行的呢🤔:数据的运算当然是放在计算机的**中央处理器(CPU)里面执行的啦,而在CPU中我们有专门的部件来完成算术和逻辑运算,这个部件叫做算术逻辑单元 (ALU: Arithmetic Logic Unit)**,我们重点展开讲一下ALU这个部件有关于它的基本知识。🙉🙉🙉
ALU
它是数字电子电路的合并形式,它对整数二进制数执行算术和按位运算。 这与**浮点运算单元(FPU: Float Point Unit)**相比,后者在浮点数上运行并起作用。 ALU是各种类型的计算电路的基本组成部分
其中包括:
The central processing unit (CPU) of computers
计算机的中央处理器(CPU)
FPUs
FPU
Graphics processing units (GPUs)
图形处理单元(GPU)
单个CPU,FPU或GPU可能包含各种ALU
ALU功能(ALU Functions)
ALU通常支持一些基本的算术和按位逻辑函数。 基本的通用ALU通常包含以下操作:
Add: A and B are summed and the sum comes out at Y and carry-out.
加 :将A和B相加,并且总和在Y处并结转。
Add with carry: A, B and carry-in are summed and the sum comes out at Y and carry-out.
带进位加法:将A,B和进位相加,总和在Y处进行进位。
Subtract: B is subtracted from A (or vice versa) and the difference comes out at Y and carry-out. For this function, carry-out is efficiently a “borrow” indicator. This operation may also be used to contrast the magnitudes of A and B; in such conditions, the Y output may be unnoticed by the processor, which is only involved in the status bits (specifically zero and negative) that outcome from the operation.
减法 :从A中减去 B(反之亦然),并且差在Y处出现并结转。 对于此功能,结转实际上是“借入”指示器。 此操作也可以用来对比A和B的大小; 在这种情况下,处理器可能不会注意到Y输出,该处理器仅涉及操作产生的状态位(特别是零和负)。
Subtract with borrow: B is subtracted from A (or vice versa) with borrow (carry-in) and the difference comes out at Y and carry-out (borrow out).
用借位减去 :用借位(进位)从A中减去B(反之亦然),差值在Y处出现,而结转(借入)。
Two’s complement (negate): A (or B) is subtracted from zero and the difference comes out at Y.
二进制补码(取反):从零中减去A(或B),并且差在Y处出现。
Increment: A (or B) is increased by one and the outcome value comes out at Y.
增量 :A(或B)增加一,结果值显示为Y。
Decrement: A (or B) is decreased by one and the outcome value comes out at Y.
减量 :A(或B)减一,结果值为Y。
Pass through: all bits of A (or B) appear unchanged at Y. This operation is normally used to find out the state of correspondence of the operand or whether it is zero or negative or to put up the operand into a processor register.
直通 :A(或B)的所有位在Y处均保持不变。此操作通常用于查找操作数的对应状态,或者它是零还是负数,或将操作数放入处理器寄存器中。
按位逻辑运算 **(**Bitwise logical operations)
AND: the bitwise AND of A and B comes out at Y.
AND :A和B的按位与在Y处出现。
OR: the bitwise OR of A and B come out at Y.
OR :A和B的按位OR在Y处出现。
Exclusive-OR: the bitwise XOR of A and B come out at Y.
异或 :A和B的按位XOR在Y处出现。
Ones’ complement: all bits of A (or B) are inverted and come out at Y.
一个补码 :A(或B)的所有位都反转并在Y处出现。
移位操作 (Bit shift operations)
Arithmetic shift: the operand is taken care of as a two’s complement integer, signifying that the highly noteworthy bit is a “sign” bit and is preserved.
算术移位 :操作数被视为二进制补码整数,表示高度值得注意的位是“符号”位并被保留。
Logical shift: a logic zero is shifted into the operand. This is used to shift unsigned integers.
逻辑移位 :将逻辑零移位到操作数中。 这用于移位无符号整数。
Rotate: the operand is taken care of as a circular buffer of bits so it’s smallest and highly noteworthy bits are successfully adjacent.
旋转 :操作数被当作位的循环缓冲区处理,因此它最小且值得注意的位成功相邻。
Rotate through carry: the carry bit and operand are cooperatively taken care of as a circular buffer of bits.
循环进位:将进位和操作数作为位的循环缓冲区共同处理。
算术运算
算术运算包括加,减,乘,除,求补和算术移位等。
首先我们从补码的加,减运算入手
补码加减运算
补码运算规则简单,计算机中大量采用。
1.补码加法运算
两个相加的数无论正负,只要表示成对应的补码形式,则可直接按二进制规则相加,且符号位作为数的一部分直接参与运算,所得结果就是和的补码形式,用关系式表示
$$
[x]补 + [y]补 = [x+y]补
$$
结论:用补码表示的数据进行加法算时可以不考虑符号位,直接运算,即与不带符号的数据的运算完全相同。
2.补码减法运算
例如:
56-34
00111000
– 00100010
————————
00010110 = 22
在计算机实际处理的时候并没有直接坐减法运算而是将减法运算转换成加法运算来处理,这就是为什么采用补码来表示带符号数的一个重要原因,在微机系统中之所以采用补码来表示带符号数,就是因为当用补码来表示带符号数的时候可以将减法运算转换成加法运算,这样在涉及到CPU内部运算器时就可以只考虑加法器,那这样呢硬件线路简单,运行速度快,将减法转加法如下
$$
[x-y]补 = [x+(-y)]补 = [x]补 + [-y]补
$$
[-y]补称为[y]补的机器负数。
不管 y的真值是正数还是负数,已知 [y]补求机器负数[-y]补的方法是:将[y]补连同符号位一起变反,末尾加1(定点小数中这个1就是2的-n次方)。
3.补码的运算规则
- 参与运算的操作数用补码表示
- 符号位参加运算
- 若指令操作码为加,则两数直接相加;若操作码为减,则将减数连同符号位一起变反加1后再与被减数相加
- 运算结果用补码表示
例题
1 | 例题:[x]补=00110110,[y]补=11001101;求[x-y]补? |
算术移位
算术移位又分为算术左移和算术右移
算术左移:所有位向左移一位,最高位丢弃,最低位补0。
算术左移n位相当于给该数乘以2n。
算术右移:所有位向右移一位,最低位丢弃,最高位补符号位。
算术右移n位相当于给该数整除以2n。
运算溢出及判断
算术运算时,若运算结果超出所能表示的数据范围,则称为溢出。
用双符号位发判断是否溢出,结果双符号位:
00:无溢出,结果为正数
01:上溢(结果大于最大正数)
10:下溢(结果小于最小负数)
11:无溢出,结果为负数
发生溢出时结果错误,需要进行异常处理
加法溢出
运算结果超出了目标所能容纳的范围,称发生了溢出。
例如:
8位所能表示的补码数据的范围是:-128~+127
只有两个同号数相加时,才有可能发生溢出。即:两个同号数相加,结果的符号位和运算数的符号位不同,则发生了溢出
减法溢出
运算结果超出了目标所能容纳的范围,称发生了溢出。
只有两个异号数相减时,才有可能发生溢出。即:两个异号数相减,结果的符号位和被减数的符号位不同,则发生了溢出。
对-128取补,会发生溢出。