Computer Engineering is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

Signed binary numbers: 1's and 2's complement

How signed integers are represented and processed in binary using 1's complement and 2's complement, with conversion methods, arithmetic rules, overflow detection and worked examples.

Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.

Why it matters

Two common ways to represent negative integers in digital systems are 1's complement and 2's complement. Engineers use these representations in CPU arithmetic units (ALUs), digital signal processors, and any hardware or low-level software that adds and subtracts signed integers. Choosing and understanding the representation affects range, ease of arithmetic, overflow handling and hardware complexity.

Key ideas

Start from first principles. A binary word of n bits is a sequence b_{n-1} b_{n-2} ... b_1 b_0 where each b_i ∈ {0,1}. The leftmost bit b_{n-1} is called the most-significant bit (MSB); the rightmost b_0 is least-significant bit (LSB).

  • Unsigned binary: interpret all bits as powers of two: value = Σ_{i=0}^{n-1} b_i·2^i. Range 0 to 2^n − 1.

  • Signed representation: we want to represent negative integers too. Several conventions exist. Define each before use.

  • Sign–magnitude: the MSB is the sign bit (0 = positive, 1 = negative), remaining n−1 bits give the magnitude in unsigned form. Range: −(2^{n-1}−1) to + (2^{n-1}−1); there are two zeros (+0 and −0).

  • 1's complement (one's complement): the negative of a number is formed by bitwise inversion (flip every bit). If X is the n-bit unsigned pattern of the magnitude, then −X (in 1's complement) is the bitwise NOT of X. This also gives two zeros (+0 and −0). Range: −(2^{n-1}−1) to + (2^{n-1}−1).

  • 2's complement (two's complement): the negative of a number is formed by inverting all bits (1's complement) and adding 1 (modulo 2^n). Equivalently the n-bit pattern for −X equals 2^n − X (mod 2^n) for 0 ≤ X ≤ 2^{n-1}; if X > 2^{n-1}, −X is not representable in n bits. Important property: arithmetic addition of n-bit two's-complement values is the same as addition modulo 2^n; with this representation, there is a single zero and most negative number has no positive counterpart. Range: −2^{n-1} to 2^{n-1} − 1.

  • Exact value interpretation for a stored n-bit pattern b_{n-1} b_{n-2} ... b_0 in two's complement: value = −b_{n-1}·2^{n-1} + Σ_{i=0}^{n-2} b_i·2^i. This is the standard formula used to decode an n-bit two's-complement binary.

  • Why two's complement is preferred in hardware: addition and subtraction use the same adder circuit as unsigned arithmetic because negative numbers are encoded so that arithmetic modulo 2^n works. Overflow detection is simpler: overflow occurs when adding two numbers of the same sign produces a result of the opposite sign. Subtraction is implemented as A + (two's complement of B); apply the same overflow test to the sum.

  • End-around carry (1's complement addition): when adding two 1's-complement numbers, if a carry out from MSB occurs, it must be added back to the LSB (this is called the end-around carry). This is required to keep results in the 1's-complement range.

  • Sign extension: to increase bit-width from n to m > n without changing numeric value: for unsigned numbers pad with zeros on left; for two's-complement signed numbers replicate the MSB (sign bit) on left (arithmetic extension). For 1's-complement and sign–magnitude, extend sign bit and be careful with negative-zero cases; when extending ±0, preserve the all-zeros (for +0) or all-ones (for −0) encoding.

  • Overflow conditions (two's complement): algebraic rule used in exams and hardware: when adding two n-bit two's-complement operands A and B, overflow occurs iff the carry into MSB and carry out of MSB differ, equivalently if A and B have same sign and result has opposite sign. This must be stated exactly in answers.

Where an exam needs diagrams: draw an n-bit box with bits labelled b_{n-1} (MSB, sign) down to b_0 (LSB). For 2's complement arithmetic, draw two addends A and B entering an n-bit binary adder, show carry into MSB and carry out of MSB lines, and mark overflow condition when carries differ.

Formulas

unsigned value = Σ_{i=0}^{n-1} b_i · 2^i — b_i is bit i (dimensionless), applies to unsigned interpretation of n bits.

two's-complement value = −b_{n-1}·2^{n-1} + Σ_{i=0}^{n-2} b_i · 2^i — b_{n-1} is sign bit; value is integer (no SI unit), for n-bit two's complement. one's-complement value = Σ_{i=0}^{n-2} b_i · 2^i − b_{n-1} · (2^{n-1} − 1) — linear decode for n-bit 1's complement; yields +0 for all-zeros and −0 for all-ones.

pattern for −X (two's complement, n bits) = (2^n − X) mod 2^n — valid for integer magnitudes 0 ≤ X ≤ 2^{n-1}; if X > 2^{n-1}, −X is not representable in n bits.

range (2's complement, n bits) = [−2^{n-1}, 2^{n-1} − 1] — integer range for n-bit two's complement.

range (1's complement, n bits) = [−(2^{n-1} − 1), 2^{n-1} − 1] — note presence of both +0 and −0.

overflow occurs ⇔ (carry into MSB) ≠ (carry out of MSB) — used when adding two's-complement n-bit numbers.

Worked examples

All examples use n = 8 bits (byte). Units: integer (no physical unit).

Example 1 — Convert −25 to 8-bit two's complement and show value decode.

  1. Start with +25 in binary (unsigned) using 8 bits: 25_{10} = 00011001_2.
  2. Compute 1's complement: invert bits → 11100110.
  3. Add 1 to get 2's complement: 11100110 + 00000001 = 11100111.
  4. Verify by decoding with formula: value = −b7·2^7 + Σ_{i=0}^6 b_i·2^i = −1·128 + (1·64 + 1·32 + 0·16 + 0·8 + 1·4 + 1·2 + 1·1) = −128 + (64+32+4+2+1) = −128 + 103 = −25. Final answer: 11100111 (8-bit two's complement for −25).

Example 2 — Add −25 and +13 in 8-bit two's complement; show result and check overflow.

  1. Represent operands: −25 = 11100111 (from example 1). +13 = 00001101.
  2. Binary addition (8-bit): 11100111
  • 00001101 = 11110100 (no carry out beyond MSB).
  1. Check sign: MSB = 1 so result negative. Decode: invert and add 1 → invert 00001011, add 1 → 00001100 = 12, so value −12. Arithmetic check: −25 + 13 = −12, matches. Overflow? Both operands had opposite signs (− and +), so overflow cannot happen. Carry into MSB = carry out = (no mismatch). Final: 11110100 (value −12).

Example 3 — Overflow when adding +70 and +70 in 8-bit two's complement.

  1. +70 = 01000110 (8-bit).
  2. Add: 01000110 + 01000110 = 10001100. Carry into MSB = 1 and carry out of MSB = 0 (they differ), so overflow by the carry-in/out rule; MSB = 1 implies the wrapped result would be negative if interpreted without overflow.
  3. Decode 10001100: invert → 01110011, add 1 → 01110100 = 116, so result interpreted = −116. Mathematical sum 70 + 70 = 140, which cannot be represented in 8-bit two's complement (range −128 to +127), so overflow occurred. Rule confirmation: both addends were positive and result sign is negative → overflow. Final: overflow (result bits 10001100 which represents −116 but arithmetic overflow).

Example 4 — One's complement negative and end-around carry (8-bit): show +50 + (−50).

  1. +50 = 00110010.
  2. −50 in 1's complement = invert bits = 11001101.
  3. Add in 8-bit: 00110010 + 11001101 = 11111111 with no carry out. In 1's complement 11111111 is −0 (negative zero). If there had been a carry out from MSB, we would add that carry (end-around) to LSB.
  4. This shows two zeros exist and how end-around carry is used in 1's-complement systems.

Common mistakes

  • Forgetting end-around carry for 1's-complement addition.
  • Detecting overflow by checking only final carry out: in two's complement, final carry out alone does not indicate overflow — must compare carry into and out of MSB or use sign-bit rule.
  • When sign-extending, padding with zeros for a negative two's-complement number instead of replicating sign bit (creates wrong negative value).
  • Not checking that adding two operands of opposite sign cannot cause overflow (unless magnitude results exceed range via special mistakes when examples incorrectly interpret bits). Also, attempting to negate the most negative two's-complement value (e.g., −128 for 8-bit) overflows and yields the same bit pattern.

For your exam

Kinds of questions:

  • Definitions and comparison: define sign–magnitude, 1's complement, 2's complement; list advantages/disadvantages.
  • Short notes: explain why two's complement simplifies hardware addition; explain end-around carry for 1's complement.
  • Derivations: show mathematically that two's complement negative equals 2^n − X and prove decoding formula value = −b_{n-1}·2^{n-1} + Σ_{i=0}^{n-2} b_i·2^i.
  • Numericals: convert decimal negatives to n-bit 1's and 2's complement, add/subtract signed numbers showing overflow detection, sign extension exercises.
  • Diagrams: draw n-bit adder showing carry into and out of MSB for overflow detection.

What a full-marks answer includes:

  • Precise definitions of terms used (state bit positions, sign bit meaning).
  • Exact conversion steps (for 1's complement and 2's complement) with bit patterns shown.
  • Range statements with formulas and explanation of why ranges differ.
  • For arithmetic questions, show all bitwise addition steps, indicate carry bits, decode result and explicitly state if overflow occurred and why (use carry-in vs carry-out or same-sign rule).

Quick check

  1. What is the 8-bit two's-complement representation of −45?
  2. What is the numeric range of 6-bit two's-complement numbers?
  3. When adding two's-complement numbers, give a simple rule to detect overflow.

Answers: 1) Start +45 = 00101101 → invert 11010010 → add1 → 11010011. 2) Range = [−32, +31]. 3) Overflow occurs when addends have the same sign and the result has the opposite sign (equivalently carry into MSB ≠ carry out of MSB).

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?