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 bits . The leading bit 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 , range to .
Sign-magnitude. The sign bit gives the sign and the remaining bits give the magnitude. Value . Range to . There are two zeros: and . To negate, flip the sign bit.
1's complement. A negative number is the bitwise complement of the positive one. Value . Range to . There are two zeros: and . 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 . Equivalently, a pattern with sign bit 1 and unsigned value stands for . Range to , which is asymmetric (one more negative number). There is one zero. The pattern is and has no positive partner.
| 8 bits | sign-magnitude | 1's complement | 2's complement |
|---|---|---|---|
| range | to | to | to |
| zeros | 00000000, 10000000 | 00000000, 11111111 | 00000000 |
| 10000101 | 11111010 | 11111011 | |
| negate | flip sign bit | flip all bits | flip all bits, add 1 |
Negation in 2's complement. The negative of in bits is (taken modulo ). Since , and is just 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 , so negating it overflows.
Sign extension. To widen an -bit 2's complement number to 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 (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 is while is on a machine with arithmetic shifts.
Excess- (biased) representation. The stored unsigned value is the true value plus , so the true value is stored minus . For bits the common bias is , 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 digits in base , the 's complement of is (flip every digit to ) and the 's complement is . For these are the 1's and 2's complements; for they are the 9's and 10's complements. Subtraction is done as : 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 '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 typedWorking
+5: sign bit 0, magnitude 101
Value, format and bits (changed bits in amber)
Step 1
+5 = 0000 0101 in every system. The leading bit is 0 for non-negative numbers.
Why 2's complement works: modulo arithmetic is a closed system, and in it and are the same number. A circuit that adds modulo 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
EasyStep 1. (32 + 4 + 1).
Step 2 (sign-magnitude). Flip only the sign bit: .
Step 3 (1's complement). Flip every bit: .
Step 4 (2's complement). Flip every bit and add 1: . Check with the fast rule: from the right copy up to the first 1 (bit 0), then flip the rest: . Check with the formula: .
#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 FFA5The first line confirms the 2's complement pattern: hex DB is .
EX 2Decode 1111 0110 in each system
EasyUnsigned. .
2's complement. The sign bit is 1, so the value is . (Check: invert to , add 1 to get .)
Sign-magnitude. Sign 1, magnitude , so .
1's complement. Sign bit is 1, so invert: , giving . (Also .)
EX 3Ranges and counts
MediumFor 6 bits: 2's complement runs from to ( values); sign-magnitude and 1's complement run from to ( distinct values, because two patterns are both zero). For bits the 2's complement minimum is and the maximum is .
Check with 16 bits. to . The range is not symmetric: the negative of is , which does not fit as a positive 16-bit number.
EX 4Negating the most negative number
MediumIn 8 bits, . Invert: . Add 1: . The result is again. The mathematical answer needs 9 bits. This is why abs(INT_MIN) is not safe and why -x == x can be true for a non-zero . The program in example 1 prints for (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 ).
EX 5Sign extension and truncation
GATE levelExtension. The 8-bit pattern is in 2's complement. Sign-extended to 16 bits it is , which is hexadecimal FFA5 and still (the program in example 1 prints -91 -91 FFA5). Zero-extension would give , which is a different number.
Truncation. The value 255 stored in 8 bits is , which is read as in 2's complement. Similarly truncated to 8 bits is . Truncation keeps only the low bits.
EX 6Right shift versus division
GATE levelFor in 8 bits:
Arithmetic shift right by 1. Copy the sign bit in: . This is .
C division. -5 / 2 truncates toward zero: . (The program in example 1 prints -3 -2.)
Logical shift right would give , which only makes sense for unsigned numbers.
Left shift. . This is exact as long as the result is in range: would need , which does not fit in 8 bits.
EX 7Excess-K and radix complements
GATE levelExcess-127. The stored value stands for . (This is how the exponent field of a float is read.)
Subtraction with 10's complements (5 digits). Compute . The 10's complement of is . Add: . Drop the carry out of the top digit: the result is , and the carry shows it is positive.
The other way round. Compute . The 10's complement of is . Add: . There is no carry out, so the result is negative: minus the 10's complement of , which is .
Cheat box
Cheat box
- -bit ranges. Unsigned: to . Sign-magnitude and 1's complement: to , two zeros. 2's complement: to , one zero.
- 2's complement value: . Pattern with sign bit 1 and unsigned value is .
- Negate in 2's complement: invert and add 1, or copy up to the first 1 from the right and invert the rest. .
- (overflow).
- Sign extension: copy the sign bit. Zero extension is for unsigned.
- Arithmetic right shift is . C's
/truncates toward zero. They differ for negative odd . - Excess-: stored value . Excess- 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).
- 's complement ; 's complement .
Traps and GATE patterns
Connections
Builds on
Unlocks
Across subjects
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
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
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
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
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
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
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
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
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 _____
The 16-bit 2's complement representation of an integer is 1111 1111 1111 0101; its decimal representation is __________.
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
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
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
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 answerHide
There are 256 patterns, but 00000000 and 11111111 both mean zero, so there are 255 distinct values ( to ).
Question 2
What is the 8-bit 2's complement representation of and of ? What is in 8 bits?
Show answerHide
and . Adding to gives ; dropping the carry gives , which is a signed overflow (the true result is below the range).
Question 3
The 8-bit pattern is read in sign-magnitude, 1's complement and 2's complement. What are the three values?
Show answerHide
Sign-magnitude: . 1's complement: invert to , so . 2's complement: .
Question 4
What is the value of the arithmetic shift right of (8-bit 2's complement) by 2 places?
Show answerHide
. Shifting right by 2 copies the sign bit twice: , which is exactly.
Revision card
One-screen revision
- bits: 2's complement to (one zero); sign-magnitude and 1's complement to (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 means in 2's complement.
- overflows. Sign extension copies the sign bit.
- Arithmetic shift right rounds down; C division truncates toward zero.
- Excess-: stored = value + (IEEE exponents use 127 and 1023).
- 1's complement adder needs an end-around carry. Radix complement: .
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
My notes (saved in this browser)