gpt4 book ai didi

c - 如何在 C 中实现借用标志

转载 作者:行者123 更新时间:2023-12-04 21:17:24 26 4
gpt4 key购买 nike

两个8位数字相减如何计算借位标志?

我找到了这个借用标志的描述,但我还是不明白怎么做?

The carry (borrow) flag is also set if the subtraction of two numbers equires a borrow into the most significant (leftmost) bits subtracted.

我想我需要一个类轮。因为使用循环计算是很大的瓶颈。很高兴看到借用标志如何工作的解释。

谢谢。

最佳答案

如果您减去 x - y,则当 y > x 同时具有 y 时会发生借用(进位) x 被视为无符号量。因此,您应该可以使用 C 代码:

b = (unsigned)y > (unsigned)x;

如果你想要处理器实际计算它的方式,那么

x7 = x >> 7;
y7 = y >> 7;
r7 = (x - y) >> 7;
b = (~x7 & y7) | (y7 & r7) | (r7 & ~x7);

这是 2 位数字的真值表:

 x     y     r   b
00 00 00 0
00 01 11 1
00 10 10 1
00 11 01 1
01 00 01 0
01 01 00 0
01 10 11 1
01 11 10 1
10 00 10 0
10 01 01 0
10 10 00 0
10 11 11 1
11 00 11 0
11 01 10 0
11 10 01 0
11 11 00 0

您可以查看例如the HC12 Reference 中的 SBA 指令.这完全使用了上面给出的 b 表达式。

关于c - 如何在 C 中实现借用标志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17749946/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com