G27
Self-checkedTier 1 12 min readdl.signed-number-representation

Sign-magnitude, 1's and 2's complement

How a fixed number of bits represents negative numbers: the three systems, their ranges, the two zeros, negation, sign extension, shifts, excess-K and the radix complements used in subtraction.

Why it exists

A bit string has no sign by itself. To store -5 in 8 bits we must agree on a rule that says which patterns stand for negative numbers. Three rules have been used in real machines. Today almost every computer uses 2's complement, because with it the same adder circuit adds signed and unsigned numbers, subtraction is addition of the negated number, and there is exactly one zero. The other two systems still appear in GATE questions, mostly to test whether you know their defects (two zeros, a special end-around carry), and the floating-point format in IEEE 754 uses sign-magnitude for the whole number and a biased (excess) code for the exponent.

Prerequisites

You should be able to convert between decimal and binary and to count bits, as covered in number systems and conversion.

Precise definitions

Fix a word of nn bits bn−1 bn−2…b0b_{n-1}\,b_{n-2}\dots b_0. The leading bit bn−1b_{n-1} is the sign bit in every signed system below: 0 means non-negative, 1 means negative (for the three systems below). As an unsigned number the word has the value ∑bi2i\sum b_i 2^i, range 00 to 2n−12^n - 1.

Sign-magnitude. The sign bit gives the sign and the remaining n−1n-1 bits give the magnitude. Value =(−1)bn−1⋅∑i<n−1bi2i= (-1)^{b_{n-1}} \cdot \sum_{i<n-1} b_i 2^i. Range −(2n−1−1)-(2^{n-1}-1) to +(2n−1−1)+(2^{n-1}-1). There are two zeros: 00…000\dots0 and 10…010\dots0. To negate, flip the sign bit.

1's complement. A negative number is the bitwise complement of the positive one. Value =−bn−1(2n−1−1)+∑i<n−1bi2i= -b_{n-1}(2^{n-1} - 1) + \sum_{i<n-1} b_i 2^i. Range −(2n−1−1)-(2^{n-1}-1) to +(2n−1−1)+(2^{n-1}-1). There are two zeros: 00…000\dots0 and 11…111\dots1. To negate, flip every bit. Addition needs an end-around carry: a carry out of the top bit is added back into the least significant bit.

2's complement. The value is −bn−1 2n−1+∑i<n−1bi2i-b_{n-1}\,2^{n-1} + \sum_{i<n-1} b_i 2^i. Equivalently, a pattern with sign bit 1 and unsigned value uu stands for u−2nu - 2^n. Range −2n−1-2^{n-1} to +2n−1−1+2^{n-1}-1, which is asymmetric (one more negative number). There is one zero. The pattern 10…010\dots0 is −2n−1-2^{n-1} and has no positive partner.

8 bitssign-magnitude1's complement2's complement
range−127-127 to 127127−127-127 to 127127−128-128 to 127127
zeros00000000, 1000000000000000, 1111111100000000
−5-5100001011111101011111011
negateflip sign bitflip all bitsflip all bits, add 1

Negation in 2's complement. The negative of xx in nn bits is 2n−x2^n - x (taken modulo 2n2^n). Since 2n−x=(2n−1−x)+12^n - x = (2^n - 1 - x) + 1, and 2n−1−x2^n - 1 - x is just xx with every bit flipped, the rule is invert and add 1. A faster hand rule: starting from the right, copy the bits up to and including the first 1, then flip all the bits to the left of it. The only number that is its own negative (apart from 0) is −2n−1-2^{n-1}, so negating it overflows.

Sign extension. To widen an nn-bit 2's complement number to m>nm > n bits, copy the sign bit into the new high bits. The value is unchanged. (For an unsigned number pad with zeros instead.) Truncation to fewer bits keeps the low bits and may change the value.

Shifts. In 2's complement, shifting left by one bit multiplies by 2 if the result is still representable. An arithmetic shift right copies the sign bit into the vacated position and computes ⌊x/2⌋\lfloor x/2 \rfloor (rounding toward minus infinity). A logical shift right fills with 0 and treats the word as unsigned. In C, integer division truncates toward zero, so −5/2-5/2 is −2-2 while −5≫1-5 \gg 1 is −3-3 on a machine with arithmetic shifts.

Excess-KK (biased) representation. The stored unsigned value is the true value plus KK, so the true value is stored minus KK. For nn bits the common bias is K=2n−1K = 2^{n-1}, which gives the same ordering as the signed numbers. Excess-128 in 8 bits equals the 2's complement pattern with the sign bit flipped. The IEEE 754 exponent uses excess-127 (single) and excess-1023 (double).

