Workbench / Mathematics Book 1 Exercises: Arithmetic and Numbers
B1·09
Negative Numbers
Exercises for chapter 9: the three jobs of the minus sign, why subtracting a negative adds, the sign rules proved from distributivity, why inequalities flip, choosing a remainder convention, two's complement by hand, encoding and decoding it in code, wrapping indexes and weekdays, a hike's climb and descent, a signed ledger in accounting style, meetings across time zones, and senior exercises on overflowing totals, the binary-search midpoint and byte-sortable keys.
Start here: how these exercises work
Write your answer in the box first; hints open one at a time as you write, and the solution a couple of minutes after the last hint. Theory is in your own words, practice and programs come with real output (Python 3.13), cumulative projects use chapters 1 to 9 together, what the chapter leaves out explains what it skips, and senior exercises put the ideas into designs and reviews.
Theory
T1. Three jobs for one sign
In , say what each minus sign means. Then evaluate it for , and explain why " is negative" is false.
T2. Why subtracting a negative adds, three ways
Explain with the number line, with money, and from the definition of subtraction.
T3. The sign rules, from distributivity
Without using the rule "a negative times a negative is positive", prove that , starting from .
T4. Why inequalities flip
Explain why multiplying both sides of by gives , using the number line and using the definition of . Then solve .
T5. Choosing a remainder
A clock shows 2 o'clock. What does it show 17 hours earlier? Compute and find its remainder modulo 12 under the floored and truncated conventions; say which one the clock needs and why.
T6. Two's complement by hand
Write as an 8-bit two's complement byte in two ways (as , and by flipping and adding 1). Then explain why the top bit is 1 exactly for the negative numbers.
Practice and programs
P1. By hand
(a) . (b) . (c) . (d) The lowest temperature recorded in a town is °C and the highest 41 °C: find the range. (e) Write , and as 8-bit two's complement bytes.
P2. Two's complement in code
Write encode(x, n), decode(bits) and negate(bits) for -bit two's complement, refusing values that don't fit. Check every 8-bit value against Python's int.to_bytes(..., signed=True), and see what negating 127 and gives.
P3. Weekdays and ring buffers
Find the weekday days after a Wednesday for , using Python's %. Then compute the index the C/Java/JavaScript remainder would give, and fix it.
P4. A hike's climb and descent
From a list of named points and elevations, compute each leg's signed change, the net change, the total climb, the total descent, and the lowest and highest points.
Cumulative projects (chapters so far: 01–09)
C1. A signed ledger in accounting style
Accounts often write negative amounts in brackets: (24,000.00). Write fmt and parse for amounts in paise with Indian grouping (chapter 2) and brackets for negatives, keep a running balance (chapter 3) and its lowest point, and test that formatting and parsing round-trip.
C2. Meetings across time zones
Convert a meeting time from Hyderabad to London, New York, Adelaide and Honolulu using signed offsets in minutes, and give the weekday each lands on. Use floored division (chapter 4, chapter 9) so negative minute counts come out right.
What the chapter leaves out
Integers as pairs
Mathematicians build the integers from whole numbers without assuming negatives exist: an integer is a pair meaning "", with and counted as the same when . Then is , or , and the sign rules can be proved from the rules for whole numbers. It's the same construction that builds fractions from pairs (chapter 6's companion), with addition in place of multiplication.
Rings
The integers, with and obeying the laws of chapter 1 and every number having an opposite, form a ring. So do remainders modulo (chapter 5's companion), polynomials, and -bit machine integers (they're the ring of remainders modulo ). The proofs in this chapter use only the ring laws, so they hold in all of these: in 8-bit arithmetic, is 1 too.
Saturating arithmetic
Some hardware and languages offer arithmetic that saturates instead of wrapping: stays 127 in signed 8 bits. Audio and image processing use it, because clipping at full volume or full white is less harmful than wrapping to full negative or black. It breaks the ring laws (), the price of a safer failure.
Negative zero
Sign-and-magnitude and ones' complement have two zeros. So do floating-point numbers: exists, compares equal to , and shows up in results such as or Math.round(-0.4) in JavaScript. Usually harmless; occasionally visible ( is ).
Senior exercises
S1. Totals that turned negative
A sales report stores amounts in paise as 32-bit signed integers. In its second month the annual total turns into a large negative number, and by December it looks plausible again. Explain using the output, and set the rule.
S2. The binary-search midpoint
A well-known bug: binary search computes mid = (lo + hi) / 2 in 32-bit integers. Show when it fails, compare the fixes, and say what changes for negative indexes or keys.
S3. Integer keys that sort as bytes
A key-value store orders keys by comparing their bytes. A team stores signed 64-bit IDs as two's complement bytes and finds negative IDs sorting after positive ones. Design an encoding that sorts correctly, and test it.