Blackboard Exercise 2: Worked solutions
Algorithms, Programming, and Data Representation · Module 92313 · Blackboard Exercise 2
A worked route through number bases, binary fractions, integer encodings, ASCII and Boolean algebra. Try each subpart before opening its steps.
Exercise 2.1: Natural numbers
Connected topic: Number systems and data representation (§3.1–3.3)
2.1.1 Number representations
Try first: Complete the five conversions; show how the first and last binary rows convert directly to octal and hexadecimal.
Attempted it? Open the worked steps
2.1.1 Number representations · worked solution
Read each subscript as the base. To go from binary to octal, group the bits in threes from the right; to hexadecimal, in fours. Pad with zeros on the left only, where a group is short.
The first and last rows, converted directly:
101101102 = 010 110 1102 = 2668
101101102 = 1011 01102 = B616
1101010110012 = 110 101 011 0012 = 65318
1101010110012 = 1101 0101 10012 = D5916
The completed table, with the given entry of each row in bold:
Binary Octal Decimal Hexadecimal 10110110 266 182 B6 11100111 347 231 E7 10011100 234 156 9C 1011010111 1327 727 2D7 110101011001 6531 3417 D59 Grouping works because 8 = 23 and 16 = 24: one octal digit stands for exactly three bits and one hexadecimal digit for exactly four, so each group converts on its own and nothing carries from one group to the next.
Check: expand one row back in its own base, for example B616 = 11 · 16 + 6 = 182 and 3478 = 3 · 64 + 4 · 8 + 7 = 231.
2.1.2 Range of an unsigned representation
Try first: Find the ranges for 8, 16, and 32 bits, derive the general rule, then find the fewest bits that cover 0 through 1000.
Attempted it? Open the worked steps
2.1.2 Range of an unsigned representation · worked solution
An n-bit unsigned pattern has place values 20 to 2n − 1. The smallest value is all zeros; the largest is all ones, the sum of every place value.
Bits Smallest Largest 8 0 28 − 1 = 255 16 0 216 − 1 = 65 535 32 0 232 − 1 = 4 294 967 295 In general an n-bit unsigned integer covers
0 ≤ x ≤ 2n − 1
The numbers 0 to 1000 are 1001 different values, and n bits give 2n patterns, so we need the smallest n with 2n ≥ 1001:
29 = 512 < 1001 ≤ 1024 = 210
10 bits are required; 9 bits stop at 511.
2.1.3 Representation and storage
Try first: Decide which listed values fit an unsigned byte, identify equal values, and explain why distinct spellings can denote the same number.
Attempted it? Open the worked steps
2.1.3 Representation and storage · worked solution
An unsigned 8-bit integer stores the values 0 to 255. Work out each value, then compare it with that range.
Count the digits of the second binary value: 1000000002 has nine digits, a 1 followed by eight zeros, so it is 28.
111111112 = 255
1000000002 = 28 = 256
FF16 = 15 · 16 + 15 = 255
25610 = 256
Fit in an unsigned byte: 111111112 and FF16. Do not fit: 1000000002 and 25610; 256 needs nine bits.
Same value, different spelling: 111111112 = FF16 = 255, and 1000000002 = 25610 = 256. A numeral is a weighted sum of its digits in its base; different bases give different digit strings for the same number. A byte holds the value's bit pattern, not the way it was written.
2.1.4 Arithmetic
Try first: Calculate the four sums, difference, and products in the operand bases without converting the operands to decimal first; decide whether each result fits an unsigned byte.
Attempted it? Open the worked steps
2.1.4 Arithmetic · worked solution
Keep the base fixed throughout: line the digits up by place value and carry or borrow in that base, as in decimal written arithmetic.
a) Add column by column from the right; a column that sums to 2 or 3 writes 0 or 1 and carries 1 to the left.
27 26 25 24 23 22 21 20 carry 1 1 1 1 1 0 1 1 0 1 1 0 + 0 0 1 0 1 1 0 1 = 1 1 1 0 0 0 1 1 The sum is 111000112 (227).
b) Subtract from the right; where the top digit is too small, borrow 1 from the column to the left (it is worth 2 here). The marked columns lent a 1.
27 26 25 24 23 22 21 20 lent 1 1 1 1 1 1 1 0 0 1 0 1 0 − 0 0 1 1 0 1 1 1 = 1 0 0 1 0 0 1 1 The difference is 100100112 (147).
c) 000001002 = 22, so multiplying by it shifts every bit two places left: 001011012 × 000001002 = 101101002 (180). The two bits pushed out at the top were 0, so nothing is lost.
d) In hexadecimal, 7 + C = 19 = 16 + 3: write 3 and carry 1. Then A + 3 + 1 = 14 = E. So A716 + 3C16 = E316 (227).
Fit: all four results are at most FF16 = 255, and the difference is not negative, so each fits an unsigned byte. Check: a) and d) are the same sum, 182 + 45 = 167 + 60 = 227.
2.1.5 Overflow
Try first: For each decimal addition, find the mathematical sum, whether an 8-bit unsigned overflow occurs, and the stored byte. Then explain the NumPy result for uint8(250) + uint8(13).
Attempted it? Open the worked steps
2.1.5 Overflow · worked solution
An unsigned byte keeps only the low eight bits of a result, which is the result modulo 256. Overflow happens when the true sum is larger than 255.
Addition Mathematical result Overflow? Stored in 8 bits 200 + 55 255 no 255 200 + 56 256 yes 256 − 256 = 0 250 + 13 263 yes 263 − 256 = 7 128 + 128 256 yes 0 Condition: two unsigned bytes a and b overflow exactly when
a + b > 255
which is the same as a carry out of the leftmost bit; the stored value is then a + b − 256.
The NumPy lines: both operands are
uint8, so the sum is also computed as auint8: 250 + 13 = 263 wraps to 263 − 256 = 7, andprintshows7. Recent NumPy versions also warn about an overflow in the scalar addition. Python's own250 + 13would give 263, because Python integers have no fixed width.Check: 200 + 55 sits exactly on the limit 255, so it does not overflow.
2.1.6 Working with an unknown radix
Try first: The numeral 243 in base r has decimal value 99. Find r and decide whether 354r and 358r are valid.
Attempted it? Open the worked steps
2.1.6 Working with an unknown radix · worked solution
Expand by place value; a radix must be a whole number larger than every digit used, so r ≥ 5 here.
243r = 2r2 + 4r + 3 = 99
Rearrange and factorise:
2r2 + 4r − 96 = 0
r2 + 2r − 48 = 0
(r + 8)(r − 6) = 0
A radix is positive, so r = 6; −8 is rejected.
In base six the digits are 0 to 5. 3546 is valid: every digit is below 6. 3586 is not valid: 8 is not a base-six digit.
Check: 2 · 36 + 4 · 6 + 3 = 72 + 24 + 3 = 99.
Exercise 2.2: Floating-point numbers
Connected topic: Binary fractions and floating-point representation (§4.1)
2.2.1 Binary fractions
Try first: Complete the four binary/decimal conversions and show one calculation in each direction.
Attempted it? Open the worked steps
2.2.1 Binary fractions · worked solution
After the binary point the places are worth 2−1 = 12, 2−2 = 14, 2−3 = 18 and so on. Binary to decimal: add the place values of the 1 bits. Decimal to binary: double the fraction again and again; each whole part that appears is the next bit.
Binary to decimal:
0.10112 = 12 + 18 + 116 = 0.6875
Decimal to binary, for 0.625:
Fraction × 2 Bit 0.625 1.25 1 0.25 0.5 0 0.5 1.0 1 The fraction reaches 0, so 0.62510 = 0.1012. For 5.375, the whole part 5 = 1012 and 0.375 doubles to 0.75 (0), 1.5 (1), 1.0 (1), so 5.37510 = 101.0112.
The completed table:
Binary Decimal 0.1011 0.6875 10.011 2.375 0.101 0.625 101.011 5.375 Check: convert back, 101.0112 = 4 + 1 + 14 + 18 = 5.375.
2.2.2 Finite and repeating representations
Try first: Convert 0.110 until its repeating pattern is visible. Decide which listed fractions terminate in binary, then state the general condition.
Attempted it? Open the worked steps
2.2.2 Finite and repeating representations · worked solution
Use the doubling method from 2.2.1 on 0.1 and watch the fractions that remain:
Fraction × 2 Bit 0.1 0.2 0 0.2 0.4 0 0.4 0.8 0 0.8 1.6 1 0.6 1.2 1 0.2 0.4 0 … … … The fraction 0.2 has come back, so from there the bits 0011 repeat for ever:
0.110 = 0.0001 1001 1001 1…2
It never reaches 0 because 110 has the factor 5 in its denominator, and doubling can never cancel a 5.
Check each fraction in lowest terms: 12 = 0.12, 38 = 0.0112 and 516 = 0.01012 are finite; 110 and 720 (denominators 2 · 5 and 4 · 5) repeat.
Condition: a fraction pq in lowest terms has a finite binary expansion exactly when q is a power of 2, that is q = 2k. Then multiplying by 2k gives a whole number, which ends the doubling after k bits.
Exercise 2.3: Signed and unsigned integers
Connected topic: Integer representations and arithmetic (§3.4)
2.3.1 Two’s complement
Try first: Complete the table for the three bit patterns and the two decimal values, using exactly eight bits.
Attempted it? Open the worked steps
2.3.1 Two’s complement · worked solution
In 8-bit two's complement the leftmost bit is worth −128 and the others keep their usual positive weights. To encode a negative number, write its magnitude in 8 bits, invert every bit and add 1.
For −37: 37 = 001001012; inverted 110110102; plus 1 gives 110110112. For −8: 8 = 000010002 → 111101112 → 111110002.
The completed table:
Binary Decimal 01111111 127 11111111 −1 10000000 −128 11111000 −8 11011011 −37 Check with the −128 weight: 111110002 = −128 + 64 + 32 + 16 + 8 = −8 and 110110112 = −128 + 64 + 16 + 8 + 2 + 1 = −37.
2.3.2 Range and interpretation
Try first: Find the 8-bit and general n-bit ranges, explain the asymmetric counts, and interpret 111101102 both ways.
Attempted it? Open the worked steps
2.3.2 Range and interpretation · worked solution
In n-bit two's complement the leading bit is worth −2n − 1 and the other bits add up to at most 2n − 1 − 1.
For 8 bits the range is −128 to 127; in general
−2n − 1 ≤ x ≤ 2n − 1 − 1
There are 2n patterns. Half of them, 2n − 1, start with 1 and are negative; the other half start with 0, and one of those is zero, which leaves 2n − 1 − 1 positive values. So there is one more negative value than positive.
111101102 read as unsigned is 128 + 64 + 32 + 16 + 4 + 2 = 246; read as two's complement the first bit counts −128 instead, giving 246 − 256 = −10.
The bits themselves carry no sign. The type the program declares decides whether the leading bit is worth +128 or −128.
2.3.3 Signed overflow
Try first: Add 011001012 and 001111102, interpret all three patterns as signed decimal, and test for signed overflow.
Attempted it? Open the worked steps
2.3.3 Signed overflow · worked solution
Add the patterns as unsigned bits, then read the operands and the result as signed values.
011001012 + 001111102 = 101000112
Both operands start with 0, so they are positive: +101 and +62. Their true sum 163 is above 127. The result starts with 1 and reads as −128 + 32 + 2 + 1 = −93, which is 163 − 256.
Signed overflow occurs. Two positive operands gave a negative result. In general an addition overflows exactly when both operands have the same sign and the result's sign differs; adding a positive and a negative number can never overflow.
Exercise 2.4: Text representation and interpretation
Connected topic: Text and character representation (§3.5)
2.4.1 ASCII
Try first: Complete the table and encode AIBE as four 8-bit ASCII bytes.
Attempted it? Open the worked steps
2.4.1 ASCII · worked solution
ASCII gives each character a code from 0 to 127, stored in one byte. Convert the binary to decimal and look the code up, or the other way round.
Binary Decimal ASCII 01000001 65 A 01100001 97 a 00110000 48 0 (the digit) AIBE in 8-bit ASCII, one byte per letter:
Character Hexadecimal Binary A 41 01000001 I 49 01001001 B 42 01000010 E 45 01000101 Check: a capital and a small letter differ by 32, a single bit (010000012 and 011000012), and code 48 is the character '0', not the number zero.
2.4.2 One bit pattern, different meanings
Try first: Interpret 010000012 as unsigned, as 8-bit two’s complement, and as ASCII. State what extra information a reader needs.
Attempted it? Open the worked steps
2.4.2 One bit pattern, different meanings · worked solution
Evaluate the same eight bits under each encoding rule rather than changing the bits.
Unsigned interpretation: 65. Two’s-complement interpretation: 65, because the leading bit is zero. ASCII interpretation: 'A'.
The data’s type or agreed encoding supplies the meaning: for example, unsigned byte, signed 8-bit integer, or ASCII character. Bits alone do not specify their interpretation.
Exercise 2.5: Boolean algebra
Connected topic: Boolean algebra identities (§1.1–1.2)
2.5.1 Truth table
Try first: Fill the columns A′, A · B, A + B and A + (B · C) for all input rows in the given order.
Attempted it? Open the worked steps
2.5.1 Truth table · worked solution
Evaluate each row on its own: ′ flips a bit, · is AND, + is OR, and the brackets make B · C happen before the outer OR.
A B C A′ A · B A + B A + (B · C) 0 0 0 1 0 0 0 0 0 1 1 0 0 0 0 1 0 1 0 1 0 0 1 1 1 0 1 1 1 0 0 0 0 1 1 1 0 1 0 0 1 1 1 1 0 0 1 1 1 1 1 1 0 1 1 1 Check row 011: A = 0 but B · C = 1, so the last column is 1. In every row with A = 1 both OR columns are 1.
2.5.2 Simplification
Try first: Simplify the four expressions and name the Boolean law used at each step.
Attempted it? Open the worked steps
2.5.2 Simplification · worked solution
Rewrite one law at a time and name it, so each step can be checked.
A + A′ = 1
A · A′ = 0
both by the complement law.
A + (A′ · B) = (A + A′) · (A + B)
= 1 · (A + B) = A + B
by distributivity of + over ·, then the complement and identity laws.
(A + B)(A + B′) = A + B · B′
= A + 0 = A
by distributivity, then the complement and identity laws.
Check the last two by putting A = 0 and A = 1: the left sides then give B and 1, and 0 and 1, matching A + B and A.
2.5.3 Equivalence and construction
Try first: Compare A + (B · C) with (A + B)(A + C) using algebra and a truth table; construct F = 1 exactly when at least two inputs are 1.
Attempted it? Open the worked steps
2.5.3 Equivalence and construction · worked solution
Equivalence by algebra: multiply out the product, then use idempotence (A · A = A) and absorption (A + A · X = A):
(A + B)(A + C) = A · A + A · C + A · B + B · C
= A + A · C + A · B + B · C = A + B · C
By truth table: both expressions give 0, 0, 0, 1, 1, 1, 1, 1 for the rows 000 to 111 (the last column of 2.5.1), so they are equivalent.
At least two inputs are 1:
A B C F 0 0 0 0 0 0 1 0 0 1 0 0 0 1 1 1 1 0 0 0 1 0 1 1 1 1 0 1 1 1 1 1 Write one product term for each row where F = 1:
F = A′BC + AB′C + ABC′ + ABC
Copy the term ABC twice more (idempotence) and pair it with each other term; each pair loses the variable that differs:
A′BC + ABC = BC
AB′C + ABC = AC
ABC′ + ABC = AB
F = AB + AC + BC
Check: with exactly two inputs 1, one pair term is 1; with all three, all three terms are; with one or none, no pair is complete.
Personal worked solutions based on the APDR Blackboard Exercise 2 sheet, written in my own words; no exercise-sheet prose or figures are reproduced. Not an official FAU publication; check course details against the current materials. Made by Thanate Amornjiraporn (Focus).