Arithmetic and NumbersMathematics · Book 1
0 / 0

Shelves / Arithmetic and Numbers

AN·09

Negative Numbers

Numbers below zero: what a negative number is (an opposite, a direction, a debt), the number line and absolute value, why subtracting a negative adds, why a negative times a negative is positive (proved, not memorised), the distributive law with subtraction, why inequalities flip, the three ways programming languages divide negative numbers, and how computers store negatives in two's complement, where 251 and −5 are the same byte.

31 topics · ~29 min read · 15-question quiz
Updated Suggest a correction

Start here

What this chapter answers

What does −3 mean, and is −10 bigger or smaller than −2? Why is 5−(−3)=8? Why is −2×−3=6 and not −6, and is that a convention someone chose or something that couldn't be otherwise? Why does multiplying both sides of an inequality by −1 turn it round? Why does Python say −17mod5=3 while JavaScript says −2? And how can the same byte mean both 251 and −5?

Negative numbers in one picture

Extend chapter 1's number line to the left of zero: −1,−2,−3,…, each as far from zero as its positive partner but on the other side. A number now has a size (its distance from zero) and a direction (which side). Adding a positive number moves right; adding a negative moves left. Everything in this chapter is that picture, plus the insistence that the laws of chapter 1 keep working on the new numbers.

What a negative number is

Opposites

For every number a, its opposite −a is the number that adds to it to make zero:

a+(−a)=0.

So −5 is "the number that undoes 5": a debt of ₹5 cancels a credit of ₹5; 5 floors down cancels 5 floors up; 5 °C below zero is as far from zero as 5 °C above it. The whole numbers together with their opposites are the integers: …,−3,−2,−1,0,1,2,3,…. The opposite of an opposite is the number itself, −(−a)=a, because a is what adds to −a to make zero. And each number has only one opposite: if b and c both add to a to make zero, then b =b+(a+c) =(b+a)+c =c.

Order and absolute value

On the number line, numbers increase to the right, so −10<−2: a temperature of −10 °C is colder than −2 °C, and a debt of ₹10 leaves you worse off than a debt of ₹2. The absolute value |a| is a number's distance from zero, its size without its direction: |−7|=|7|=7. The distance between two numbers on the line is |a−b|: from −3 to 4 is |−3−4|=7 steps.

Adding and subtracting

Adding: moves on the line

Start at the first number and move: right for a positive amount, left for a negative one. 3+(−5): start at 3, move 5 left, land on −2. −4+(−2)=−6. In words: same signs, add the sizes and keep the sign; different signs, subtract the smaller size from the larger and keep the sign of the larger. The laws of chapter 1 still hold: addition of integers is commutative and associative, and 0 is still the identity.

Subtracting: add the opposite

Chapter 3 defined a−b as the number you add to b to get a. With negatives that number always exists, and it's a+(−b), because b+(a+(−b)) =a+(b+(−b)) =a+0 =a. So:

Interactive diagram: Adding and subtracting signed numbers on the line. It needs JavaScript; the text around it says the same.
a−b=a+(−b),5−(−3)=5+3=8.

Subtracting a negative adds. On the line: 5−(−3) asks "how far from −3 to 5?", which is 8 steps to the right. And for money: cancelling a ₹3 debt (subtracting −3) leaves you ₹3 better off. Subtraction is now always possible, which is why the integers were invented, and it's never commutative: a−b and b−a are opposites.

Multiplying and dividing

Positive times negative

3×(−4) is three lots of −4: −4+(−4)+(−4)=−12. Three debts of ₹4 are a debt of ₹12. By commutativity, (−4)×3=−12 too. In general, (−a)×b =−(a×b): a negative times a positive is negative.

Negative times negative

What should (−3)×(−4) be? "Minus three lots of minus four" has no direct picture, so ask what the laws of arithmetic allow. Chapter 1's distributive law forces the answer.

So a negative times a negative must be positive, if distributivity is to keep working for the new numbers; any other choice would break the arithmetic of the whole numbers we already trust. That's the honest answer to "why": not a rule handed down, but the only rule consistent with the ones before it. A pattern shows the same thing without algebra: 3×(−4)=−12, 2×(−4)=−8, 1×(−4)=−4, 0×(−4)=0; each step down adds 4, so the next, (−1)×(−4), is 4.

Dividing

