溢出和进位标志

发布于 2024-08-20 06:26:45 字数 36 浏览 1 评论 0原文

是否可以将两个带符号的 8 位数字相加并设置进位和溢出位?

Is it possible to add two signed 8-bit numbers together and set both the carry and overflow bits?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

感性 2024-08-27 06:26:45

根据您的评论,您的问题似乎是“是否可以为涉及有符号数的二进制补码加法设置进位和溢出?”这是。典型的实现是将最后一个加法器的进位输入与链末尾的进位输出进行异或 - 因此,负数的溢出加法将导致设置进位位和要设置的溢出位。

这是一个例子,将-1添加到-128:

Carry 10000 0000 
       1000 0000  (-128)
       1111 1111  (-1)
       ---------
       0111 1111 (oops, this is 127!)

将设置进位,因为最后一次添加导致进位 - 并且将根据上面的规则设置溢出(另外,请注意,-128添加到-1显然不是127)

Per your comments, your question seems to be "is it possible to have both carry and overflow set for a two's complement add involving signed number?" It is. The typical implementation is to take the exclusive-OR of the carry-in for the last adder with the carry-out at the end of the chain -- hence, an overflowing addition of negative numbers will cause the carry-out bit to be set and the overflow bit to be set.

Here's an example, add -1 to -128:

Carry 10000 0000 
       1000 0000  (-128)
       1111 1111  (-1)
       ---------
       0111 1111 (oops, this is 127!)

Carry will be set, since the last add resulted in a carry -- and overflow will be set based on the rule above (also, note that -128 added to -1 is obviously not 127)

谈场末日恋爱 2024-08-27 06:26:45

您无权访问 C 中的标志,即使您可以让编译器生成设置它们的代码,您也无法使用它们。

You don't have access to the flags in C, even if you could get the compiler to generate code that set them, you have have no way to use them.

鱼忆七猫命九 2024-08-27 06:26:45

您可以用 C 语言编写自己的加法例程,该例程将返回进位和溢出标志
有符号的 8 位操作数。如果您指的是内部的硬件进位和溢出位
处理器,不,这不能在 C 中移植。

You can write your own add routine in C that will return carry and overflow flags for
signed 8-bit operands. If you're referring to the hardware carry and overflow bits inside
the processor, no, that cannot be done portably in C.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文