6

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

Steve Jessop
  • 273,490
  • 39
  • 460
  • 699
freezie
  • 63
  • 1
  • 3

3 Answers3

6

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)

Arthur Shipkowski
  • 3,606
  • 1
  • 22
  • 30
1

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.

John Knoeller
  • 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
  • That is what I am asking, can both be changed in a single operation. – freezie Jan 31 '10 at 19:26
  • 1
    And 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
0

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.

Jim Lewis
  • 43,505
  • 7
  • 82
  • 96