为什么选择二的补码? [英] Why Two's Complement?

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

问题描述

我正在写一个教程,教9至13岁的孩子编程。我是从计算机本身开始的,它们与计算机科学没有太大关系,更多的是与解决计算问题有关的过程。

I'm writing a tutorial to teach kids (ages 9 to 13) about programming. I started with computers themselves, they don't have that much to do with computer science, it's more about the process involved with a solution to a computational problem.

从这个出发点,我引导他们理解机器可以帮助我们解决某些计算问题。人们擅长抽象思维和想象力,但是计算机擅长遵循明确指定的例程。他们可以以惊人的速度一次又一次地做到这一点!

With that starting point, I'm guiding them toward an understanding that machines can help us with certain computational problems. People are great at abstract thinking and imagination, but computers are AWESOME at following a well-specified routine. They can do it again and again, at amazing speed!

在我的教程中已经介绍了用二进制格式表示数字。但是,您如何表示负数?在任何符号系统中,执行此操作的方法有很多,但是为计算机选择的系统是出于一个非常特定的原因:减少添加有符号整数值所涉及的机器数量。我们不想只为处理负数而构建和构建单独的芯片,我们想使用与自然数运算相同的芯片!

Representing numbers in binary format has already been covered in my tutorial. But how do you represent negative numbers? There are so many ways to do this, in any notational system, but the system chosen for computers is for a very specific reason: to reduce the amount of machinery involved with adding signed integer values. We don't want to have to construct and build separate chips just to handle negative numbers, we want to use the same chips we have been using for natural number arithmetic!

如果有人在街上问您(看起来似乎完全不现实),计算机如何代表负数,为什么它们又以这种方式代表它们?

If someone asked you on the street (as totally unrealistic as this seems) "how do computers represent negative numbers, and why do they represent them this way?"

我的具体问题:



  1. 计算机如何表示负数?

  1. How do computers represent negative numbers?

为什么计算机以这种方式表示负数?

Why do computers represent negative numbers this way?


我猜想,这么多有经验的开发人员将不得不对此有所考虑。有些人甚至可能无法给出答案。我并不是要吹牛,这是根据实际经验得出的,我已经问过专业开发人员这个问题,他们无法回答。他们茫然地凝视着。给他们JBoss和JavaBeans,他们就会充满信心。真好笑!我也为这个问题而苦恼,每次都必须使自己想起答案,并且我需要一张纸或白板来制定解决方案。我希望能引导学生更好地了解他们正在使用的机器。

I would guess that this many experienced developers would have to think about this a bit. Some might not even be able to come up with an answer. I'm not trying to be pompous, this is from actual experience, I've asked professional developers this question and they can't answer it. They draw a blank stare. Give them JBoss and JavaBeans and they will steamroll you with confidence. So funny! I too struggle with this question, I have to remind myself of the answers every time and I need a piece of paper or white board to work out a solution. What I'm hoping is to guide students toward a better understanding of the machine they are working with.

推荐答案


1。计算机如何表示负数?

1.How do computers represent negative numbers?

取正值,将所有位取反并加一个。

Take the positive value, invert all bits and add one.


2。为什么计算机用这种方式表示负数?

2.Why do computers represent negative numbers this way?

可以很容易地在-7中添加7并得出零。位操作很快。

It makes easy to add 7 in -7 and came up with a zero. The bit operations are fast.

它如何使它变得容易?

以7和-7为例。如果将7表示为 00000111 ,则要找到-7反转所有位并加一个:

Take the 7 and -7 example. If you represent 7 as 00000111, to find -7 invert all bits and add one:

11111000 -> 11111001

现在,您可以添加以下标准数学规则:

Now you can add following standard math rules:

  00000111
+ 11111001
-----------
  00000000

对于计算机而言,此操作相对容易,因为它基本上涉及逐位比较并携带一个。

For the computer this operation is relatively easy, as it involves basically comparing bit by bit and carrying one.

如果相反,您将-7表示为 10000111 ,则没有任何意义:

If instead you represented -7 as 10000111, this won't make sense:

  00000111
+ 10000111
-----------
  10001110 (-14)

要添加它们,您将涉及到更复杂的规则,例如分析第一位并转换值。

To add them, you will involve more complex rules like analyzing the first bit, and transforming the values.

不要忘记@trashgod所说的,在2的补码中,您只有一个零。要检查它:

And don't forget what @trashgod said, in 2's complement you have only one zero. To check it:


00000000

11111111 (反转所有位)

00000000 (加一)

00000000
11111111 (invert all bits)
00000000 (add one)

不同于 00000000 0 )等于 10000000 -0

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

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