Radix complements (for arithmetic by hand). For nn digits in base rr, the (r−1)(r-1)'s complement of NN is rn−1−Nr^n - 1 - N (flip every digit to r−1−dr-1-d) and the rr's complement is rn−Nr^n - N. For r=2r = 2 these are the 1's and 2's complements; for r=10r = 10 they are the 9's and 10's complements. Subtraction A−BA - B is done as A+(r’s complement of B)A + (r\text{'s complement of } B): a carry out of the top digit is dropped and means the result is non-negative; no carry means the result is negative and equals minus the rr's complement of the sum.

How it works

Step through the animation. Each pattern is computed by a script and decoded again by each system's own rule in the tests. The sign bit is ringed.

Sign-magnitude, 1's complement and 2's complement: A positive number looks the same in all three

Computed exactly, not typed

Working

+5: sign bit 0, magnitude 101

Value, format and bits (changed bits in amber)

sign-magnitudesign-magnitude= 5
00000101
1's complementones-complement= 5
00000101
2's complementtwos-complement= 5
00000101

Step 1

+5 = 0000 0101 in every system. The leading bit is 0 for non-negative numbers.

1 / 7
computed by script: exact integer, Fraction and truth-table evaluation (IEEE 754 patterns via Python struct where used), not typed by hand. Python 3.15.1, darwin arm64. python3 scripts/gen-dl-traces.py

Why 2's complement works: modulo 2n2^n arithmetic is a closed system, and in it −x-x and 2n−x2^n - x are the same number. A circuit that adds modulo 2n2^n therefore adds signed numbers correctly, with no knowledge of the signs, as long as the true result is representable. That is what you see in fixed-point arithmetic and in the adder and subtractor circuits.

Worked examples

EX 1Represent -37 in 8 bits in all three systems

Easy

Step 1. +37=0010 0101+37 = 0010\,0101 (32 + 4 + 1).

Step 2 (sign-magnitude). Flip only the sign bit: 1010 01011010\,0101.

Step 3 (1's complement). Flip every bit: 1101 10101101\,1010.

Step 4 (2's complement). Flip every bit and add 1: 1101 1010+1=1101 10111101\,1010 + 1 = 1101\,1011. Check with the fast rule: from the right copy up to the first 1 (bit 0), then flip the rest: 1101 10111101\,1011. Check with the formula: 256−37=219=1101 1011256 - 37 = 219 = 1101\,1011.

#include <stdio.h>
#include <stdint.h>
 
int main(void) {
    int8_t a = -37;
    uint8_t u = (uint8_t)a;
    printf("%d as unsigned byte %u, hex %02X\n", a, u, u);
    int8_t m = -128;
    int8_t neg = (int8_t)(-m);
    printf("-(-128) in 8 bits: %d\n", neg);
    printf("%d %d\n", -5 >> 1, -5 / 2);
    int wide = 255;
    int8_t narrow = (int8_t)wide;
    printf("%d\n", narrow);
    int8_t s = (int8_t)0xA5;
    int16_t e = s;
    printf("%d %d %04X\n", s, e, (uint16_t)e);
    return 0;
}

Output (the same program also shows the traps below):

-37 as unsigned byte 219, hex DB
-(-128) in 8 bits: -128
-3 -2
-1
-91 -91 FFA5

The first line confirms the 2's complement pattern: hex DB is 1101 10111101\,1011.

EX 2Decode 1111 0110 in each system

Easy

Unsigned. 128+64+32+16+4+2=246128 + 64 + 32 + 16 + 4 + 2 = 246.

2's complement. The sign bit is 1, so the value is 246−256=−10246 - 256 = -10. (Check: invert to 0000 10010000\,1001, add 1 to get 0000 1010=100000\,1010 = 10.)

Sign-magnitude. Sign 1, magnitude 111 0110=118111\,0110 = 118, so −118-118.

1's complement. Sign bit is 1, so invert: 0000 1001=90000\,1001 = 9, giving −9-9. (Also 246−255=−9246 - 255 = -9.)

EX 3Ranges and counts

Medium

For 6 bits: 2's complement runs from −32-32 to +31+31 (6464 values); sign-magnitude and 1's complement run from −31-31 to +31+31 (6363 distinct values, because two patterns are both zero). For nn bits the 2's complement minimum is −2n−1-2^{n-1} and the maximum is 2n−1−12^{n-1} - 1.

Check with 16 bits. −32768-32768 to 3276732767. The range is not symmetric: the negative of −32768-32768 is 3276832768, which does not fit as a positive 16-bit number.

EX 4Negating the most negative number

Medium

In 8 bits, −128=1000 0000-128 = 1000\,0000. Invert: 0111 11110111\,1111. Add 1: 1000 00001000\,0000. The result is −128-128 again. The mathematical answer +128+128 needs 9 bits. This is why abs(INT_MIN) is not safe and why -x == x can be true for a non-zero xx. The program in example 1 prints −128-128 for −(−128)-(-128) (in C, -m is computed as the int 128; converting 128 to int8_t is implementation-defined, and gcc keeps the low 8 bits, giving −128-128).

EX 5Sign extension and truncation

GATE level

Extension. The 8-bit pattern 1010 01011010\,0101 is −91-91 in 2's complement. Sign-extended to 16 bits it is 1111 1111 1010 01011111\,1111\,1010\,0101, which is hexadecimal FFA5 and still −91-91 (the program in example 1 prints -91 -91 FFA5). Zero-extension would give 0000 0000 1010 0101=1650000\,0000\,1010\,0101 = 165, which is a different number.

Truncation. The value 255 stored in 8 bits is 1111 11111111\,1111, which is read as −1-1 in 2's complement. Similarly 300300 truncated to 8 bits is 300−256=44300 - 256 = 44. Truncation keeps only the low bits.

EX 6Right shift versus division

GATE level

For x=−5=1111 1011x = -5 = 1111\,1011 in 8 bits:

Arithmetic shift right by 1. Copy the sign bit in: 1111 1101=−31111\,1101 = -3. This is ⌊−5/2⌋=⌊−2.5⌋=−3\lfloor -5/2 \rfloor = \lfloor -2.5 \rfloor = -3.

C division. -5 / 2 truncates toward zero: −2-2. (The program in example 1 prints -3 -2.)

Logical shift right would give 0111 1101=1250111\,1101 = 125, which only makes sense for unsigned numbers.

Left shift. x≪1=1111 0110=−10=2xx \ll 1 = 1111\,0110 = -10 = 2x. This is exact as long as the result is in range: −100≪1-100 \ll 1 would need −200-200, which does not fit in 8 bits.

EX 7Excess-K and radix complements

GATE level

Excess-127. The stored value 1000 0010=1301000\,0010 = 130 stands for 130−127=3130 - 127 = 3. (This is how the exponent field of a float is read.)

Subtraction with 10's complements (5 digits). Compute 72532−325072532 - 3250. The 10's complement of 0325003250 is 100000−3250=96750100000 - 3250 = 96750. Add: 72532+96750=16928272532 + 96750 = 169282. Drop the carry out of the top digit: the result is 6928269282, and the carry shows it is positive.

The other way round. Compute 3250−725323250 - 72532. The 10's complement of 7253272532 is 2746827468. Add: 3250+27468=307183250 + 27468 = 30718. There is no carry out, so the result is negative: minus the 10's complement of 3071830718, which is −(100000−30718)=−69282-(100000 - 30718) = -69282.

Cheat box

Cheat box

  • nn-bit ranges. Unsigned: 00 to 2n−12^n-1. Sign-magnitude and 1's complement: −(2n−1−1)-(2^{n-1}-1) to 2n−1−12^{n-1}-1, two zeros. 2's complement: −2n−1-2^{n-1} to 2n−1−12^{n-1}-1, one zero.
  • 2's complement value: −bn−12n−1+∑i<n−1bi2i-b_{n-1}2^{n-1} + \sum_{i<n-1} b_i 2^i. Pattern with sign bit 1 and unsigned value uu is u−2nu - 2^n.
  • Negate in 2's complement: invert and add 1, or copy up to the first 1 from the right and invert the rest. −x≡2n−x-x \equiv 2^n - x.
  • −(−2n−1)=−2n−1-(-2^{n-1}) = -2^{n-1} (overflow).
  • Sign extension: copy the sign bit. Zero extension is for unsigned.
  • Arithmetic right shift is ⌊x/2⌋\lfloor x/2 \rfloor. C's / truncates toward zero. They differ for negative odd xx.
  • Excess-KK: stored == value +K+ K. Excess-2n−12^{n-1} is 2's complement with the sign bit flipped.
  • 1's complement addition: add the carry out of the top back into the bottom (end-around carry).
  • rr's complement =rn−N= r^n - N; (r−1)(r-1)'s complement =rn−1−N= r^n - 1 - N.

Traps and GATE patterns

Connections

The addition of 2's complement numbers and the meaning of the carry and overflow flags is the topic of fixed-point arithmetic. The hardware that subtracts by adding the complement is in adders and subtractors. C's int, short and char use these rules, which is why integer promotion and conversions behave as they do. The exponent field in IEEE 754 is excess-127.

PYQs

13 previous year questions from your book. I re-solved each one myself (13 by running or simulating, 0 by reasoning only); the book's key is shown next to my answer. All PYQs

GATE 2026 · Set 1 · MSQ · 1 markQ2 · page 153 of the book
Consider the 8-bit signed integers X, Y and Z
represented using the sign-magnitude form. The
binary representations of X and Y are as follows:

X: 10110100
Y: 01001100

Which of the following operations to compute Z
result(s) in an arithmetic overflow?
  • (a)Z = X + Y
  • (b)Z = X - Y
  • (c)Z = -X + Y
  • (d)Z = -X - Y
GATE 2026 · Set 2 · MSQQ3 · page 153 of the book
In a system, numbers are represented using 4-
bit two's complement form. Consider four
numbers
N1 = 1011, N2 = 1101, N3 = 1010 and N4 =
1001 in the system.

Which of the following operations will result in
arithmetic overflow?
  • (a)N1 + N2
  • (b)N2 + N3
  • (c)N3 - N4
  • (d)N1 + N4
GATE 2025 · Set 1 · MSQ · 1 markQ5 · page 153 of the book
The number  6 can be represented as 1010 in 4-
bit 2's complement representation.
Which of the following is/are CORRECT 2's
complement representation(s) of -6?
  • (a)1000 1010 in 8-bits
  • (b)1111 1010 in 8-bits
  • (c)1000 0000 0000 1010 in 16-bits
  • (d)1111 1111 1111 1010 in 16-bits
GATE 2024 · Set 1 · MCQ · 1 markQ8 · page 154 of the book
Consider a system that uses 5 bits for representing
signed integers in 2's complement format. In this
system, two integers A and B are represented as A
= 01010 and B = 11010. Which one of the
following operations will result in either an
arithmetic overflow or an arithmetic underflow?
  • (a)A + B
  • (b)A - B
  • (c)B - A
  • (d)2 * B
year not read yetQ11 · page 154 of the book
Let R1 and R2 be two 4-bit registers that store
numbers in 2's complement form. For the operation
R1 + R2, which one of the

following values of R1 and R2 gives an arithmetic
overflow?
  • (a)R1 = 1011 and R2 = 1110
  • (b)R1 = 1100 and R2 = 1010
  • (c)R1 = 0011 and R2 = 0100
  • (d)R1 = 1001 and R2 = 1111
GATE 2019 · MCQQ15 · page 154 of the book
Consider Z = X - Y where X, Y and Z are all in sign-
magnitude form. X and Y are each represented in n
bits. To avoid overflow, the representation of Z would
require a minimum of:

DIGITAL LOGIC
  • (a)n bits
  • (b)n - 1 bits
  • (c)n + 1 bits
  • (d)n + 2 bits
GATE 2019 · MCQ · 1 markQ16 · page 155 of the book
In 16-bit 2's complement representation, the
decimal number  28 is:
  • (a)1111 1111 0001 1100
  • (b)0000 0000 1110 0100
  • (c)1111 1111 1110 0100
  • (d)1000 0000 1110 0100
GATE 2016 · Set 2 · NATQ20 · page 155 of the book
Let X be the number of distinct 16-bit integers in 2's
complement representation. Let Y be the number of
distinct
16-bit
integers
in
sign
magnitude
representation. Then X - Y is _____
GATE 2016 · Set 1 · NATQ21 · page 155 of the book
The 16-bit 2's complement representation of an
integer is 1111 1111 1111 0101; its decimal
representation is __________.
GATE 2013 · MCQ · 1 markQ25 · page 156 of the book
The smallest integer that can be represented by an
8-bit number in 2's complement form is
  • (a)-256
  • (b)-128
  • (c)-127
  • (d)0
GATE 2010 · MCQ · 1 markQ26 · page 156 of the book
P is a 16-bit signed integer. The 2's complement
representation of P is (F87B)16. The 2's
complement representation of 8 × P is
  • (a)(C3D8)16
  • (b)(187B)16
  • (c)(F878)16
  • (d)(987B)16
GATE 2006 · MCQQ31 · page 156 of the book
We consider the addition of two 2's complement
numbers bn-1 bn-2 ... b0 and an-1 an-2 ... a0. A binary
adder for adding unsigned binary numbers is used to
add the two numbers. The sum is denoted by cn-1 cn-2
... c0 and the carry-out by cout. Which one of the
following options correctly identifies the overflow
condition?
  • (a)cout ( an-1 ⊕bn-1̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅)
  • (b)an-1 bn-1cn-1̅̅̅̅̅̅ + an-1̅̅̅̅̅̅ bn-1̅̅̅̅̅̅ cn-1
  • (c)cout ⊕ cn-1
  • (d)an-1 ⊕ bn-1 ⊕ cn-1
GATE 2005 · MCQ · 1 markQ34 · page 157 of the book
The range of integers that can be represented by an
n-bit 2's complement number system is:
  • (a)-2n-1 to (2n-1-1)
  • (b)-(2n-1 - 1) to (2n-1 - 1)
  • (c)-2n-1 to 2n-1
  • (d)-(2n-1+1) to (2n-1-1)

Self-check

Question 1

How many distinct integers can an 8-bit 1's complement word represent?

Show answer

There are 256 patterns, but 00000000 and 11111111 both mean zero, so there are 255 distinct values (−127-127 to +127+127).

Question 2

What is the 8-bit 2's complement representation of −1-1 and of −128-128? What is −128−1-128 - 1 in 8 bits?

Show answer

−1=1111 1111-1 = 1111\,1111 and −128=1000 0000-128 = 1000\,0000. Adding −1-1 to −128-128 gives 1000 0000+1111 1111=1 0111 11111000\,0000 + 1111\,1111 = 1\,0111\,1111; dropping the carry gives 0111 1111=+1270111\,1111 = +127, which is a signed overflow (the true result −129-129 is below the range).

Question 3

The 8-bit pattern 1000 00111000\,0011 is read in sign-magnitude, 1's complement and 2's complement. What are the three values?

Show answer

Sign-magnitude: −3-3. 1's complement: invert to 0111 1100=1240111\,1100 = 124, so −124-124. 2's complement: 131−256=−125131 - 256 = -125.

Question 4

What is the value of the arithmetic shift right of 1001 01001001\,0100 (8-bit 2's complement) by 2 places?

Show answer

1001 0100=148−256=−1081001\,0100 = 148 - 256 = -108. Shifting right by 2 copies the sign bit twice: 1110 0101=229−256=−271110\,0101 = 229 - 256 = -27, which is −108/4=−27-108 / 4 = -27 exactly.

Revision card

One-screen revision

  • nn bits: 2's complement −2n−1-2^{n-1} to 2n−1−12^{n-1}-1 (one zero); sign-magnitude and 1's complement −(2n−1−1)-(2^{n-1}-1) to 2n−1−12^{n-1}-1 (two zeros).
  • Negate: sign-magnitude flips the sign bit, 1's complement flips all bits, 2's complement flips all bits and adds 1.
  • Sign bit 1 and unsigned value uu means u−2nu - 2^n in 2's complement.
  • −(−2n−1)-(-2^{n-1}) overflows. Sign extension copies the sign bit.
  • Arithmetic shift right rounds down; C division truncates toward zero.
  • Excess-KK: stored = value + KK (IEEE exponents use 127 and 1023).
  • 1's complement adder needs an end-around carry. Radix complement: rn−Nr^n - N.

References

  • ·Mano and Ciletti, Digital Design, 5th ed., Chapter 1 Digital Systems and Binary Numbers (complements, signed binary numbers)
  • ·Hamacher, Vranesic, Zaky, Computer Organization, 5th ed., Chapter 6 Arithmetic (addition and subtraction of signed numbers)
  • ·Bryant and O'Hallaron, Computer Systems: A Programmer's Perspective, 3rd ed., Chapter 2 Representing and Manipulating Information (two's-complement encodings)

Verification

The animation patterns are computed by scripts/gen-dl-traces.py and decoded again, by each system's own rule, in tests/dl-traces.test.ts. All 256 eight-bit patterns, the negation rule, the fast negation rule, sign extension, end-around carry, shifts and excess-K are exhaustively checked in scripts/enum-checks/dl_numbers.py. C programs are compiled and run by npm run verify:c. Textbook chapter titles are from memory and not yet checked against the books; the independent second pass has not been run yet, so the page stays draft. SECOND PASS (2026-11-02, done by the assistant, not a human check): every Self-check question was solved before the printed answer was read and all agreed; the definitions, worked examples, cheat box and traps were read claim by claim and the numbers re-derived by hand or by a small script (conversions, signed ranges and flags, IEEE 754 fields and custom formats, Boolean identities and counts, K-map groups, adder carries, BCD correction, flip-flop conversions, counter sequences and T equations, state reduction, Quine-McCluskey rounds and covers, floating-point alignment and rounding); long C programs were not re-read line by line (they are compiled and compared by npm run verify:c, and their printed outputs were checked against the prose); tables and animations are machine output and were read, not re-run; No other defect found. Status raised to self-checked; verified needs a textbook or human check.

last verified: never