如何CPU做减法 [英] How does CPU do subtraction

查看:671
本文介绍了如何CPU做减法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些基本的怀疑,但每次我坐在尝试我的手在面试问题,这些问题和我的疑惑弹出。

I have some basic doubts, but every time I sit to try my hands at interview questions, these questions and my doubts pop up.

说= 5

B = -2

我说A + B。我假设A和B是4字节
如何在CPU做加法?
据我所知,将有符号位(MSB)为0,表示正整数
和B将有符号位为1来表示负整数。

I say A+B. I am assuming that A and B are 4-bytes How does the CPU do this addition? I understand that A will have sign bit (MSB) as 0 to signify positive integer and B will have sign bit as 1 to signify negative integer.

现在当C ++程序,我想打印A + B,确实ALU(算术逻辑单元)的加入模块符号位先检查,然后决定做减法,然后按照减法的过程。扣除的方式将是我的下一个问题。

Now when in C++ program, I want to print A+B, does the addition module of ALU(Arithmetic Logic Unit) first check for sign bit and then decide to do subtraction and then follow the procedure of subtraction. How subtraction is done will be my next question.

A = 5

B = 2

想做A-B
计算机将需要2秒的B补充和添加A +二进制补码,并返回本(丢弃左额外位之后)?

want to do A-B computer will take 2 secs complement of B and add A+2s complement and return this (after discarding the extra bit on left)?

A = 2

B = 5

做A-B
如何计算机在这种情况下怎么办?

to do A-B How does computer do in this case?

我明白任何的if-then等种条件逻辑都将在硬件上进行内部ALU。计算二进制补等,丢弃额外的位都将在硬件上完全没有内部ALU。如何ALU的这一部分是什么样子?

I understand that any if-then etc kind of conditional logic all will be done in hardware inside ALU. computing 2s complement etc,discarding extra bit all will be done in hardware inside ALU. How does this component of ALU look like?

谢谢,

推荐答案

的全部原因,我们使用 2的-complement 是,加入的是相同的数字是否是正或负的 - 没有特殊的情况来考虑,如存在与 1's补符号 - 幅度再presentations。

The whole reason we use 2's-complement is that addition is the same whether the numbers are positive or negative - there are no special cases to consider, like there are with 1's-complement or signed-magnitude representations.

因此​​,要找到 A-B ,我们就可以否定B和补充;也就是说,我们发现 A +(-B),因为我们使用的是二进制补码,我们没有如果担心( - B )是正还是负的,因为除了算法的工作原理相同两种方式。

So to find A-B, we can just negate B and add; that is, we find A + (-B), and because we're using 2's-complement, we don't worry if (-B) is positive or negative, because the addition-algorithm works the same either way.

这篇关于如何CPU做减法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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