CPU是怎么做减法的? [英] How does the CPU do subtraction?

查看:36
本文介绍了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.

假设 A = 5,B = -2.假设A和B是4个字节,CPU是如何做A+B加法的?

Say A = 5, B = -2. Assuming that A and B are 4-bytes, how does the CPU do the A + B addition?

我知道 A 的符号位 (MSB) 为 0 以表示正值B 的符号位为 1 表示负整数.

I understand that A will have sign bit (MSB) as 0 to signify a positive value and B will have sign bit as 1 to signify a negative integer.

现在在C++程序中,我想打印A + B,ALU(算术逻辑单元)的加法模块是否先检查符号位,然后决定做减法,然后按照减法的过程.如何减法将是我的下一个问题.

Now when in C++ program, I want to print A + B, does the addition module of the 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.计算机将取 B 的 2 的补码并加上 B 的 A + 2 的补码并返回(在丢弃左边的多余位后)?

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

A = 2
B = 5

A - B.在这种情况下,计算机是怎么做的?

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

我知道任何 if-then 等条件逻辑都将在 ALU 内部的硬件中完成.计算2s补码等,丢弃多余的位都将在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-complement有符号幅度表示.

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),并且因为我们使用的是2的补码,所以我们不用担心(-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天全站免登陆