10

It has been a long time since I last programmed at the bits and bytes level and wanted to confirm something I seem to remember from those days:

Say I have two integers of equal length (1, 2, 4, 8 bytes; it doesn't matter), and I add them up: does the bit-by-bit result of the sum differ if they are signed or unsigned. In other words: regardless of whether they are signed or unsigned integers, will the bits end up being the same?

My intuition and my frail memory tell me they will, but I just wanted to confirm. Thanks.

Eduardo
  • 8,362
  • 6
  • 38
  • 72
  • 1
    IIRC, although the result looks OK, the C/C++ standard says integer overflow results in undefined behaviour. **EDIT**: Unsigned arithmetic does not overflow, and follow the wrap-around normally. – nhahtdh Feb 19 '13 at 16:29
  • @nhahtdh: thanks; that's OK. I tagged it with C++ because I thought it would be a good audience to ask, but I will be using this in another language. – Eduardo Feb 19 '13 at 16:30
  • 2
    http://stackoverflow.com/questions/9024826/how-disastrous-is-integer-overflow-in-c – nhahtdh Feb 19 '13 at 16:35

2 Answers2

13

Assuming the implementation uses 2's complement as representation of signed integers, then the results will be the same. In other representations, they won't.

EDIT

As pointed out in comments, overflow in signed addition is undefined behaviour, which means nothing can be said about the results in such case.

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
4

I know this has already been answered, but all processors that I've ever worked with (about a dozen different architectures - and I mean architectures, not different flavours) only have ONE type of ADD instruction - it may have different size options, but it's the one instruction. The same applies to subtract. It is different on multiply and divide which typically has variants for signed and unsigned - or require the input to be "sign adjusted" in some way.

The only other instructions that make a distinction between signed and unsigned are the conditional instructions, e.g. "branch on less than" will have one variant for "unsigned less than" and one variant for "signed less than" (one of which usually is called something other than "less than", such as "below" or "carry set" or some such).

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
  • This is a very good point - even though the standard says the behavior is undefined, it's unlikely to do something that the underlying CPU architecture doesn't. That's not how C and C++ roll. – Mark Ransom Feb 19 '13 at 17:00
  • 1
    @MarkRansom It really depends on context. The question linked by nhahtdh above shows an example where the compiler intervenes. – Angew is no longer proud of SO Feb 19 '13 at 17:03
  • Yes. Of course, there may be SOME CPU's that have different signed and unsigned instructions for "add" and "subtract", I've yet to see one [or even hear about one]. The C/C++ specifications are written by language lawyers that have to satisfy all manner of WEIRD hardware. That doesn't mean that we'll see that type of hardware very often... ;) – Mats Petersson Feb 19 '13 at 17:03
  • MIPS has two (four in fact, because add immediate) ADD instructions: one that traps and one that doesn't. –  Feb 19 '13 at 17:04
  • Is that trapping on overflow for signed or unsigned values? – Mats Petersson Feb 19 '13 at 17:06
  • Actually, I was wondering if there was any difference at processor level (not in the experience that I recall). Thank you for enhancing the answer. – Eduardo Feb 19 '13 at 19:42