为什么用补重新present负数? [英] Why is two's complement used to represent negative numbers?

查看:336
本文介绍了为什么用补重新present负数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

林,如果有为什么以重新present -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(补)psented $ P $,而不是(对我来说更直观)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。在重新presenting号码你的直觉的方式,他们将 0010 1001 分别为(我'M粘到4位为大小)。在补的方式,他们是 0010 1111 。现在,让我们说我要添加。

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)" to be.

但在你的直觉的方法,将是比较复杂的:

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.

有与存储整数这样另一个好处,当你需要扩展的寄存器中的值存储在宽度是随着补,在一个8位寄存器中存储一个4位数字是早晚的事重复其最显著位:

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位为好。它在最根本,最常见的操作之一,一个很小的额外步骤present在每个应用程序。

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.

这篇关于为什么用补重新present负数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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