为什么对带正负号的数字宁愿用二进制补码,也不用正负号? [英] Why prefer two's complement over sign-and-magnitude for signed numbers?

查看:266
本文介绍了为什么对带正负号的数字宁愿用二进制补码,也不用正负号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是好奇是否有一个原因,为了用二进制表示-1,使用了二进制补码:翻转位并加1?

I'm just curious if there's a reason why in order to represent -1 in binary, two's complement is used: flipping the bits and adding 1?

-1由11111111(二进制补码)表示,而不是(对我来说更直观)10000001,后者是二进制1,第一位为负标志.

-1 is represented by 11111111 (two's complement) rather than (to me more intuitive) 10000001 which is binary 1 with first bit as negative flag.

免责声明:我的工作不依赖于二进制算法!

Disclaimer: I don't rely on binary arithmetic for my job!

推荐答案

这样做的目的是使加法处理负数不需要任何特殊的逻辑.查看有关Wikipedia的文章.

It's done so that addition doesn't need to have any special logic for dealing with negative numbers. Check out the article on Wikipedia.

假设您有两个数字2和-1.用直观"的数字表示方式,它们分别是00101001(我坚持使用4位来表示大小).用二进制补码的方式,它们是00101111.现在,假设我要添加它们.

Say you have two numbers, 2 and -1. In your "intuitive" way of representing numbers, they would be 0010 and 1001, respectively (I'm sticking to 4 bits for size). In the two's complement way, they are 0010 and 1111. Now, let's say I want to add them.

Two的补码加法非常简单.您通常会添加数字,并且末尾的任何进位都将被丢弃.因此,它们添加如下:

Two's complement addition is very simple. You add numbers normally and any carry bit at the end is discarded. So they're added as follows:

  0010
+ 1111
=10001
= 0001 (discard the carry)

0001为1,这是"2 +(-1)"的预期结果.

0001 is 1, which is the expected result of "2+(-1)".

但是在您的直观"方法中,添加更为复杂:

But in your "intuitive" method, adding is more complicated:

  0010
+ 1001
= 1011

哪个是-3,对不对?在这种情况下,简单加法不起作用.您需要注意的是,其中一个数字为负数,如果是这种情况,请使用其他算法.

Which is -3, right? Simple addition doesn't work in this case. You need to note that one of the numbers is negative and use a different algorithm if that's the case.

对于这种直观"的存储方法,减法是与加法不同的操作,需要对数字进行附加检查才能将其相加.由于您希望最基本的运算(加法,减法等)尽快完成,因此需要以一种允许使用最简单算法的方式来存储数字.

For this "intuitive" storage method, subtraction is a different operation than addition, requiring additional checks on the numbers before they can be added. Since you want the most basic operations (addition, subtraction, etc) to be as fast as possible, you need to store numbers in a way that lets you use the simplest algorithms possible.

此外,在直观"存储方法中,有两个零:

Additionally, in the "intuitive" storage method, there are two zeroes:

0000  "zero"
1000  "negative zero"

直观上讲,它们是相同的数字,但存储时有两个不同的值.每个应用程序都需要采取额外的步骤来确保非零值也不是负零.

Which are intuitively the same number but have two different values when stored. Every application will need to take extra steps to make sure that non-zero values are also not negative zero.

以这种方式存储整数还有另一个好处,那就是当您需要扩展寄存器的宽度来存储值时.使用二进制补码,将4位数字存储在8位寄存器中就成为问题了.重复其最高有效位:

There's another bonus with storing ints this way, and that's when you need to extend the width of the register the value is being stored in. With two's complement, storing a 4-bit number in an 8-bit register is a matter of repeating its most significant bit:

    0001 (one, in four bits)
00000001 (one, in eight bits)
    1110 (negative two, in four bits)
11111110 (negative two, in eight bits)

只需要查看较小单词的符号位,然后重复该操作,直到填充较大单词的宽度即可.

It's just a matter of looking at the sign bit of the smaller word and repeating it until it pads the width of the bigger word.

使用您的方法,您需要清除现有位,这是除填充之外的一项额外操作:

With your method you would need to clear the existing bit, which is an extra operation in addition to padding:

    0001 (one, in four bits)
00000001 (one, in eight bits)
    1010 (negative two, in four bits)
10000010 (negative two, in eight bits)

在两种情况下,您仍然都需要设置这些额外的4位,但是在直观"情况下,您还需要清除第5位.这是每个应用程序中存在的最基本,最常见的操作之一,这是额外的微小步骤.

You still need to set those extra 4 bits in both cases, but in the "intuitive" case you need to clear the 5th bit as well. It's one tiny extra step in one of the most fundamental and common operations present in every application.

这篇关于为什么对带正负号的数字宁愿用二进制补码,也不用正负号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