简体   繁体   中英

Overflow and Carry flags

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

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)

您无法访问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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM