A computer stores a negative integer in a fixed number of bits by giving the leftmost bit a sign. This lesson defines the three ways of doing it, sign and magnitude, ones’ complement and two’s complement, and proves the properties of two’s complement, the usual choice: its range from −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1, negation by inverting and adding 1, one adder for signed and unsigned numbers, and widening by sign extension.
Signs in a fixed number of bits
The unsigned integers of lesson 2.1 stop at 0. The integers of mathematics do not (Integers on the number line): every positive integer has an opposite on the other side of 0 (Integers: opposites), and its distance from 0 is its absolute value |x| (Integers: absolute value). A signed integer is one that can be negative or positive; the word “signed” refers to its sign, + or −.[1]
Storing the sign costs something. n bits have 2ⁿ patterns and can tell apart at most 2ⁿ values (lesson 1.5, Proposition 1), so if some patterns are spent on negative numbers, fewer are left for the others. Designers tried several ways of sharing out the patterns before one became the usual choice.[2] All three ways in use give the leftmost bit the same job, the sign bit: 0 for a positive number and 1 for a negative one.[3] They differ in how the other n − 1 bits are read. Throughout, n ≥ 2 is the width.
Sign and magnitude
The most direct way writes the sign and then the size of the number. The most significant bit, the leftmost, is the sign bit, and the rest of the number is treated with a method called sign-magnitude.[4] The C standard calls it sign and magnitude.[5]
Let |x| ≤ 2ⁿ⁻¹ − 1. The n-bit sign-and-magnitude pattern of x is the sign bit, 0 if x ≥ 0 and 1 if x < 0, followed by the (n − 1)-bit pattern of |x|. A pattern with sign bit s and remaining bits of value r stores r if s = 0 and −r if s = 1.
The largest magnitude that n − 1 bits can hold is 2ⁿ⁻¹ − 1, so the range is −(2ⁿ⁻¹ − 1) to 2ⁿ⁻¹ − 1. Zero has two patterns: 00…0 and 10…0, “+0” and “−0”, a waste of one pattern.[6] So the 2ⁿ patterns store only 2ⁿ − 1 different integers.
Write −37 as an 8-bit sign-and-magnitude pattern.
- The sign bit is 1, because −37 < 0.
- |−37| = 37 = 32 + 4 + 1, which is 0100101 in 7 bits.
Answer: 10100101. Reading it back: sign 1, magnitude 0100101 = 37, so −37.
In 4 bits, 5 = 0101 and −3 = 1011 in sign and magnitude. Add the patterns in columns as unsigned integers (lesson 2.1) and keep 4 bits. Is the result 2?
- 0101 + 1011 = 5 + 11 = 16 = 10000 as unsigned integers.
- Keeping 4 bits leaves 0000, which stores 0.
Answer: no; the result is 0, not 2. Sign and magnitude needs its own rules for addition: when the signs differ, the magnitudes must be compared and subtracted. The column adder of lesson 2.1 cannot do it alone.
Ones’ complement
The second way stores a negative number by inverting the bits of its size. Inverting has a simple effect on the value.
Let P be an n-bit pattern whose unsigned value is u. The pattern obtained by changing every 1 of P to 0 and every 0 to 1 has the unsigned value (2ⁿ − 1) − u.
Add P and the inverted pattern in columns. Each column holds one 1 and one 0, so it writes 1 and never carries. The sum is n 1s, which names 2ⁿ − 1 (Number Bases lesson 1.2). So the two values add up to 2ⁿ − 1, and the inverted pattern is worth (2ⁿ − 1) − u.
Let |x| ≤ 2ⁿ⁻¹ − 1. The n-bit ones’ complement pattern of x is the n-bit pattern of x if x ≥ 0. If x < 0, it is the n-bit pattern of |x| with every bit inverted.[7]
Since |x| ≤ 2ⁿ⁻¹ − 1, the pattern of |x| begins with 0, so a negative x gets the sign bit 1, as it should. Some textbooks spell the name “one’s complement”;[7]
The range is again −(2ⁿ⁻¹ − 1) to 2ⁿ⁻¹ − 1, and 0 again has two patterns, 00…0 and 11…1, the second inverting the first. The positive and negative values now mirror each other, but the two patterns of 0 remain.[9]
Write −37 as an 8-bit ones’ complement pattern, and check it with the weight of the sign bit.
- The 8-bit pattern of 37 is 00100101.
- Invert every bit: 11011010.
- Check: the sign bit is worth −(2⁷ − 1) = −127, so the value is −127 + 64 + 16 + 8 + 2 = −127 + 90 = −37.
Answer: 11011010.
In 4 bits, 5 = 0101 and −3 = 1100 in ones’ complement. Add them as unsigned integers and keep 4 bits.
- 0101 + 1100 = 5 + 12 = 17 = 10001.
- Keeping 4 bits leaves 0001, which stores 1.
Answer: 1, not 2: off by exactly one. The reason is the Lemma: −3 is stored as 15 − 3 = 12, so the unsigned sum is 5 − 3 + 15. When it produces a carry out, dropping the carry removes 16 where only 15 had been added, and the kept bits come out one too small.
Two’s complement
The third way changes the weight of the leftmost bit and nothing else. In the C standard C23, if the sign bit of a signed integer type of width N is one, it has the value −2ᴺ⁻¹;[10] Rosen’s textbook gives the same formula for the value of a two’s complement pattern.[11]
In n-bit two’s complement, a pattern b₍ₙ₋₁₎b₍ₙ₋₂₎ … b₁b₀ stores the integer
−b₍ₙ₋₁₎ × 2ⁿ⁻¹ + b₍ₙ₋₂₎ × 2ⁿ⁻² + ⋯ + b₁ × 2 + b₀.
The leftmost bit is worth −2ⁿ⁻¹ instead of +2ⁿ⁻¹; every other bit has its unsigned place value.
- The n-bit patterns store exactly the integers from −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1, each by exactly one pattern. In particular 0 has one pattern.
- A pattern with sign bit 0 stores its unsigned value u. A pattern with sign bit 1 stores u − 2ⁿ.
- So for x ≥ 0 the pattern of x is its ordinary n-bit pattern, and for x < 0 it is the n-bit pattern of 2ⁿ + x = 2ⁿ − |x|. The sign bit is 1 exactly for the negative integers.
Let r be the value of the n − 1 bits after the sign bit. By the theorem “what k bits can name” with k = n − 1, r runs through 0, 1, …, 2ⁿ⁻¹ − 1, each value once. The patterns with sign bit 0 store r: the integers 0 to 2ⁿ⁻¹ − 1. The patterns with sign bit 1 store −2ⁿ⁻¹ + r: the integers −2ⁿ⁻¹ to −1. Together these are all the integers from −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1, each once, which proves (1).
(2) With sign bit 1, the unsigned value is u = 2ⁿ⁻¹ + r and the stored value is −2ⁿ⁻¹ + r = u − 2 × 2ⁿ⁻¹ = u − 2ⁿ. (3) follows: x < 0 is stored by the pattern with u − 2ⁿ = x, that is u = 2ⁿ + x, and since 2ⁿ + x lies between 2ⁿ⁻¹ and 2ⁿ − 1 its leftmost bit is 1 (Proposition 1 of lesson 1.3).
The sources give the same range: Rosen represents the integers x with −2ⁿ⁻¹ ≤ x ≤ 2ⁿ⁻¹ − 1 in n bits,[12] and OpenStax notes that this range contains 2ⁿ numbers, as many as the n-bit unsigned integers.[13] For 8 bits it is −128 to 127, and for 16 bits −32,768 to 32,767, as in Intel’s processors, whose sign bit is set for negative integers and cleared for positive integers and zero;[14] the Java language gives its 8-bit type the same range.[15] Rosen describes the pattern of a negative x as the sign bit 1 followed by the bits of 2ⁿ⁻¹ − |x|.[16] That is Theorem 1 (3) again: the n − 1 bits after the sign bit of 2ⁿ − |x| name 2ⁿ − |x| − 2ⁿ⁻¹ = 2ⁿ⁻¹ − |x|.
| Pattern | Unsigned | Sign and magnitude | Ones’ complement | Two’s complement |
|---|---|---|---|---|
| 0000 | 0 | 0 | 0 | 0 |
| 0001 | 1 | 1 | 1 | 1 |
| 0010 | 2 | 2 | 2 | 2 |
| 0011 | 3 | 3 | 3 | 3 |
| 0100 | 4 | 4 | 4 | 4 |
| 0101 | 5 | 5 | 5 | 5 |
| 0110 | 6 | 6 | 6 | 6 |
| 0111 | 7 | 7 | 7 | 7 |
| 1000 | 8 | −0 | −7 | −8 |
| 1001 | 9 | −1 | −6 | −7 |
| 1010 | 10 | −2 | −5 | −6 |
| 1011 | 11 | −3 | −4 | −5 |
| 1100 | 12 | −4 | −3 | −4 |
| 1101 | 13 | −5 | −2 | −3 |
| 1110 | 14 | −6 | −1 | −2 |
| 1111 | 15 | −7 | −0 | −1 |
The range is not symmetric: there are 2ⁿ⁻¹ negative values but only 2ⁿ⁻¹ − 1 positive ones, because 0 uses one of the patterns with sign bit 0. The smallest value, −2ⁿ⁻¹, has no positive partner in the range.
Which integer does the 8-bit two’s-complement pattern 11011011 store?
- With the weights: −128 + 64 + 16 + 8 + 2 + 1 = −37.
- Through the unsigned value: 11011011 = 219, and the sign bit is 1, so the value is 219 − 256 = −37.
Answer: −37, by both routes.
Write −100 as an 8-bit two’s-complement pattern.
- By Theorem 1 (3), the pattern is the 8-bit pattern of 256 − 100 = 156.
- 156 = 128 + 16 + 8 + 4 = 10011100.
Answer: 10011100. Check with the weights: −128 + 16 + 8 + 4 = −100.
Which 8-bit two’s-complement patterns store the largest value, the smallest value, −1 and 0?
- The largest value is 2⁷ − 1 = 127 = 01111111: sign bit 0 and all other bits 1.
- The smallest is −2⁷ = −128 = 10000000: only the sign bit, worth −128.
- −1 is the pattern of 256 − 1 = 255 = 11111111: −128 + 127 = −1.
- 0 is 00000000, and no other pattern stores 0.
Answer: 01111111, 10000000, 11111111 and 00000000. The patterns of 127 and −128 are neighbors on the circle, at the seam where the values jump.
Someone reads the 8-bit two’s-complement pattern 10000101 as “negative, size 5”, that is −5. What does it store?
- The sign bit is worth −128 and the 1s on the right 4 + 1 = 5, so the value is −128 + 5 = −123.
- −5 is the pattern of 256 − 5 = 251 = 11111011.
Answer: −123. In two’s complement the bits after the sign bit are not the size of the number. Only for sign and magnitude would 10000101 be −5.
Negating: invert and add 1
The word “two’s complement” also names an operation on patterns: the two’s complement of a binary number is its ones’ complement, every bit inverted, with 1 added to the result.[18] In the Java language, for every integer value x, −x equals the inverted bits of x plus 1.[19] The operation turns the pattern of x into the pattern of −x, with one exception.
Let P be the n-bit two’s-complement pattern of x. Invert every bit of P, add 1 in columns, and keep n bits (dropping a carry out of the leftmost column). If x ≠ −2ⁿ⁻¹, the result is the pattern of −x. If x = −2ⁿ⁻¹, the result is P itself, and −x = 2ⁿ⁻¹ has no n-bit pattern.
Let u be the unsigned value of P. By the Lemma, inverting gives (2ⁿ − 1) − u, and adding 1 gives 2ⁿ − u. Four cases, by Theorem 1:
- x = 0: then u = 0 and 2ⁿ − u = 2ⁿ, which is 1 followed by n zeros. Keeping n bits leaves 00…0, the pattern of 0 = −0.
- 0 < x ≤ 2ⁿ⁻¹ − 1: then u = x, and 2ⁿ − x lies between 2ⁿ⁻¹ + 1 and 2ⁿ − 1, so it has n bits. By Theorem 1 (3) it is the pattern of the negative number −x.
- −2ⁿ⁻¹ < x < 0: then u = 2ⁿ + x, and 2ⁿ − u = −x, which lies between 1 and 2ⁿ⁻¹ − 1: the ordinary pattern of the positive number −x.
- x = −2ⁿ⁻¹: then u = 2ⁿ⁻¹ and 2ⁿ − u = 2ⁿ⁻¹ = u, the same pattern 10…0. And −x = 2ⁿ⁻¹ is larger than 2ⁿ⁻¹ − 1, outside the range.
The exception is real, not a flaw of the proof: the Java specification notes that, because the range of two’s-complement values is not symmetric, negating the most negative value of its 32-bit and 64-bit types gives that same value;[20] and the C standard notes that the absolute value of the most negative number is not representable.[21]
Negate the 8-bit pattern of 37, then negate the result.
- 37 = 00100101. Invert: 11011010. Add 1: 11011011, which stores −37 (the figure above).
- Negate 11011011: invert to 00100100, add 1: 00100101 = 37.
Answer: 11011011, and back to 00100101. Negating twice gives the number back, as −(−x) = x for integers.
Apply “invert and add 1” to the 8-bit patterns 00000000 and 10000000.
- 00000000 inverts to 11111111; adding 1 gives 100000000, nine bits. Keeping 8 bits leaves 00000000: −0 = 0, as it should be.
- 10000000 (−128) inverts to 01111111; adding 1 gives 10000000 again. +128 is outside the range −128 to 127.
Answer: 00000000 and 10000000; each is its own result, and only the first is a true negation.
Someone negates 37 by inverting its bits only, and stores 11011010 as −37 in two’s complement. What is stored?
- 11011010 = 218 as unsigned, and its sign bit is 1, so it stores 218 − 256 = −38.
- By the Lemma, inverting alone gives 255 − 37 = 218, which is 256 − 38: the pattern of −38, one too far.
Answer: −38. Inverting alone is the ones’ complement, which is off by one in two’s complement; the added 1 corrects it.
Set the bits
- Round
- 1
- Score
- 0
- Streak
- 0
The 4-bit pattern 1011 stores a number x in two’s complement. Set the pattern of −x, or say that no pattern can store it.
Two’s complement: the leftmost bit is worth −8; the other bits as in an unsigned integer.
the pattern of x
your pattern of −x
Above the bits: what each bit is worth.
One adder for signed and unsigned
The great practical advantage of two’s complement is that it needs no new arithmetic. Adding the patterns as unsigned integers, as in lesson 2.1, and keeping n bits gives the right result, whatever the signs.
Let P and Q be the n-bit two’s-complement patterns of x and y, and suppose that x + y also lies in the range −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1. Add P and Q in columns as unsigned integers and keep the n bits written in the columns. The result is the two’s-complement pattern of x + y.
Let u and v be the unsigned values of P and Q, w the value of the n kept bits, and t the unsigned value of the pattern of x + y. By Theorem 1, u is x or x + 2ⁿ, v is y or y + 2ⁿ, and t is x + y or x + y + 2ⁿ. By Theorem 4 of lesson 2.1, w is u + v or u + v − 2ⁿ. So w and t each equal x + y plus an integer times 2ⁿ (the integer being −1, 0, 1 or 2), and therefore w − t = k × 2ⁿ for some integer k.
But w and t both lie between 0 and 2ⁿ − 1, so w − t lies between −(2ⁿ − 1) and 2ⁿ − 1. Then k = 0, because k ≥ 1 would make w − t at least 2ⁿ and k ≤ −1 at most −2ⁿ. So w = t: the kept bits are the pattern of x + y.
The adder does not need to know whether its patterns are signed. Intel’s processors, for example, have addition and subtraction instructions that operate on either unsigned or signed integer operands.[22] Subtraction needs no circuit of its own either: x − y = x + (−y) (Integers: subtraction as adding the opposite), and the pattern of −y is obtained by inverting and adding 1 (Theorem 2), so A − B can be computed as A plus the two’s complement of B.[23] This works even for y = −2ⁿ⁻¹, whose negation has no pattern, whenever x − y itself is in the range (Exercise 8). The sources sum up the advantages: one pattern of 0;[17] the same hardware for addition and subtraction, and for signed and unsigned integers;[24] and addition and subtraction of positive and negative integers that are easy to perform, which is why computers often use two’s complement, and more commonly than ones’ complement.[25]
Compute 5 + (−3) in 4 bits by adding the patterns as unsigned integers, in each of the three ways.
- Sign and magnitude: 0101 + 1011 = 10000, kept 0000 = 0. Wrong.
- Ones’ complement: 0101 + 1100 = 10001, kept 0001 = 1. Wrong by one.
- Two’s complement: −3 is the pattern of 16 − 3 = 13 = 1101, and 0101 + 1101 = 10010; kept 0010 = 2. Right.
Answer: only two’s complement gives 2. The carry out, worth 16, is exactly the 2ⁿ that Theorem 1 added to −3.
Compute 20 − 45 in 8-bit two’s complement as 20 + (−45).
- 45 = 00101101. Invert and add 1: 11010010 + 1 = 11010011, the pattern of −45 (unsigned 211 = 256 − 45).
- 00010100 + 11010011 = 20 + 211 = 231 = 11100111, with no carry out.
- 11100111 has sign bit 1, so it stores 231 − 256 = −25.
Answer: 11100111 = −25, and indeed 20 − 45 = −25.
Compute −70 + (−50) in 8-bit two’s complement.
- −70 is the pattern of 256 − 70 = 186 = 10111010, and −50 the pattern of 206 = 11001110.
- 186 + 206 = 392 = 110001000: the carry out is 1, and the kept bits are 10001000 = 136.
- 136 has sign bit 1, so it stores 136 − 256 = −120.
Answer: −120. The carry out is not an error here: it is the extra 2ⁿ that the proof of Theorem 3 removes.
Theorem 3 assumes that x + y is in the range. When it is not, as in 100 + 50 = 150 in 8 bits, the kept bits 10010110 store −106 instead. Such results are the subject of the next lesson, “Overflow”.
The C programming language allowed all three ways: its standard of 2011, C11, left it to each implementation to store signed integers in sign and magnitude, two’s complement or ones’ complement.[5] Intel’s manuals state that all operations on signed integers assume two’s complement,[27] and the Java language defines its signed integer types as two’s-complement integers.[28] The C standard C23, which ISO published in 2024, defines only two’s complement, and notes that the earlier editions, that of 2018 and those before it, also allowed other sign representations.[29]
Sign extension
A two’s-complement number often has to be moved into a wider field. Zero extension (lesson 2.1) keeps an unsigned value, but it changes a negative two’s-complement value. The rule for two’s complement is to copy the sign bit.
To sign-extend an n-bit pattern to m > n bits is to write m − n copies of its sign bit on its left. The Java language, for example, widens its signed integer types in exactly this way.[31]
Sign-extending an n-bit two’s-complement pattern to m bits gives the m-bit two’s-complement pattern of the same integer.
If the sign bit is 0, the new bits are 0s on the left, which change no value (lesson 1.3). If the sign bit is 1, the n-bit pattern stores −2ⁿ⁻¹ + r, where r is the value of its other n − 1 bits. The m-bit pattern has 1s in positions m − 1 down to n − 1 and the same bits below. Its value is −2ᵐ⁻¹ + (2ᵐ⁻² + ⋯ + 2ⁿ⁻¹) + r. The sum in parentheses is (1 + 2 + ⋯ + 2ᵐ⁻²) − (1 + 2 + ⋯ + 2ⁿ⁻²) = (2ᵐ⁻¹ − 1) − (2ⁿ⁻¹ − 1) = 2ᵐ⁻¹ − 2ⁿ⁻¹ (Number Bases lesson 1.2, Exercise 6). So the value is −2ᵐ⁻¹ + 2ᵐ⁻¹ − 2ⁿ⁻¹ + r = −2ⁿ⁻¹ + r, the same as before.
Widen the 4-bit two’s-complement pattern 1011 to 8 bits, correctly and with zeros.
- 1011 stores −8 + 2 + 1 = −5.
- Sign extension: 11111011 = −128 + 64 + 32 + 16 + 8 + 2 + 1 = −5.
- Zero extension: 00001011 = 8 + 2 + 1 = 11.
Answer: 11111011, still −5. Zero extension gives 11, a different number: it treats the sign bit as a place worth +8.
Widen the 8-bit two’s-complement pattern 0x9C to 16 bits.
- 0x9C = 1001 1100: the sign bit is 1, and the value is 156 − 256 = −100.
- Eight copies of 1 make the new high byte 1111 1111 = 0xFF, so the pattern is 0xFF9C.
- Check: 0xFF9C = 65,436 as unsigned, and 65,436 − 65,536 = −100.
Answer: 0xFF9C. A negative number’s high bytes become 0xFF; a positive number’s become 0x00.
The opposite move, narrowing a number into fewer bits, keeps its low bits (lesson 2.1, Theorem 3), and it keeps the value only when the value fits in the narrower range. What happens when it does not is part of the next lesson, “Overflow”.
Exercises
Write −13 as a 6-bit pattern in (a) sign and magnitude, (b) ones’ complement and (c) two’s complement.
▸ Show solution
13 = 001101 in 6 bits.
(a) Sign and magnitude: sign 1 and 13 = 01101 in 5 bits: 101101.
(b) Ones’ complement: invert 001101: 110010.
(c) Two’s complement: the pattern of 64 − 13 = 51 = 110011. Check: −32 + 16 + 2 + 1 = −13.
Which integers do these 8-bit two’s-complement patterns store: (a) 10000001, (b) 01111110, (c) 11110000, (d) 10101010?
▸ Show solution
(a) 10000001 = −128 + 1 = −127. (b) 01111110 = 126.
(c) 11110000 = −128 + 64 + 32 + 16 = −16. (d) 10101010 = 170 as unsigned, so 170 − 256 = −86.
What is the range of 16-bit two’s complement? How many of its values are negative, how many positive?
▸ Show solution
−2¹⁵ = −32,768 to 2¹⁵ − 1 = 32,767. The negative values are −32,768 to −1, 32,768 of them; the positive values 1 to 32,767, 32,767 of them; and 0. In all 32,768 + 32,767 + 1 = 65,536 = 2¹⁶ values, one per pattern.
Prove the shortcut for negating a two’s-complement pattern: copy the bits from the right up to and including the first 1, and invert all the bits to the left of it. Use it to negate 01101000.
▸ Show solution
Let P end in a 1 followed by k zeros (k ≥ 0), with the bits A before it: P = A 1 0…0. Inverting gives Ā 0 1…1, with k 1s at the end. Adding 1 turns these k 1s into 0s, carrying all the way to the 0, which becomes 1 and stops the carry: Ā 1 0…0. So the last k + 1 bits of P are copied, and every bit before them is inverted.
For 01101000 (104): copy 1000, invert 0110 to 1001: 10011000 = −128 + 16 + 8 = −104. If P is all 0s, it has no 1, and the result is all 0s.
Which n-bit patterns are unchanged by “invert and add 1”? Prove that there are exactly two.
▸ Show solution
By Theorem 2, invert-and-add-1 returns P for x = 0 and x = −2ⁿ⁻¹, and for every other x it returns the pattern of −x ≠ x (since −x = x only for x = 0, Integers: opposites). So exactly two patterns are their own negation: 00…0 and 10…0.
Widen the 8-bit two’s-complement pattern 0xE5 to 16 bits. Give its value before and after, and what zero extension would give.
▸ Show solution
0xE5 = 1110 0101, sign bit 1, value 229 − 256 = −27. Sign-extended to 16 bits: 0xFFE5, which is 65,509 − 65,536 = −27. Zero-extended: 0x00E5 = 229.
Show by an example that comparing two two’s-complement patterns as unsigned integers can give the wrong answer, and state and prove a correct rule.
▸ Show solution
As unsigned integers 11111111 = 255 is larger than 00000001 = 1, but in two’s complement they store −1 and 1. Comparing as unsigned integers puts every negative number above every nonnegative one, because the sign bit has weight +2ⁿ⁻¹ there and −2ⁿ⁻¹ here.
Rule: if the sign bits differ, the pattern with sign bit 0 stores the larger number. If they agree, compare as unsigned integers (lesson 2.1, Proposition 2): when both are 0, the values are the unsigned values; when both are 1, each value is its unsigned value minus the same 2ⁿ (Theorem 1), which keeps the order.
Let x and y be n-bit two’s-complement integers with patterns P and Q, and suppose that x − y is in the range. Prove that inverting Q, adding 1, adding P and keeping n bits gives the pattern of x − y, even when y = −2ⁿ⁻¹. Check it with −1 − (−128) in 8 bits.
▸ Show solution
Let u and v be the unsigned values of P and Q. Inverting Q gives (2ⁿ − 1) − v (the Lemma), and adding 1 gives 2ⁿ − v; keeping n bits leaves q = 2ⁿ − v if v ≥ 1 and q = 0 if v = 0. Adding P and keeping n bits leaves w = u + q or u + q − 2ⁿ (lesson 2.1, Theorem 4). By Theorem 1, u is x or x + 2ⁿ and v is y or y + 2ⁿ, so w equals x − y plus an integer times 2ⁿ. So does t, the unsigned value of the pattern of x − y, which is x − y or x − y + 2ⁿ. As in the proof of Theorem 3, w and t both lie from 0 to 2ⁿ − 1, so they differ by less than 2ⁿ, and therefore w = t.
Check with −1 − (−128) = 127 in 8 bits: inverting 10000000 gives 01111111, and adding 1 gives 10000000 again. Then 11111111 + 10000000 = 101111111, and the kept bits 01111111 store 127, as they should.
Summary
- Signed integers in n bits use the leftmost bit as the sign bit, 1 for negative. Sign and magnitude follows it with |x|; ones’ complement inverts every bit of |x|. Both store −(2ⁿ⁻¹ − 1) to 2ⁿ⁻¹ − 1 and have two patterns of 0.
- In two’s complement the leftmost bit is worth −2ⁿ⁻¹. It stores −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1, each by one pattern; a negative x is the unsigned pattern of 2ⁿ + x. The range has one more negative value than positive ones.
- Inverting every bit and adding 1 negates a two’s-complement number, except −2ⁿ⁻¹, whose negation does not fit.
- Adding two’s-complement patterns as unsigned integers and keeping n bits gives the right sum whenever the sum is in range; subtraction is adding the inverted bits plus 1. One adder serves signed and unsigned numbers, which is why computers use two’s complement.
- Sign extension, copying the sign bit into the new positions, widens a two’s-complement number without changing it; zero extension does not.
Sources
- 1.Jean-Claude Franchitti and others, Introduction to Computer Science (web PDF of April 20, 2026, OpenStax, Rice University, 2024).Chapter 5, “Hardware Realizations of Algorithms: Computer Systems Design”, section 5.3, “Machine-Level Information Representation” (“Signed Integer Numbers Representation”), p. 206 (the whole topic: pp. 206–207).
“A signed integer is an integer that can be negative or positive. The word signed means that they have a sign of + or –.”
Used for: a signed integer can be negative or positive; “signed” refers to its sign, + or −. - 2.Jean-Claude Franchitti and others, Introduction to Computer Science (web PDF of April 20, 2026, OpenStax, Rice University, 2024).Chapter 5, “Hardware Realizations of Algorithms: Computer Systems Design”, section 5.3, “Machine-Level Information Representation” (“Signed Integer Numbers Representation”), p. 206 (the whole topic: pp. 206–207).
“To present negative numbers, designers experimented with several options before picking the de facto choice.”
Used for: designers tried several ways of storing negative numbers before one became the usual choice. - 3.Kenneth H. Rosen, Discrete Mathematics and Its Applications (8th ed., McGraw-Hill, 2019).Chapter 4, “Number Theory and Cryptography”, section 4.2, “Integer Representations and Algorithms” (Exercises, the text before Exercises 40 and 46), p. 270 (the whole topic: pp. 270).
“The leftmost bit is used to represent the sign. A 0 bit in this position is used for positive integers, and a 1 bit in this position is used for negative integers.”
Used for: the leftmost bit is the sign bit, 0 for a positive number and 1 for a negative one (said of ones’ complement and, “just as in one’s complement expansions”, of two’s complement). - 4.Jean-Claude Franchitti and others, Introduction to Computer Science (web PDF of April 20, 2026, OpenStax, Rice University, 2024).Chapter 5, “Hardware Realizations of Algorithms: Computer Systems Design”, section 5.3, “Machine-Level Information Representation” (“Signed Integer Numbers Representation”), p. 206 (the whole topic: pp. 206–207).
“The obvious option is to use the most significant bit (i.e., the leftmost bit) as a sign bit where 0 means positive and 1 means negative. The rest of the number is treated with a method called sign-magnitude.”
Used for: sign and magnitude: the leftmost bit is the sign bit and the rest of the number is its size, a method called sign-magnitude. - 5.ISO/IEC JTC 1/SC 22/WG 14, “N1570: committee draft of ISO/IEC 9899:2011 (C11), the C programming language standard. A free draft that the C committee lists as similar to the published standard, not the ISO text itself”, open-std.org, the site of the ISO C standards committee (12 April 2011).Section “6.2.6.2, “Integer types”, paragraph 2 (p. 46)”. open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf · open at the quoted passage.
“the corresponding value with sign bit 0 is negated (sign and magnitude); — the sign bit has the value −(2ᴹ) (two’s complement); — the sign bit has the value −(2ᴹ − 1) (ones’ complement). Which of these applies is implementation-defined”
Used for: the C standard of 2011 (M is the number of value bits, n − 1) names the three ways sign and magnitude, two’s complement and ones’ complement, spells the last with “ones’ ”, gives the sign bit of ones’ complement the value −(2ⁿ⁻¹ − 1), and lets each implementation choose which way applies. - 6.Jean-Claude Franchitti and others, Introduction to Computer Science (web PDF of April 20, 2026, OpenStax, Rice University, 2024).Chapter 5, “Hardware Realizations of Algorithms: Computer Systems Design”, section 5.3, “Machine-Level Information Representation” (“Signed Integer Numbers Representation”), p. 206 (the whole topic: pp. 206–207).
“First, there are two presentations of 0 (+0 and –0), which is a waste of data.”
Used for: in sign and magnitude 0 has two patterns, +0 and −0, a waste of a pattern. - 7.Kenneth H. Rosen, Discrete Mathematics and Its Applications (8th ed., McGraw-Hill, 2019).Chapter 4, “Number Theory and Cryptography”, section 4.2, “Integer Representations and Algorithms” (Exercises, the text before Exercises 40 and 46), p. 270 (the whole topic: pp. 270).
“For negative integers, the remaining bits are obtained by first finding the binary expansion of the absolute value of the integer, and then taking the complement of each of these bits, where the complement of a 1 is a 0 and the complement of a 0 is a 1.”
Used for: the definition of ones’ complement (Rosen’s spelling “one’s complement”): a negative number is stored by inverting the bits of its absolute value. - 8.Jean-Claude Franchitti and others, Introduction to Computer Science (web PDF of April 20, 2026, OpenStax, Rice University, 2024).Chapter 5, “Hardware Realizations of Algorithms: Computer Systems Design”, section 5.3, “Machine-Level Information Representation” (“Signed Integer Numbers Representation”), p. 206 (the whole topic: pp. 206–207).
“It is called the one’s complement method of a binary number, which is obtained by flipping each 1 in the original number to 0 and each 0 to 1.”
Used for: the ones’ complement of a pattern flips every 1 to 0 and every 0 to 1. - 9.Jean-Claude Franchitti and others, Introduction to Computer Science (web PDF of April 20, 2026, OpenStax, Rice University, 2024).Chapter 5, “Hardware Realizations of Algorithms: Computer Systems Design”, section 5.3, “Machine-Level Information Representation” (“Signed Integer Numbers Representation”), p. 207 (the whole topic: pp. 206–207).
“Is the one’s complement a good method? There is a mirroring effect around the +3/–3. But we still have the two presentations of 0.”
Used for: in ones’ complement the positive and negative values mirror each other, but 0 still has two patterns. - 10.ISO/IEC JTC 1/SC 22/WG 14, “N3220: working draft of ISO/IEC 9899:2024 (C23), the C programming language standard. A free draft that the C committee lists as similar to the published standard, not the ISO text itself”, open-std.org, the site of the ISO C standards committee (22 February 2024).Section “6.2.6.2, “Integer types”, paragraph 2 (p. 43)”. open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf · open at the quoted passage.
“If the sign bit is one, it has value −(2ᴺ⁻¹).”
Used for: in the C standard C23, the sign bit of a signed integer type of width N, when it is one, has the value −2ᴺ⁻¹: the definition of two’s complement. - 11.Kenneth H. Rosen, Discrete Mathematics and Its Applications (8th ed., McGraw-Hill, 2019).Chapter 4, “Number Theory and Cryptography”, section 4.2, “Integer Representations and Algorithms” (Exercise 51), p. 270 (the whole topic: pp. 270).
“Show that the integer m with two’s complement representation (aₙ₋₁aₙ₋₂ … a₁a₀) can be found using the equation m = −aₙ₋₁ ⋅ 2ⁿ⁻¹ + aₙ₋₂2ⁿ⁻² + ⋯ + a₁ ⋅ 2 + a₀.”
Used for: the value of an n-bit two’s complement pattern is −b₍ₙ₋₁₎ × 2ⁿ⁻¹ + b₍ₙ₋₂₎ × 2ⁿ⁻² + ⋯ + b₁ × 2 + b₀. - 12.Kenneth H. Rosen, Discrete Mathematics and Its Applications (8th ed., McGraw-Hill, 2019).Chapter 4, “Number Theory and Cryptography”, section 4.2, “Integer Representations and Algorithms” (Exercises, the text before Exercises 40 and 46), p. 270 (the whole topic: pp. 270).
“To represent an integer x with −2ⁿ⁻¹ ≤ x ≤ 2ⁿ⁻¹ − 1 for a specified positive integer n, a total of n bits is used.”
Used for: n-bit two’s complement represents the integers x with −2ⁿ⁻¹ ≤ x ≤ 2ⁿ⁻¹ − 1. - 13.Jean-Claude Franchitti and others, Introduction to Computer Science (web PDF of April 20, 2026, OpenStax, Rice University, 2024).Chapter 5, “Hardware Realizations of Algorithms: Computer Systems Design”, section 5.3, “Machine-Level Information Representation” (“Signed Integer Numbers Representation”), p. 207 (the whole topic: pp. 206–207).
“With n bits, the range of numbers that can be presented is [–2ⁿ⁻¹, +2ⁿ⁻¹ – 1]. You can see that this range contains 2ⁿ different numbers, exactly like n-bit unsigned integer numbers.”
Used for: the n-bit two’s complement range, −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1, contains 2ⁿ numbers, as many as the n-bit unsigned integers. - 14.Intel Corporation, “Intel 64 and IA-32 Architectures Software Developer’s Manual, Volume 1: Basic Architecture (order number 253665-084US)”, Intel (June 2024).Section “4.2.1.2, “Signed Integers” (p. 4-4)”. cdrdv2-public.intel.com/825744/253665-sdm-vol-1.pdf · open at the quoted passage.
“The sign bit is set for negative integers and cleared for positive integers and zero. Integer values range from –128 to +127 for a byte integer, from –32,768 to +32,767 for a word integer”
Used for: Intel’s signed integers: the sign bit is 1 for negative integers and 0 for positive integers and zero; the ranges −128 to 127 (8 bits) and −32,768 to 32,767 (16 bits). - 15.James Gosling and others, “The Java Language Specification, Java SE 25 Edition, Chapter 4: Types, Values, and Variables”, Oracle (2025).Section “4.2.1, “Integral Types and Values””. docs.oracle.com/javase/specs/jls/se25/html/jls-4.html · open at the quoted passage.
“For byte, from -128 to 127, inclusive”
Used for: Java’s 8-bit type byte has the range −128 to 127. - 16.Kenneth H. Rosen, Discrete Mathematics and Its Applications (8th ed., McGraw-Hill, 2019).Chapter 4, “Number Theory and Cryptography”, section 4.2, “Integer Representations and Algorithms” (Exercises, the text before Exercises 40 and 46), p. 270 (the whole topic: pp. 270).
“For a negative integer, the remaining bits are the bits of the binary expansion of 2ⁿ⁻¹ −|x|.”
Used for: Rosen’s description of two’s complement: a negative x is the sign bit 1 followed by the bits of 2ⁿ⁻¹ − |x|. - 17.Jean-Claude Franchitti and others, Introduction to Computer Science (web PDF of April 20, 2026, OpenStax, Rice University, 2024).Chapter 5, “Hardware Realizations of Algorithms: Computer Systems Design”, section 5.3, “Machine-Level Information Representation” (“Signed Integer Numbers Representation”), p. 207 (the whole topic: pp. 206–207).
“Since we have only one presentation of 0, we can see from the figure that with three bits, the two’s complement method can present up to –4, which we did not find in the other two methods.”
Used for: two’s complement has one pattern of 0; with three bits it reaches −4, which the other two ways do not. - 18.Jean-Claude Franchitti and others, Introduction to Computer Science (web PDF of April 20, 2026, OpenStax, Rice University, 2024).Chapter 5, “Hardware Realizations of Algorithms: Computer Systems Design”, section 5.3, “Machine-Level Information Representation” (“Signed Integer Numbers Representation”), p. 207 (the whole topic: pp. 206–207).
“The two’s complement of a binary number is simply the one’s complement with 1 added to the result.”
Used for: the operation “two’s complement”: invert every bit and add 1. - 19.James Gosling and others, “The Java Language Specification, Java SE 25 Edition, Chapter 15: Expressions”, Oracle (2025).Section “15.15.4, “Unary Minus Operator -””. docs.oracle.com/javase/specs/jls/se25/html/jls-15.html · open at the quoted passage.
“For all integer values x, -x equals (~x)+1.”
Used for: in Java, −x equals the inverted bits of x plus 1, for every integer value x. - 20.James Gosling and others, “The Java Language Specification, Java SE 25 Edition, Chapter 15: Expressions”, Oracle (2025).Section “15.15.4, “Unary Minus Operator -””. docs.oracle.com/javase/specs/jls/se25/html/jls-15.html · open at the quoted passage.
“The Java programming language uses two's-complement representation for integers, and the range of two's-complement values is not symmetric, so negation of the maximum negative int or long results in that same maximum negative number.”
Used for: the two’s-complement range is not symmetric, so negating the most negative value of Java’s 32-bit and 64-bit types (int and long) gives that same value. - 21.ISO/IEC JTC 1/SC 22/WG 14, “N3220: working draft of ISO/IEC 9899:2024 (C23), the C programming language standard. A free draft that the C committee lists as similar to the published standard, not the ISO text itself”, open-std.org, the site of the ISO C standards committee (22 February 2024).Section “7.8.2.1, “The imaxabs function”, footnote 257 (p. 224)”. open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf · open at the quoted passage.
“The absolute value of the most negative number is not representable.”
Used for: the C standard notes that the absolute value of the most negative number is not representable. - 22.Intel Corporation, “Intel 64 and IA-32 Architectures Software Developer’s Manual, Volume 1: Basic Architecture (order number 253665-084US)”, Intel (June 2024).Section “4.2.1, “Integers” (p. 4-3)”. cdrdv2-public.intel.com/825744/253665-sdm-vol-1.pdf · open at the quoted passage.
“Some integer instructions (such as the ADD, SUB, PADDB, and PSUBB instructions) operate on either unsigned or signed integer operands.”
Used for: Intel’s addition and subtraction instructions operate on unsigned or signed operands alike. - 23.Jean-Claude Franchitti and others, Introduction to Computer Science (web PDF of April 20, 2026, OpenStax, Rice University, 2024).Chapter 5, “Hardware Realizations of Algorithms: Computer Systems Design”, section 5.3, “Machine-Level Information Representation” (“Signed Integer Numbers Representation”), p. 207 (the whole topic: pp. 206–207).
“Additionally, the subtraction is nothing but an addition; that is, A – B is the same as A + two’s complement of B.”
Used for: A − B can be computed as A plus the two’s complement of B. - 24.Jean-Claude Franchitti and others, Introduction to Computer Science (web PDF of April 20, 2026, OpenStax, Rice University, 2024).Chapter 5, “Hardware Realizations of Algorithms: Computer Systems Design”, section 5.3, “Machine-Level Information Representation” (“Signed Integer Numbers Representation”), p. 207 (the whole topic: pp. 206–207).
“Another important issue with the two’s complement is that the hardware implementation of it is very efficient because we can use the same piece of hardware for both addition and subtraction and for both signed and unsigned integers.”
Used for: with two’s complement the same hardware serves addition and subtraction, and signed and unsigned integers. - 25.Kenneth H. Rosen, Discrete Mathematics and Its Applications (8th ed., McGraw-Hill, 2019).Chapter 4, “Number Theory and Cryptography”, section 4.2, “Integer Representations and Algorithms” (Exercises, the text before Exercises 40 and 46), p. 270 (the whole topic: pp. 270).
“Two’s complement expansions of integers are often used by computers because addition and subtraction of integers can be performed easily using these expansions, where these integers can be either positive or negative.”
Used for: two’s complement is used by computers because addition and subtraction of positive and negative integers are easy in it. - 26.Kenneth H. Rosen, Discrete Mathematics and Its Applications (8th ed., McGraw-Hill, 2019).Chapter 4, “Number Theory and Cryptography”, section 4.2, “Integer Representations and Algorithms” (Exercises, the text before Exercises 40 and 46), p. 270 (the whole topic: pp. 270).
“Two’s complement representations of integers are also used to simplify computer arithmetic and are used more commonly than one’s complement representations.”
Used for: two’s complement is used more commonly than ones’ complement. - 27.Intel Corporation, “Intel 64 and IA-32 Architectures Software Developer’s Manual, Volume 1: Basic Architecture (order number 253665-084US)”, Intel (June 2024).Section “4.2.1.2, “Signed Integers” (p. 4-4)”. cdrdv2-public.intel.com/825744/253665-sdm-vol-1.pdf · open at the quoted passage.
“All operations on signed integers assume a two's complement representation.”
Used for: Intel’s manuals state that all operations on signed integers assume two’s complement. - 28.James Gosling and others, “The Java Language Specification, Java SE 25 Edition, Chapter 4: Types, Values, and Variables”, Oracle (2025).Section “4.2, “Primitive Types and Values””. docs.oracle.com/javase/specs/jls/se25/html/jls-4.html · open at the quoted passage.
“The integral types are byte, short, int, and long, whose values are 8-bit, 16-bit, 32-bit and 64-bit signed two's-complement integers, respectively”
Used for: Java defines its signed integer types as two’s-complement integers of 8, 16, 32 and 64 bits. - 29.ISO/IEC JTC 1/SC 22/WG 14, “N3220: working draft of ISO/IEC 9899:2024 (C23), the C programming language standard. A free draft that the C committee lists as similar to the published standard, not the ISO text itself”, open-std.org, the site of the ISO C standards committee (22 February 2024).Section “6.2.6.2, “Integer types”, paragraph 6 (Note 2, p. 43)”. open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf · open at the quoted passage.
“The sign representation defined in this document is called two’s complement. Previous editions of this document (specifically ISO/IEC 9899:2018 and prior editions) additionally allowed other sign representations.”
Used for: C23 defines only two’s complement; the earlier editions, that of 2018 and those before it, also allowed other sign representations. - 30.ISO/IEC JTC 1/SC 22/WG 14, “Projects: ISO/IEC 9899, Revision of the C standard”, open-std.org, the site of the ISO C standards committee.Section “SC22/WG14 Milestones”. open-std.org/jtc1/sc22/wg14/www/projects · open at the quoted passage.
“2024: Published ISO/IEC 9899:2024 (aka C23)”
Used for: C23 is the edition of the C standard that ISO published in 2024. - 31.James Gosling and others, “The Java Language Specification, Java SE 25 Edition, Chapter 5: Conversions and Contexts”, Oracle (2025).Section “5.1.2, “Widening Primitive Conversion””. docs.oracle.com/javase/specs/jls/se25/html/jls-5.html · open at the quoted passage.
“A widening conversion of a signed integer value to an integral type T simply sign-extends the two's-complement representation of the integer value to fill the wider format.”
Used for: sign extension: Java widens its signed integer types by sign-extending the two’s-complement representation.