Your bank balance can go negative, a temperature in Leh can be -15 °C, and a game character's velocity can point left. On paper we just write a minus sign. But memory holds only 0s and 1s; there is no place for a "-" symbol. So we must encode the sign using bits.
There are three classic ways to do this: sign-magnitude, 1's complement and 2's complement. All three agree for positive numbers and differ for negative ones. In this chapter we learn each one, find their ranges, see why the same bit pattern can mean three different numbers, and understand why virtually every modern computer uses 2's complement. We also cover sign extension, biased (excess) codes and the general r's and (r-1)'s complements that exams like to ask about.
Unsigned vs Signed
With n bits, an unsigned number uses all bits for magnitude, giving 0 to 2n - 1 (0 to 255 for 8 bits), as we saw in Number Systems & Conversions. A signed representation gives up some of these patterns to represent negative values.
In all three signed systems below:
The most significant bit (MSB) tells the sign: 0 means positive (or zero), 1 means negative.
Positive numbers look exactly like unsigned numbers with an MSB of 0. For 8 bits, +45 is
00101101in every system.
The systems differ only in how they write negative numbers. All examples below use 8 bits unless stated otherwise.
Sign-Magnitude
Sign-magnitude: the MSB is the sign bit (0 = +, 1 = -) and the remaining n - 1 bits hold the magnitude (absolute value) in ordinary binary.
This is exactly how we write numbers on paper: a sign followed by the size.
+45 = 0 0101101
-45 = 1 0101101 (only the sign bit changes)
Range with n bits: the magnitude has n - 1 bits, so it goes up to 2n-1 - 1. The range is -(2n-1 - 1) to +(2n-1 - 1); for 8 bits, -127 to +127.
Problems:
Two zeros:
00000000(+0) and10000000(-0). Comparing with zero needs two checks, and one pattern is wasted.Arithmetic is awkward: to add two numbers the hardware must first compare signs; if they differ, it must compare magnitudes, subtract the smaller from the larger and take the sign of the larger. That needs extra comparison and subtraction circuits.
Sign-magnitude is not used for integers in modern CPUs, but it survives in one important place: the sign of a floating-point number (see Floating-Point Representation (IEEE 754)).
1's Complement
1's complement: a negative number -x is represented by inverting every bit of +x. Mathematically, -x is stored as (2n - 1) - x.
+45 = 00101101
-45 = 11010010 (flip every bit)
Why "(2n - 1) - x"? 2n - 1 is all 1s (11111111 for 8 bits), and subtracting any number from all 1s just flips its bits, with no borrows.
Range: the same as sign-magnitude, -(2n-1 - 1) to +(2n-1 - 1).
Problems:
Still two zeros:
00000000(+0) and11111111(-0).Addition works with an ordinary adder, but a carry out of the MSB must be added back into the LSB (the end-around carry), as we will see in Binary Arithmetic & Overflow. That costs extra time.
1's complement was used in some early machines (such as the CDC 6600 and UNIVAC 1100 series) and still appears in the Internet checksum used by IPv4, TCP and UDP.
2's Complement
2's complement: a negative number -x is represented by inverting every bit of +x and then adding 1. Mathematically, -x is stored as 2n - x.
+45 = 00101101
invert = 11010010 (this is the 1's complement)
add 1 = 11010011 => -45 in 2's complement
Why 2n - x? In n bits, arithmetic naturally wraps around modulo 2n, like a car's odometer rolling from 99999 to 00000. Adding x and (2n - x) gives exactly 2n, which in n bits is a 1 that falls off the end followed by all zeros, i.e. 0. So 2n - x really behaves as "-x".
00101101 (+45)
+ 11010011 (-45)
----------
1 00000000 the carry out is discarded => 0
Shortcut for Taking the 2's Complement
Scan from the right. Copy every bit up to and including the first 1, then invert all remaining bits to the left.
+88 = 0101 1000
^ first 1 from the right: copy "1000", invert "0101" -> "1010"
-88 = 1010 1000
This works because adding 1 to the inverted bits turns the trailing 1s (which were 0s) back into 0s and carries until it reaches the position of the first 1.
The same operation also converts a negative number back to positive: the 2's complement of the 2's complement is the original number. In C, -x on a 2's complement machine is exactly ~x + 1.
The Weighted View (Fastest Way to Read a Value)
In 2's complement, the MSB simply has a negative weight:
Value = -b(n-1) x 2^(n-1) + b(n-2) x 2^(n-2) + ... + b1 x 2 + b0
For 8 bits, the weights are -128, 64, 32, 16, 8, 4, 2, 1. Example: 11010011 = -128 + 64 + 16 + 2 + 1 = -45. No inverting needed.
Range and the Special Case -2n-1
2's complement has only one zero (00000000; the pattern 11111111 is -1). The freed pattern 10000000 represents -128. So the range is -2n-1 to +2n-1 - 1: for 8 bits, -128 to +127. There is one more negative number than positive.
Consequence: -128 has no positive partner in 8 bits. Taking its 2's complement gives 01111111 + 1 = 10000000, itself again. This is a classic source of bugs, for example abs(INT_MIN) in C cannot be represented.
Why 2's Complement Won
One zero, so no wasted pattern and simpler comparisons.
The same adder works for signed and unsigned numbers. The hardware just adds bits modulo 2n; only the interpretation (and the overflow check) differs.
Subtraction is addition: A - B = A + (~B) + 1, so one adder with a carry-in handles both.
No end-around carry: the carry out of the MSB is simply discarded.
For these reasons all mainstream processors use it, and the C23 and C++20 standards now require signed integers to be stored in 2's complement.
All Three Side by Side
8-bit Representations of +45 and -45
Number | Sign-magnitude | 1's complement | 2's complement |
|---|---|---|---|
+45 | 00101101 | 00101101 | 00101101 |
-45 | 10101101 | 11010010 | 11010011 |
-1 | 10000001 | 11111110 | 11111111 |
-127 | 11111111 | 10000000 | 10000001 |
-128 | not representable | not representable | 10000000 |
Every 4-bit Pattern
This table is worth studying slowly; it shows all the differences at once. The last column is the excess-8 (biased) code explained later in this chapter.
Bits | Unsigned | Sign-magnitude | 1's complement | 2's complement | Excess-8 |
|---|---|---|---|---|---|
0000 | 0 | +0 | +0 | 0 | -8 |
0001 | 1 | +1 | +1 | +1 | -7 |
0010 | 2 | +2 | +2 | +2 | -6 |
0011 | 3 | +3 | +3 | +3 | -5 |
0100 | 4 | +4 | +4 | +4 | -4 |
0101 | 5 | +5 | +5 | +5 | -3 |
0110 | 6 | +6 | +6 | +6 | -2 |
0111 | 7 | +7 | +7 | +7 | -1 |
1000 | 8 | -0 | -7 | -8 | 0 |
1001 | 9 | -1 | -6 | -7 | +1 |
1010 | 10 | -2 | -5 | -6 | +2 |
1011 | 11 | -3 | -4 | -5 | +3 |
1100 | 12 | -4 | -3 | -4 | +4 |
1101 | 13 | -5 | -2 | -3 | +5 |
1110 | 14 | -6 | -1 | -2 | +6 |
1111 | 15 | -7 | -0 | -1 | +7 |
Observations: in 2's complement, a negative pattern's unsigned value is exactly 16 more than its signed value (1011 is 11 unsigned and -5 signed, and 11 - 16 = -5). That is the "2n - x" rule seen from the other side.
Ranges with n Bits
Representation | Range | 8-bit range | 16-bit range | Zeros | Distinct values |
|---|---|---|---|---|---|
Unsigned | 0 to 2n - 1 | 0 to 255 | 0 to 65535 | 1 | 2n |
Sign-magnitude | -(2n-1 - 1) to 2n-1 - 1 | -127 to 127 | -32767 to 32767 | 2 | 2n - 1 |
1's complement | -(2n-1 - 1) to 2n-1 - 1 | -127 to 127 | -32767 to 32767 | 2 | 2n - 1 |
2's complement | -2n-1 to 2n-1 - 1 | -128 to 127 | -32768 to 32767 | 1 | 2n |
One Bit Pattern, Different Meanings
A bit pattern has no meaning by itself; the representation decides. Take 10110110:
Unsigned : 128 + 32 + 16 + 4 + 2 = 182
Sign-magnitude : sign 1, magnitude 0110110 = 54 = -54
1's complement : negative; invert -> 01001001 = 73 = -73
2's complement : -128 + 32 + 16 + 4 + 2 (or 182 - 256) = -74
This is exactly what happens in C when you store the byte 0xB6 and read it as uint8_t or int8_t: you get 182 or -74 from the same 8 bits.
Sign Extension
Often a value must move to a wider register, for example an 8-bit byte loaded into a 32-bit register. How do we fill the new bits?
Sign extension: to widen a 2's complement number, copy its sign bit into all the new high-order positions. Zero extension (filling with 0s) is used for unsigned numbers.
-5 in 4 bits : 1011
-5 in 8 bits : 11111011 (copy the 1)
+5 in 4 bits : 0101
+5 in 8 bits : 00000101 (copy the 0)
Why it preserves the value: in the 4-bit number, the MSB has weight -8. In the 8-bit version, the new 1s have weights -128, 64, 32, 16, 8, which sum to -128 + 120 = -8. The negative weight simply moves to the new top bit. Filling with zeros instead would turn -5 into 00001011 = +11, a different number.
Processors have separate instructions for the two cases, for example RISC-V lb (load byte, sign-extend) and lbu (load byte unsigned, zero-extend), or x86 movsx and movzx.
The reverse, truncation (dropping high bits), keeps the value only if all the dropped bits equal the new sign bit. Truncating 11111011 (-5) to 4 bits gives 1011 (-5, fine), but truncating 00010110 (+22) to 4 bits gives 0110 (+6), a wrong value.
Biased (Excess-K) Representation
Excess-K (biased) representation: a value v is stored as the unsigned number v + K. With n bits, the usual bias is K = 2n-1 (or 2n-1 - 1).
In excess-8 (4 bits), -8 is stored as 0000, 0 as 1000 and +7 as 1111 (see the last column of the 4-bit table). Its big advantage: the stored patterns are in the same order as the values, so two numbers can be compared with a plain unsigned comparison. That is why IEEE 754 floating point stores the exponent in biased form (bias 127 for single precision), as you will see in Floating-Point Representation (IEEE 754). Notice also that excess-2n-1 is just 2's complement with the MSB flipped.
r's and (r - 1)'s Complement in Any Base
The complement idea works in any base r. For an n-digit number N:
(r - 1)'s complement (diminished radix complement) = (rn - 1) - N: subtract each digit from r - 1. In binary this is the 1's complement; in decimal it is the 9's complement.
r's complement (radix complement) = rn - N = (r - 1)'s complement + 1 (for N ≠ 0). In binary this is the 2's complement; in decimal it is the 10's complement.
Example. For the 4-digit decimal number 4672: 9's complement = 9999 - 4672 = 5327; 10's complement = 10000 - 4672 = 5328.
Subtraction Using 10's Complement
To compute M - N, add M to the r's complement of N:
(a) 7250 - 3456:
10's complement of 3456 = 6544
7250 + 6544 = 13794
A carry came out of the 4th digit => discard it, result is positive: 3794
(b) 3456 - 7250:
10's complement of 7250 = 2750
3456 + 2750 = 6206
No carry => the result is negative; take the 10's complement: 10000 - 6206 = 3794
Answer: -3794
Binary 2's complement subtraction follows exactly the same rules, and computers use it for every subtraction.
Signed Numbers in C
#include <stdio.h>
#include <stdint.h>
void print_binary(uint32_t x, int bits) {
for (int i = bits - 1; i >= 0; i--)
putchar(((x >> i) & 1) ? '1' : '0');
}
int main(void) {
int8_t a = 45, b = -45;
printf("+45 : "); print_binary((uint8_t)a, 8); printf("\n");
printf("-45 : "); print_binary((uint8_t)b, 8); printf("\n");
/* same 8 bits, read two ways */
uint8_t bits = 0xB6; /* 1011 0110 */
printf("0xB6 as unsigned = %u, as int8_t = %d\n",
(unsigned)bits, (int8_t)bits);
/* ~x + 1 is the two's complement (negation) */
int8_t x = 45;
printf("~x + 1 = %d\n", (int8_t)(~x + 1));
/* sign extension: widening a negative value copies the sign bit */
int8_t small = -5;
int16_t wide = small;
printf("-5 in 8 bits : "); print_binary((uint8_t)small, 8);
printf("\n-5 in 16 bits: "); print_binary((uint16_t)wide, 16);
printf("\n");
/* the asymmetric range */
printf("INT8_MIN = %d, INT8_MAX = %d\n", INT8_MIN, INT8_MAX);
int8_t m = INT8_MIN;
printf("-(-128) stored back in int8_t = %d\n", (int8_t)(-m));
return 0;
}
Output (GCC on x86-64 Linux):
+45 : 00101101
-45 : 11010011
0xB6 as unsigned = 182, as int8_t = -74
~x + 1 = -45
-5 in 8 bits : 11111011
-5 in 16 bits: 1111111111111011
INT8_MIN = -128, INT8_MAX = 127
-(-128) stored back in int8_t = -128
Note: converting an out-of-range value such as 182 to int8_t was "implementation-defined" before C23; GCC documents that it wraps modulo 28, which is what we see. In the last line, -m is computed as the int 128, which does not fit in 8 bits and wraps back to -128.
Solved Problems
Problem 1
The 16-bit 2's complement representation of an integer is 1111 1111 1111 0101. What is its decimal value?
The MSB is 1, so it is negative. Take the 2's complement: invert → 0000 0000 0000 1010, add 1 → 0000 0000 0000 1011 = 11. Answer: -11. (Or: it is 0xFFF5 = 65525 unsigned, and 65525 - 65536 = -11.)
Problem 2
Write -539 in 12-bit 2's complement, in hexadecimal.
539 = 512 + 16 + 8 + 2 + 1 = 0010 0001 1011 = 0x21B
2^12 - 539 = 4096 - 539 = 3557 = 0xDE5
Check with invert + 1: 1101 1110 0100 + 1 = 1101 1110 0101 = 0xDE5
Answer: 0xDE5.
Problem 3
Represent -74 in 8-bit sign-magnitude, 1's complement and 2's complement.
+74 = 0100 1010
Sign-magnitude : 1100 1010
1's complement : 1011 0101
2's complement : 1011 0110
Problem 4
Sign-extend the 8-bit 2's complement number 10110110 to 12 bits and check the value.
Copy the sign bit 1 four times: 1111 1011 0110 = 0xFB6 = 4022 unsigned, and 4022 - 4096 = -74. Same value as before. Correct.
Common Mistakes Students Make
Taking the 2's complement of a positive number "to represent it". Positive numbers are stored as is; complement only negative ones.
Forgetting to fix the width first. -5 is
1011in 4 bits but11111011in 8 bits. Always pad the positive number to n bits before inverting.Saying the 8-bit 2's complement range is -127 to +127. It is -128 to +127.
Zero-extending a signed number. Widening a negative number with zeros changes its value; use sign extension.
Reading a pattern without asking "which representation?"
10110110is 182, -54, -73 or -74 depending on the system.Thinking 1's complement has no -0. Both sign-magnitude and 1's complement have two zeros; only 2's complement has one.
GATE / Interview Questions
1. What is the range of an n-bit 2's complement number, and why is it asymmetric?
-2n-1 to 2n-1 - 1. There is only one zero, which uses a pattern with MSB 0, so of the 2n-1 patterns with MSB 0, one is zero and the rest are positive, while all 2n-1 patterns with MSB 1 are negative.
2. Which 8-bit numbers have the same representation in sign-magnitude, 1's complement and 2's complement?
All non-negative numbers 0 to 127 (MSB 0). They are stored as plain binary in all three systems.
3. What is 0x9C as an 8-bit 2's complement number?
0x9C = 156 unsigned. Since the MSB is 1, the signed value is 156 - 256 = -100.
4. How many distinct values can 8-bit sign-magnitude represent?
255. There are 256 patterns, but +0 and -0 represent the same value.
5. Why do computers prefer 2's complement over 1's complement?
2's complement has a single zero, needs no end-around carry, allows the same adder for signed and unsigned arithmetic, and makes subtraction simply A + ~B + 1.
6. Find the 9's and 10's complements of 0350 (4 digits).
9's complement: 9999 - 0350 = 9649. 10's complement: 9649 + 1 = 9650.
7. What does sign extension of the 6-bit 2's complement number 101001 to 8 bits give, and what is its value?
11101001. Value: -128 + 64 + 32 + 8 + 1 = -23, the same as the 6-bit value -32 + 8 + 1 = -23.
Summary
All three signed systems use MSB 0 for non-negative numbers and store positive values as plain binary.
Sign-magnitude: sign bit + magnitude. Two zeros, range ±(2n-1 - 1), awkward arithmetic.
1's complement: invert all bits, i.e. (2n - 1) - x. Two zeros, same range, needs end-around carry.
2's complement: invert and add 1, i.e. 2n - x. One zero, range -2n-1 to 2n-1 - 1, MSB has weight -2n-1. Used by all modern CPUs.
Sign extension copies the sign bit when widening; unsigned values are zero-extended.
Excess-K stores v + K and keeps patterns in numeric order (used for floating-point exponents).
r's complement = rn - N; (r - 1)'s complement = rn - 1 - N; subtraction M - N = M + r's complement of N.
Now that we can represent negative numbers, the next chapter, Binary Arithmetic & Overflow, shows how hardware adds, subtracts, multiplies and shifts them, and how it detects when a result does not fit.