Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overflow and Carry flags

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

like image 694
freezie Avatar asked Jan 31 '10 19:01

freezie


2 Answers

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)

like image 102
Arthur Shipkowski Avatar answered Sep 20 '22 16:09

Arthur Shipkowski


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.

like image 36
John Knoeller Avatar answered Sep 18 '22 16:09

John Knoeller