为什么更喜欢二进制补码而不是有符号数的符号和大小? [英] Why prefer two's complement over sign-and-magnitude for signed numbers?

查看:34
本文介绍了为什么更喜欢二进制补码而不是有符号数的符号和大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想知道为什么为了用二进制表示-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!

推荐答案

这样做是为了加法不需要任何特殊的逻辑来处理负数.查看维基百科上的文章.

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'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天全站免登陆