Is it possible to add two signed 8-bit numbers together and set both the carry and overflow bits?
-
That kinda depends on the processor, doesn't it? – dmckee --- ex-moderator kitten Jan 31 '10 at 19:10
-
I've removed the references to C in the title and tags, since this question is about CPU ops, not C. – Steve Jessop Jan 31 '10 at 22:12
3 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)

- 3,606
- 1
- 22
- 30
-
Thanks a lot for this. It make so much more sense now with your eloquent explanation. – freezie Jan 31 '10 at 20:10
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.

- 33,512
- 4
- 61
- 92
-
i don't think that he asked how to change the flags. he asked if both of the flags can be changed while doing a single operation – jojo Jan 31 '10 at 19:17
-
-
1And since C has no concept of carry/overflow flags, you can't do that - unless there's an extension to your particular C compiler that tells you otherwise. – nos Jan 31 '10 at 19:33
-
Not to mention that not all processors that have C compilers have 8 bit ADD instructions. But if the question is _in general_ can an add set both carry and overflow, the answer is yes. – John Knoeller Jan 31 '10 at 19:40
-
@nos if the processor has the flags, you can cause them to be set. Whether c can access the flags has nothing to do with it. – Justin Smith Jan 31 '10 at 20:21
-
To "get the compiler to generate code that set them", all you need to do is add two numbers and have the result carry and overflow. The processor sets the flags automatically (on all the processors I know of that have the flags, at the very least). – Justin Smith Jan 31 '10 at 20:44
-
1@Justin: Just because the CPU has an 8 bit addition op doesn't mean the C implementation uses it when you add together two 8 bit numbers as in the question. C arithmetic operations are never "logically" performed on types smaller than `int`, so you're relying on particular code transformations/optimisations. Anyway, I've removed the reference to C from the title and tags, since the actual question isn't about C. – Steve Jessop Jan 31 '10 at 22:17
-
@Steve Jessop: I'm glad you changed the tags, I wanted to, but I wasn't sure whether that would be seen as stepping over a line. – John Knoeller Jan 31 '10 at 23:02
-
The site philosophy is that it's like a Wiki - if the OP or anyone else with rep-based edit privileges wants to change it back, I can't stop them, so I don't think of it as interfering very much. I certainly won't get into an edit war over it, it just seems to me that "what can the C language guarantee about the state of CPU flags not mentioned in the C standard" probably isn't the important part of the question ;-) – Steve Jessop Jan 31 '10 at 23:14
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.

- 43,505
- 7
- 82
- 96
-
You simply don't know that in the general case. – dmckee --- ex-moderator kitten Jan 31 '10 at 19:23
-
@dmckee: I'm not sure what you're getting at here. One can certainly write an 'add' routine that returns carry and overflow flags in pure, portable C. It'll be dog-slow compared to inline assembly or other platform-specific hacks, but that wasn't the question... – Jim Lewis Jan 31 '10 at 19:44
-
Ah...I didn't read you "portable" weasel word. I think we are on the same page. Certainly you can't do anything useful with it baring non-standard extensions. – dmckee --- ex-moderator kitten Jan 31 '10 at 21:11
-
@dmckee: As an aspiring language lawyer, I appreciate the compliment on my talent for weasel-wording! – Jim Lewis Jan 31 '10 at 23:29