整数表示

部分内容来自

<<COMPUTER ORGANIZATION AND ARCHITECTURE-DESIGNING FOR PERFORMANCE>> by William Stallings

整数表示(Integer representation)


Sign-Magnitude Representation

符号-量级表示法

对于n位的数,最左边的一位代表符号位,接下来的n-1位代表量级( 绝对值大小)

The simplest form of representation that employs a sign bit is the sign-magnitude representation. In an n-bit word, the rightmost bits hold the magnitude of
the integer.


这种方法有几种缺陷。一种是加法与减法操作既要考虑符号又要考虑量级,另外0的表示有两种。

+0=00000000

-0=10000000

因此ALU里面几乎不会使用这种表示方法(符号-量级法)




二的补码表示 (Twos Complement Representation)


最左边一位也是符号位(0为正数,1为负数)

该表示中减法操作可以转化为加法操作

(Negation):

Take the Boolean complement of each bit of the corresponding positive number, then add 1 to the resulting bit pattern viewed
as an unsigned integer.


To subtract B from A, take the twos complement of B and addit to A.

比如A-B相当于A加上B的补码(B按位取反加1)

最高有效位(most significant bit)权重为-2^(n-1)


w=-a_{N-1} 2^{N-1} + \sum_{i=0}^{N-2} a_i 2^i


Negation(取反/取负):

1. Take the Boolean complement of each bit of the integer (including the sign bit).That is, set each 1 to 0 and each 0 to 1.
2. Treating the result as an unsigned binary integer, add 1.

总体思想便是按位取反加1


减法操作

比如

2-7

0010-0111

减法转化成加法

0111取(TWOs complement)按位取反加1结果为1001

0010+1001=1011=-5


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值