Skip to content
myAudience.
← Explore the library

Two’s complement subtraction and signed boundaries

By UK A Level Computer Science · independent study · myAudience

Eight cells 11101011 have two’s-complement weights −128,64,32,16,8,4,2,1; the same pattern denotes 235 unsigned or −21 signed. Addition 00100101+11000110=11101011 represents 37−58. Signed range −128..127 and desired 127+1=128 overflow are distinguished from stored 10000000 meaning −128. All numbers are stipulated teaching examples; see exact text and worked answer. No measured processor or instrument performance is depicted.

Original generated teaching illustration, built-in imagegen,2026-10-04; mathematical model,not documentary photograph. · Original generated artwork; operator provenance/rights and independent scientific acceptance pending.

Two’s complement subtraction and signed boundaries

Two’s complement changes the most significant bit's weight from positive 2^(n −1) to negative 2^(n −1), leaving the remaining weights positive. An eight-bit pattern therefore has weights −128,64,32,16,8,4,2,1. Its range is −128 through 127. The same 11101011 pattern interpreted unsigned is 235 but interpreted as two’s complement is −128+64+32+8+2+1=−21. The bits have not changed; the declared interpretation has. A sign bit is not a separate detachable minus sign in this scheme.

The range is asymmetric because the 2^n patterns include zero once. There are 2^(n −1) negative values and 2^(n −1) nonnegative values, giving minimum −2^(n −1) and maximum 2^(n −1)−1. Removing the leading bit from a negative pattern or applying a sign-magnitude rule changes the coding scheme. examines two’s complement for negative integers here. Other representations may be discussed as context, but they must not displace the specified encoding in a conversion or subtraction answer.

To encode a representable negative integer −a, take 2^n −a and write its n-bit unsigned pattern. Inverting the positive a pattern and adding one implements the same result modulo 2^n. For 58,00111010 becomes 11000101 on inversion and 11000110 after adding one, encoding −58. The operation is a finite-width construction; adding indefinitely many leading ones without a stated width obscures which positions are actually stored. Verify the negative-weight sum rather than relying only on the mnemonic.

Subtraction 37−58 becomes addition of 37 and −58. Patterns 00100101 and 11000110 sum to 11101011, or −21 under the eight-bit signed interpretation. The desired result lies in the signed range, so it is valid. Carry out is not by itself a signed-overflow rule. For example, adding −1 and 1 can produce a carry beyond the byte while the signed result zero is correct. The decision depends on the intended mathematical result and signed representability, not merely whether a ninth arithmetic bit appeared.

Signed addition overflows when two operands with the same sign produce a stored result with the opposite sign. At 127+1,01111111+00000001 yields 10000000, interpreted as −128. The desired 128 is not representable. At −128+(−1), the low byte becomes 01111111, or 127, although the desired −129 is below the minimum. Adding opposite signs cannot leave the interval between the operands, so it cannot overflow this signed range when both inputs are valid. This argument explains the sign test instead of treating it as a memorised exception.

Negating the minimum is another boundary trap. Inverting 10000000 and adding one returns 10000000 at eight bits. That does not prove that −128 equals its mathematical negative: positive 128 cannot be represented in the same signed width. Similarly, subtraction can overflow even when both operands fit, such as 100−(−50)=150. A checked contract must distinguish this case from a valid negative answer. Extending the representation before operating or returning a documented failure can preserve the intended meaning.

Sign extension preserves value when widening two’s complement: repeat the old sign bit on the left. Widening 11101011 to 1111111111101011 gives the same −21 because the added negative weight and added positive high weights cancel. Zero extension would produce 235 instead, a valid unsigned value but the wrong signed conversion. A representation boundary such as saving to a file must specify the width and signedness; the reader cannot safely reconstruct them by inspecting whether a result looks plausible.

Reading the diagram: The concept plate uses one pattern with two explicitly named interpretations and a signed interval labelled −128 and 127. Its subtraction panel encodes −58 before adding it to 37; the overflow panel labels 127+1 as an unrepresentable desired result rather than a legitimate negative inventory. The geometry is conceptual, not physical wiring. When transferring the method, calculate the mathematical result first, verify the permitted range and only then compare the stored pattern, separating algorithm errors from deliberate finite-width behaviour.

Worked example: Compute 37−58 using eight-bit two’s complement, then explain 127+1 and negation of −128.

37=00100101; 58=00111010; invert/addone gives −58=11000110.

Adding gives 11101011; negative-weight decoding yields −21, within −128..127.

127+1 desires 128 outside the range; its stored 10000000 would mean −128. Negating minimum −128 also cannot produce a representable positive 128.

Answer: 37−58=−21, encoded 11101011. The other two operations require widening or a defined overflow failure.

Check your understanding: Evaluate 100−(−50), explain why a carry from −1+1 is not signed overflow, and widen 11101011 to sixteen signed bits. Critique using an unsigned maximum 255 as the validity limit for a signed byte.

Answer: 100−(−50)=150 exceeds signed maximum 127 although each operand fits. Adding 11111111 and 00000001 produces a carry and low byte 00000000; signed −1+1=0 is valid. Sign extension gives 1111111111101011 and preserves −21. Zero extension would misinterpret it as 235. A limit 255 belongs to unsigned decoding: a signed byte only permits −128..127, so checking 0..255 both accepts invalid positive results and incorrectly rejects valid negatives.

Why: Require explicit signed weights, a mathematical range test and a counterexample distinguishing carry from overflow. The application must specify its response to unrepresentable results; a wrapped pattern is not proof that the desired result exists.