Division undoes multiplication, so the signs follow: −12÷3=−4 because 3×(−4)=−12, and −12÷(−3)=4 because (−3)×4=−12. Same signs give a positive result, different signs a negative one, for both multiplication and division. Fractions take signs the same way: −34 =3−4 =−34, which is why fraction types keep the sign on the top (chapter 6's companion, drill D2).

Distributivity with subtraction

With subtraction written as adding the opposite, distributivity covers it at once:

a(b−c)=a(b+(−c))=ab+a(−c)=ab−ac.

So 7×98 =7×(100−2) =700−14 =686, a mental-arithmetic trick that chapter 1 could only state for positive differences. The same algebra gives the familiar (a−b)(a+b)=a2−b2: 49×51 =502−12 =2499.

Why inequalities flip

If a<b, then adding the same number to both keeps the order. Multiplying by a positive number keeps it; multiplying by a negative number reverses it: 2<5, but −2>−5. The reason: a<b means b−a is positive; multiplying by −1 gives (−b)−(−a)=−(b−a), which is negative, so −b<−a. This is the bug in chapter 6's companion, drill D2: cross-multiplying to compare fractions multiplies by both denominators, which flips the comparison if one of them is negative.

Division with remainder, for negatives

Chapter 4's division algorithm, a=qb+r with 0≤r<b, was for whole numbers. With negatives, there's a choice to make about r, and programming languages chose differently:

Aspect Floored (Python's //, %) Truncated (C, Java, JavaScript) Euclidean
Quotient Rounded down Rounded towards zero Chosen so r≥0
−17 by 5 q=−4, r=3 q=−3, r=−2 q=−4, r=3
17 by −5 q=−4, r=−3 q=−3, r=2 q=−3, r=2
Sign of r Same as the divisor Same as the dividend Never negative

All three satisfy a=qb+r with |r|<|b|; they differ only in which of the two possible remainders they pick. For clocks, calendars, array indexes and hash buckets you want a remainder in 0 to b−1 even for negative inputs: Python's % with a positive divisor gives that; in C, Java and JavaScript, -17 % 5 is -2, and an index of −2 is a bug. The lab measures all three.

Negative numbers in computers

The problem

A byte has 8 bits and can hold 256 patterns. For whole numbers they mean 0 to 255 (chapter 2). To store negatives, some patterns must be given negative meanings. Three schemes have been used:

  • Sign and magnitude: the first bit is the sign, the other seven the size. +5 is 00000101, −5 is 10000101. Simple, but there are two zeros (00000000 and 10000000) and addition needs special cases.
  • Ones' complement: −x is x with every bit flipped. −5 is 11111010. Still two zeros.
  • Two's complement: −x is stored as 28−x. −5 is 256−5=251, 11111011. One zero, and addition needs no special cases at all.

Two's complement

In two's complement with n bits, a number x from −2n−1 to 2n−1−1 is stored as the remainder of x on division by 2n (chapter 4, floored): positives as themselves, negatives as 2n+x. For 8 bits that's −128 to 127, and the first bit happens to be 1 exactly for the negatives.

Interactive diagram: Two's complement: a circle of 8 patterns. It needs JavaScript; the text around it says the same.

The reason every computer uses it is chapter 5's "remainders add and multiply". The hardware adds bit patterns as unsigned numbers and keeps the last 8 bits, which is arithmetic modulo 256; and −5 and 251 are the same number modulo 256 (they differ by 256). So adding 251 is the same, modulo 256, as adding −5: 12+251=263, and keeping 8 bits gives 7, which is 12+(−5). The same circuit adds signed and unsigned numbers; only the reading of the result differs.

So 5 = 00000101, flipped 11111010, plus 1 = 11111011 = 251, the code for −5.

Overflow, and the number with no opposite

Fixed width means a fixed range. In 8 bits, 127+1 wraps to −128 (the pattern 10000000): the true answer, 128, isn't representable, and the remainder modulo 256 reads as −128. The range is lopsided: −128 exists but +128 doesn't, so −128 has no opposite in 8 bits, and negating it gives −128 again. In 32 bits the same happens to −2,147,483,648: abs of it is itself, a famous source of bugs in code that assumes absolute values are never negative.

Lab: negative numbers in code

Experiment 1: three remainders

Python
import math
print(" a   b | a // b  a % b | trunc  rem (C, Java, JS) | Euclidean q  r")
for a, b in ((17, 5), (-17, 5), (17, -5), (-17, -5)):
    tq = int(a / b)                      # truncate towards zero
    tr = a - b * tq
    eq = (a - a % abs(b)) // b           # Euclidean: remainder always 0 <= r < |b|
    er = a - b * eq
    print(f"{a:>3} {b:>3} | {a // b:>6} {a % b:>5} | {tq:>5} {tr:>4}            | {eq:>11} {er:>2}")
print("math.fmod(-17, 5) =", math.fmod(-17, 5), "| divmod(-17, 5) =", divmod(-17, 5))
Text
 a   b | a // b  a % b | trunc  rem (C, Java, JS) | Euclidean q  r
 17   5 |      3     2 |     3    2            |           3  2
-17   5 |     -4     3 |    -3   -2            |          -4  3
 17  -5 |     -4    -3 |    -3    2            |          -3  2
-17  -5 |      3    -2 |     3   -2            |           4  3
math.fmod(-17, 5) = -2.0 | divmod(-17, 5) = (-4, 3)

All three conventions agree for positive numbers and disagree as soon as a sign is negative. Python's // and % are floored, so -17 % 5 is 3; truncation (what C, Java and JavaScript do, and Python's math.fmod) gives −2; the Euclidean remainder is never negative, even for a negative divisor. Each row satisfies a=qb+r: check −17 =(−4)(5)+3 =(−3)(5)−2.

Experiment 2: one byte, two readings

Python
for n in (5, -5, 127, -128, -1):
    b = n.to_bytes(1, "big", signed=True)
    print(f"{n:>5} -> byte {b[0]:08b} = {b[0]:>3} read unsigned")
print(int.from_bytes(bytes([251]), "big", signed=True), int.from_bytes(bytes([251]), "big", signed=False))
print("-5 in 16 bits:", f"{(-5) & 0xFFFF:016b}", "| 2**8 - 5 =", 2 ** 8 - 5)
Text
    5 -> byte 00000101 =   5 read unsigned
   -5 -> byte 11111011 = 251 read unsigned
  127 -> byte 01111111 = 127 read unsigned
 -128 -> byte 10000000 = 128 read unsigned
   -1 -> byte 11111111 = 255 read unsigned
-5 251
-5 in 16 bits: 1111111111111011 | 2**8 - 5 = 251

The byte 11111011 is 251 read as unsigned and −5 read as signed: the same bits, 28−5=251. −1 is all ones (255), and −128 is 10000000 (128). In 16 bits, −5 is twelve 1s and then 1011: two's complement sign-extends, filling the new high bits with copies of the sign bit, which keeps the value the same at any width.

Experiment 3: overflow in JavaScript

JavaScript
const a = new Int8Array([127, -128, 100]);
a[0] += 1; a[1] -= 1; a[2] *= 2;
console.log(Array.from(a));
console.log((2147483647 + 1) | 0, Math.abs(-2147483648 | 0) | 0, (-7 >> 1), (-7 / 2) | 0, Math.floor(-7 / 2));
const i = new Int32Array([-2147483648]); i[0] = -i[0]; console.log(i[0]);
Text
[ -128, 127, -56 ]
-2147483648 -2147483648 -4 -3 -4
-2147483648

An Int8Array stores 8-bit two's complement: 127 + 1 wraps to −128, −128−1 wraps to 127, and 100×2=200 becomes 200−256=−56. JavaScript's | 0 converts to a 32-bit integer, so 231−1+1 wraps to −231, and Math.abs of −231, forced back to 32 bits, is still negative; negating it in an Int32Array gives itself. Shifting right by one (-7 >> 1) floors (−4), while (-7 / 2) | 0 truncates (−3): two "halvings" that disagree for negatives, like Experiment 1's remainders.

Experiment 4: sorting signed bytes

Python
nums = [3, -1, 0, -128, 127, -20, 5]
as_bytes = {n: n.to_bytes(1, "big", signed=True)[0] for n in nums}
print("by value:          ", sorted(nums))
print("by stored byte:    ", sorted(nums, key=lambda n: as_bytes[n]))
print("byte, sign flipped:", sorted(nums, key=lambda n: as_bytes[n] ^ 0x80))
print("as text:           ", sorted(str(n) for n in nums))
Text
by value:           [-128, -20, -1, 0, 3, 5, 127]
by stored byte:     [0, 3, 5, 127, -128, -20, -1]
byte, sign flipped: [-128, -20, -1, 0, 3, 5, 127]
as text:            ['-1', '-128', '-20', '0', '127', '3', '5']

Sorting the stored bytes as unsigned numbers puts every negative after every positive, because negatives have the top bit set. Flipping the sign bit (XOR with 0x80) maps −128…127 onto 0…255 in order, so byte order then matches numeric order; databases and sort routines use exactly this trick to compare signed keys as raw bytes. Sorting the numbers as text is wrong in yet another way (−1 before −128, 127 before 3), chapter 1's point that numerals aren't numbers.

Worked examples

Arithmetic with signs

Temperatures, balances and distances

Bytes and remainders

Exercises

Practice

  1. Put in order, smallest first: −3, 2, −11, 0, −0.5, |−4|.
  2. Calculate: (a) −9+4; (b) −9−4; (c) −9−(−4); (d) 4−9−(−12)+(−3).
  3. Calculate: (a) −7×8; (b) −56÷(−7); (c) (−1)10 and (−1)11; (d) −4×(−5)×(−1)×2.
  4. Use distributivity with subtraction to compute mentally: (a) 9×99; (b) 25×38−25×18; (c) 31×29.
  5. The temperature on a mountain is −14 °C and in the valley 9 °C. What's the difference? If it drops 6 degrees everywhere, what is each?
  6. Write in 8-bit two's complement: (a) −1; (b) −100; (c) 100. What number is 10110000?
  7. Find q and r for −23 divided by 6 under the floored, truncated and Euclidean conventions.
Answers
  1. −11,−3,−0.5,0,2,|−4| =4.
  2. (a) −5; (b) −13; (c) −5; (d) 4−9+12−3=4.
  3. (a) −56; (b) 8; (c) 1 and −1; (d) −40.
  4. (a) 9×(100−1)=891; (b) 25×(38−18)=500; (c) (30+1)(30−1) =900−1 =899.
  5. 9−(−14)=23 degrees. After the drop: −20 °C and 3 °C; the difference is still 23.
  6. (a) 11111111; (b) 256−100=156 = 10011100; (c) 01100100. 10110000 = 176 unsigned, 176−256=−80.
  7. Floored: q=−4, r=1 (−23=−24+1). Truncated: q=−3, r=−5. Euclidean (divisor positive): same as floored, q=−4, r=1.

Harder

  1. Prove that −(a+b)=(−a)+(−b) and that −(a−b)=b−a.
  2. Show that if a<b and c<0, then ac>bc. Use it to explain why −2x<6 means x>−3.
  3. Explain why, in n-bit two's complement, adding a number to its own code with the bits flipped always gives all 1s, and why that means flipped-plus-one negates.
  4. A program stores temperatures in tenths of a degree as signed 16-bit integers. What range can it store? What happens if it computes the average of two readings as (a+b)/2 when both are near the top of the range, and how should it be computed instead?
  5. Why is (−a)2=a2 for every a, and what does that tell you about solving y2=25?
Answers
  1. (a+b)+((−a)+(−b)) =(a+(−a))+(b+(−b)) =0, so (−a)+(−b) is the opposite of a+b (opposites are unique). Then −(a−b) =−(a+(−b)) =(−a)+b =b−a.
  2. a<b means b−a>0. With c<0, (b−a)c<0 (a positive times a negative), so bc−ac<0, ac>bc. For −2x<6: multiply both sides by −12, which is negative, so the inequality flips: x>−3. Check x=0: 0<6 ✓; x=−4: 8<6 ✗.
  3. In each position, a bit and its flip are one 0 and one 1, so they add to 1 with no carries: the sum is n ones, 2n−1. So flipped =2n−1−x, and flipped plus one is 2n−x, the code of −x.
  4. −32,768 to 32,767 tenths: −3276.8 to 3276.7 degrees, far more than needed. But a+b can overflow if both are large (two readings of 2000.0 degrees, 20,000 each, sum to 40,000, which wraps negative). Compute a+(b−a)/2 (safe when the difference fits), or widen to 32 bits before adding. A real thermometer won't reach those values, but the same bug in binary search over large arrays was found in widely used libraries in 2006.
  5. (−a)2 =(−a)(−a) =a·a =a2, by the sign rule. So squaring loses the sign: both 5 and −5 square to 25, and y2=25 has two solutions, y=5 and y=−5. (Chapter 10's square roots pick the positive one by convention.)

Challenge

  1. Suppose someone proposes the rule (−1)×(−1)=−1 instead. Using only the distributive law and (−1)×1=−1, derive a contradiction, so that the proposal can't be adopted without giving up distributivity.
Discussion

Distributivity gives (−1)×(1+(−1)) =(−1)×1+(−1)×(−1). The left side is (−1)×0=0 (zero times anything is zero, which itself follows from distributivity). The right side, under the proposal, is −1+(−1)=−2. So 0=−2, a contradiction. Keeping (−1)(−1)=−1 would mean giving up distributivity, and with it long multiplication, the area model and most of algebra. So (−1)(−1)=1 isn't a convention in the way that "1 isn't prime" is; it's forced.

In real life

Misconceptions and limits

The model to keep

Common misconceptions

Misconception What's actually true Where it's shown
"−10 is bigger than −2 because 10 is bigger than 2." Order runs left to right on the line: −10<−2. Size is |−10|>|−2|. Order and absolute value
"−a is always negative." −a is the opposite of a; if a=−3, then −a=3. Opposites
"Two negatives make a positive" (for addition too). Only for multiplication and division; −3+(−4)=−7. Adding: moves on the line
"Negative times negative is positive by convention." It's forced by the distributive law. Negative times negative
"−22=4." −22=−(22)=−4; (−2)2=4. Worked examples
"Multiplying both sides of an inequality by a number keeps it true." A negative multiplier reverses it. Why inequalities flip
"% always gives a remainder from 0 to b−1." In C, Java and JavaScript, -17 % 5 is −2. Division with remainder, for negatives
"The absolute value of a number is never negative." In fixed-width types, abs of the most negative value overflows and stays negative. Overflow, and the number with no opposite

Limits and trade-offs

  • Integers are a model of opposites, not of everything with a minus sign. A temperature of −10 °C isn't "minus ten units of something"; it's a position on a scale with an arbitrary zero. Twice −10 °C isn't "twice as cold".
  • Fixed-width integers aren't the integers. They're remainders modulo 2n read in a particular way; they obey the laws of arithmetic only modulo 2n, and comparisons break at the wrap.
  • Negative remainders are a convention. Choose the one your problem needs (usually a remainder from 0 to b−1), and don't assume your language chose it.
  • Some quantities can't be negative. Counts, lengths and prices: a negative result there signals an error, not a debt. Validate.

Revision sheet

Key ideas

  • Opposite: a+(−a)=0; unique; −(−a)=a. Integers: whole numbers and their opposites.
  • Order: left to right on the line; −10<−2. Absolute value |a|: distance from 0. Distance between a and b: |a−b|.
  • Add: move right for positive, left for negative. Subtract = add the opposite: a−(−b)=a+b.
  • Signs: 0×b=0; (−a)b=−(ab); (−a)(−b)=ab, forced by distributivity. Same for division.
  • Distributivity with subtraction: a(b−c)=ab−ac; (a−b)(a+b)=a2−b2.
  • Inequalities: multiplying or dividing by a negative reverses them.
  • Remainders with negatives: floored (Python), truncated (C, Java, JS), Euclidean (r≥0); all satisfy a=qb+r.
  • Two's complement (n bits): x stored as xmod2n; −x as 2n−x = flip the bits and add 1. Range −2n−1 to 2n−1−1.
  • Overflow wraps; −2n−1 has no opposite in n bits.

Glossary

  • Opposite (additive inverse): the number that adds to a given number to make zero; −a.
  • Integer: a whole number or the opposite of one: …,−2,−1,0,1,2,…
  • Absolute value: a number's distance from zero, |a|.
  • Signed number: a number with a direction (positive or negative) as well as a size.
  • Sign rule: same signs multiply or divide to a positive result, different signs to a negative one.
  • Floored division: division whose quotient is rounded down, so the remainder has the divisor's sign.
  • Truncated division: division whose quotient is rounded towards zero, so the remainder has the dividend's sign.
  • Euclidean division: division whose remainder is never negative.
  • Sign and magnitude: storing a sign bit and the size separately.
  • Ones' complement: storing −x as x with every bit flipped.
  • Two's complement: storing −x in n bits as 2n−x; flip the bits and add 1.
  • Sign extension: widening a two's complement number by copying its sign bit into the new high bits.
  • Integer overflow: a result outside a fixed-width type's range, which wraps around.