移码
移码 | |
---|---|
术语名称 | 移码 |
英语名称 | offset binary |
别名 | biases representation, excess code |
移码(offset binary)是一种计算机内的有符号整数表示方法(机器数)。在移码中全部的数都被加上一个基准偏移值,这个偏移值保证要表示的负数都在相加后变成非负数,然后按照无符号数的方式表示为二进制。
定义
移码(offset binary)指用 [math]\displaystyle{ n }[/math] 位二进制位构成的机器数表示有符号数的方法,其中将所有要表示的数增加一定的偏移量。在 [math]\displaystyle{ n }[/math] 位时这一偏移量默认取 [math]\displaystyle{ 2^{n-1} }[/math] ,也就是使得 [math]\displaystyle{ -2^{n-1} }[/math] 到 [math]\displaystyle{ 2^{n-1}-1 }[/math] 的范围偏移到可以直接表示的范围。
具体对应:
- [math]\displaystyle{ 00\cdots0 }[/math] 到 [math]\displaystyle{ 11\cdots1 }[/math] 对应 [math]\displaystyle{ -2^{n-1} }[/math] 到 [math]\displaystyle{ +2^{n-1}-1 }[/math] 。
注意:与普通的方法不同,补码因为有偏移值,不会把全 0 的数字对应到 0 。
移码运算
将机器数表示本身视为二进制数,则可以认为从数本身到机器数的映射关系为:
[math]\displaystyle{ x \mapsto x+2^{n-1} }[/math]
可以看到移码就是补码减去 [math]\displaystyle{ 2^{n-1} }[/math] 的结果,这一点也可以作为相关运算的基准。
移码相反数
[math]\displaystyle{ x'=x+2^{n-1} \Rightarrow (-x)'=-x+2^{n-1} \Rightarrow (-x)'=2^n - x' }[/math]
移码加减法
[math]\displaystyle{ a'=a+2^{n-1}, b'=b+2^{n-1} \Rightarrow (a+b)' = a+b+2^{n-1} = a'+b'-2^{n-1} }[/math]
数的内部表示 | |
---|---|
十进制数的二进制编码 | BCD 、 Gray 码 、 奇偶校验码 、 字符表示 |
有符号整数的机器数 | 原码、反码、补码、移码 |
有符号小数的机器数 | 定点数、浮点数(IEEE 754) |