2

Asumming a=1

1+(-1) =0

but with 4 bit binary ,using 2's complement

0001+1111=10000 ~ 0000

Isn't signed integer overflow undefined behavior ?

Do we depend on undefined behavior for such trivial result or I am missing something .

from wikipedia: If the left two carry bits (the ones on the far left of the top row in these examples) are both 1s or both 0s, the result is valid; if the left two carry bits are "1 0" or "0 1", a sign overflow has occurred.

5
  • Why would this be any different than 8-bit, 16-bit, etc? If you add 000...01 to 111...11 you get 000...00.
    – lurker
    Jul 16, 2013 at 19:21
  • 1
    This may help as it discusses the carry bits and overflow: en.wikipedia.org/wiki/Two's_complement#Addition Jul 16, 2013 at 19:24
  • 1
    When you add signed number with opposite signals, there will never be an overflow, what you are seeing is the carry number propagation to enable larger container to operate correctly.
    – Amadeus
    Jul 16, 2013 at 19:44
  • "EDIT : from wikipedia link" which link?
    – Amadeus
    Jul 16, 2013 at 19:57
  • When Wikipedia talks about the left two carry bits, it clearly specifies they are not the ones in the far left of the top row in your examples. Look at the Wikipedia page again for 11111 111 (carry) Jul 16, 2013 at 20:03

4 Answers 4

3

Adding signed numbers with opposite signs never generate overflow. Indeed, hardware usually has two flag bits related to addition, i.e., overflow bit and carry bit.

Overflow bit is only set if the container is not large enough to represent the number correctly. For signed number the hardware set this bit after analyzing the sign bit of each operand and the sign of the results. If the signs of operands are differents, it will never set this bit. Otherwise, if the signals of operands are equals, it analyses the sign of result. If it is different from the operands, it set this flag, indicating that the result is in overflow.

In your example, the sign of each operand is different, so there no overflow, but this operation generate a carry bit if you want to use large container to keep operation correct.

1
  • I assumed you meant “sign” each time you wrote “signal”. Jul 16, 2013 at 20:06
2

Signed integer overflow is undefined, but I fail to see how this would be an issue here, since you are adding two signed numbers and the result does fall within range.

For a 4 bit integer type, overflow would be adding 7+7, where the result should be 14 that does not fit in the range -2^3..2^3-1

7
  • we know result is in range ,but at machine level adding 7+7 as 111+111=1110 is no different than adding 4+(-3) as 0100+1101=10001~0001.
    – Zxcv Mnb
    Jul 16, 2013 at 19:38
  • 1
    @ZxcvMnb: That depends on the hardware. Whether add for a signed value will add all bits and set the carry flag or it will yield the result without modifying the carry flag is defined by the hardware. Jul 16, 2013 at 19:46
  • In addition there cannot be an undefined overflow in 4-bit arithmetic because types smaller than int are promoted to int and int is at least 16 bits wide. For smaller types/bit-fields, there is only an implementation-defined conversion from int to the destination type/bit-field. Jul 16, 2013 at 20:03
  • @PascalCuoq: He's clearly only using 4 bits for purposes of simplicity, his question scales up with any finite number of bits. Jul 16, 2013 at 20:05
  • @PascalCuoq: I was considering a hypothetical architecture in which int might be just 4 bits (to avoid having to pull large numbers). But yes, integral types are promoted to int before the operation is performed. Jul 16, 2013 at 20:06
0

The underlying representation may internally use overflow to get the correct result, but that is irrelevant. The behavior is only undefined if the result of an operation exceeds the range of the type.

2
  • So are you saying undefined behaviour depends on our conclusion if range was exceeded rather than bits actually overflowing ?
    – Zxcv Mnb
    Jul 16, 2013 at 19:42
  • @ZxcvMnb From the language perspective, 1+-1 doesn't overflow, so it's not undefined behavior. What the compiler or hardware does is 100% irrelevant. Jul 16, 2013 at 19:59
-1

It is undefined behavior but I'm not sure I understand your question. Are you asking why it works in that example? Because those values are within range and the result is in range. Consider

-8 + -1

In 4 bit

1000 + 1111 -> 10111 -> 0111 -> 7. Here we no longer have the correct answer

2
  • "it is unsigned behavior"? Jul 16, 2013 at 20:15
  • Wow...terrible typo. Should not answer questions when I'm tired. Thanks for the extra set of eyes, fixed
    – sedavidw
    Jul 17, 2013 at 13:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.