What is Two's complement?

The usual binary representation of negative integers. Inverting all bits and adding 1 negates a number. n bits cover −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1, and the same adder can be used for subtraction.

Field: Logic circuits

Japanese name: 2の補数

How the encoding works

In n-bit two's complement, every bit has its usual binary weight except the most significant bit, which carries weight −2ⁿ⁻¹. In 4 bits, 1111 therefore reads as −8 + 4 + 2 + 1 = −1, and 1000 is −8. To negate a value, invert every bit and add 1: +1 (0001) becomes 1110, then 1111. Zero has a single pattern, which leaves one more negative value than positive, so the range is −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1: −128 to +127 in 8 bits and −32,768 to +32,767 in 16 bits.

Why hardware uses it

Two's complement arithmetic is simply binary arithmetic modulo 2ⁿ, so one adder handles signed and unsigned numbers identically, and only the interpretation of the result differs. Subtraction A − B becomes A + (inverted B) + 1, implemented by inverting B and setting the adder's carry-in to 1. Sign-magnitude, where 1001 means −1 in 4 bits, has two zeros and needs separate add and subtract logic, and ones' complement also has two zeros and an end-around carry. Booth multipliers likewise rely on two's complement to generate negative partial products cheaply.

Overflow, carries and flags

Signed overflow happens only when two operands of the same sign produce a result of the opposite sign, for example 0111 + 0001 = 1000 (7 + 1 giving −8) in 4 bits; adding numbers of opposite sign never overflows. Hardware detects it as the XOR of the carry into and the carry out of the most significant bit, reported as the V (overflow) flag. The C (carry) flag instead signals unsigned overflow. The asymmetric range creates corner cases: negating or taking the absolute value of the most negative number, or computing −128 × −1 in 8 bits, overflows.

Sign extension and shifts

Widening a two's complement value copies its sign bit into the new upper bits. A RISC-V addi immediate is a 12-bit field covering −2048 to 2047; −1 is stored as 0xFFF and sign-extends to 0xFFFFFFFF, whereas zero-filling would give 4095. An arithmetic right shift also replicates the sign bit, so 1111 1000 (−8) becomes 1111 1100 (−4); for negative odd values it rounds toward negative infinity, so −7 shifted right by one gives −4, while integer division in C truncates toward zero to −3. Offset binary, common at ADC outputs, differs from two's complement only by an inverted MSB.

Try it: questions about Two's complement

Which bit pattern represents −1 in 4-bit two's complement?

  1. A1111
  2. B1001
  3. C0001
Show answer and explanation

Answer: A. 1111

In two's complement, a negative number is formed by inverting the bits of the positive value and adding 1: +1 (0001) → 1110 → 1111 = −1. Treating the top bit as having weight −8, 1111 = −8 + 4 + 2 + 1 = −1 can be read directly. Four bits cover −8 to +7, and since subtraction becomes addition of a complement, nearly every computer uses it. 1001 is −1 in sign-magnitude form.

What range of integers can be represented in 8-bit two's complement?

  1. A−128 to +127
  2. B−127 to +127
  3. C0 to 255
Show answer and explanation

Answer: A. −128 to +127

n-bit two's complement covers −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1, so 8 bits span −128 to +127. Zero has a single representation (00000000), which leaves one extra negative value. Edge values need care—for example, −128 × −1 overflows because +128 cannot be represented. 16 bits cover −32,768 to +32,767 and 32 bits about ±2.1 billion. 0–255 is the same 8 bits interpreted as unsigned.

FAQ

How do you find the two's complement of a negative number?

Write the positive value in binary with the chosen number of bits, invert every bit, then add 1. For example, −5 in 8 bits is 00000101 → 11111010 → 11111011.

How can you tell if two's complement addition overflowed?

Overflow occurs when two positives give a negative or two negatives give a positive, which hardware detects when the carry into the MSB differs from the carry out of it. The carry-out alone indicates unsigned overflow, not signed overflow.

Why is the range of 8-bit two's complement −128 to 127 and not symmetric?

Zero uses one of the 256 patterns with a 0 sign bit, leaving 127 positive values, while all 128 patterns with a 1 sign bit are negative. As a result +128 cannot be represented.

Questions that mention Two's complement (6)

Two's complement appears in 8 practice questions across 4 topics, including Logic circuits, Computer Arithmetic, ISA & Assembly.

  1. When adding two's complement numbers, which condition indicates that an overflow has occurred?
  2. Which correctly describes the "offset binary" code used in ADCs that handle both positive and negative voltages?
  3. What is the main effect of using radix-4 Booth's algorithm (Booth encoding) in a multiplier?
  4. What is the result of arithmetic right-shifting the 8-bit signed integer −8 (1111 1000) by one bit?
  5. The immediate of RISC-V addi is a 12-bit signed integer. What range can it represent?
  6. On RISC-V RV32I, what 32-bit value is in t0 after "addi t0, zero, -1"?

Books on Logic circuits Ad

Learn hands-on: parts and kits

Building real circuits is a good way to check what you learned in the questions.

As an Amazon Associate I earn from qualifying purchases.

Semiconductor, Electronics and Computing Calculators

This page was drafted with AI and fact-checked by a separate AI review before publication. Our Editorial policy explains how we write and check content. If you spot an error, please tell us on our Contact page.

Back to the glossary