About

Homework: Exercise 2

Theory: How to Approach These

Jump to: Q1 · Q2 · Q3 · Q4 · Q5


Homework Solutions

Exercise Q1

[Positional numbering system] Write each number in positional (expanded) form:

a. 526526

b. 1011010110

PartDetail
AsksShow each digit times its place value, then add them up.
NeedsValue =∑di⋅bi= \sum d_i \cdot b^i. The base: 526 is decimal (base 10), 10110 is binary (base 2).
Look atThe digits 5 and 6 in 526 mean it cannot be binary; 10110 uses only 0 and 1, so it is read as binary.
If it changesAnother base: same recipe, only bb changes.
Show solution ↓
Solution

How to solve

  1. Number the digits from the right, starting at position 0.
  2. Write each digit times bpositionb^{\text{position}}.
  3. Add the terms.
Position43210
Digit
Place value

Sample answer

(a) Base 10:

52610=5×102+2×101+6×100=500+20+6=526526_{10} = 5 \times 10^2 + 2 \times 10^1 + 6 \times 10^0 = 500 + 20 + 6 = 526

(b) Base 2:

101102=1×24+0×23+1×22+1×21+0×20=16+0+4+2+010110_2 = 1 \times 2^4 + 0 \times 2^3 + 1 \times 2^2 + 1 \times 2^1 + 0 \times 2^0 = 16 + 0 + 4 + 2 + 0

So 101102=221010110_2 = 22_{10} ✓ (check: 22=16+4+222 = 16 + 4 + 2).

Exercise Q2

[Converting numbers into different bases]

a. 2AD162AD_{16} to base 2

b. 5.155.15 to base 2 (limited to 8 fractional bits)

PartDetail
Asks(a) hex to binary. (b) a decimal fraction to binary, stopping at 8 places after the point.
Needs(a) one hex digit = 4 bits. (b) integer part by its binary form; fraction by repeated ×2\times 2.
Look at“8 fractional bits”: stop after 8 multiplications, the rest is dropped.
If it changesMore or fewer bits: do more or fewer multiplications.
Show solution ↓
Solution

How to solve

  1. (a) Replace each hex digit by its 4-bit group, then drop leading zeros.
  2. (b) Convert the integer part, then multiply the fraction by 2 eight times, writing down each integer part (0 or 1) and keeping the rest.
StepNumber ×2\times 2BitFraction kept
1
2

Sample answer

(a) 2=00102 = 0010, A=1010A = 1010, D=1101D = 1101:

2AD16=0010 1010 11012=101010110122AD_{16} = 0010\ 1010\ 1101_2 = 1010101101_2

Check: 2⋅256+10⋅16+13=6852 \cdot 256 + 10 \cdot 16 + 13 = 685 and 512+128+32+8+4+1=685512 + 128 + 32 + 8 + 4 + 1 = 685 ✓.

(b) Integer part: 5=10125 = 101_2. Fraction part 0.150.15:

StepNumber ×2\times 2BitFraction kept
10.15×2=0.300.15 \times 2 = 0.3000.30
20.30×2=0.600.30 \times 2 = 0.6000.60
30.60×2=1.200.60 \times 2 = 1.2010.20
40.20×2=0.400.20 \times 2 = 0.4000.40
50.40×2=0.800.40 \times 2 = 0.8000.80
60.80×2=1.600.80 \times 2 = 1.6010.60
70.60×2=1.200.60 \times 2 = 1.2010.20
80.20×2=0.400.20 \times 2 = 0.4000.40

Reading the bits top to bottom: 0.15≈0.0010011020.15 \approx 0.00100110_2. So 5.15≈101.0010011025.15 \approx 101.00100110_2.

Check: 0.001001102=18+164+1128=0.14843750.00100110_2 = \tfrac18 + \tfrac1{64} + \tfrac1{128} = 0.1484375, close to 0.150.15. The fraction never ends (it repeats 00110011), which is why the result is only approximate.

Exercise Q3

[Integer number representation and arithmetic] For −3−5-3 - 5, show the operation using four-bit binary of:

a. One’s complement

b. Two’s complement

Note: indicate if the result is overflow or not.

PartDetail
AsksCompute −3−5-3 - 5 in 4 bits in two systems and say whether each result fits.
Needs−3−5=(−3)+(−5)-3 - 5 = (-3) + (-5). One’s complement: flip bits, end-around carry. Two’s complement: flip and add 1, discard the carry out.
Look atSame-sign operands (both negative): overflow if the result sign is positive. The true answer is −8-8.
If it changesOther numbers: same steps. Check the true answer against the range of the system.
Show solution ↓
Solution

How to solve

  1. Write +3=0011+3 = 0011 and +5=0101+5 = 0101.
  2. Form −3-3 and −5-5 in the required system.
  3. Add the two patterns.
  4. One’s complement only: add any carry out back into the right end (end-around carry).
  5. Two’s complement only: discard the carry out.
  6. Compare the signs of the operands and the result, and check the range.
−3-3−5-5SumResultOverflow?
One’s complement
Two’s complement

Sample answer

(a) One’s complement (range −7-7 to +7+7): −3=1100-3 = 1100, −5=1010-5 = 1010.

1100+1010=1 0110⇒0110+1=01111100 + 1010 = 1\,0110 \quad\Rightarrow\quad 0110 + 1 = 0111

The carry out of the MSB is added back (end-around carry), giving 0111=+70111 = +7. Two negative numbers produced a positive result, and the true answer −8-8 is outside −7-7 to +7+7, so overflow.

(b) Two’s complement (range −8-8 to +7+7): −3=1101-3 = 1101, −5=1011-5 = 1011.

1101+1011=1 1000⇒discard the carry: 10001101 + 1011 = 1\,1000 \quad\Rightarrow\quad \text{discard the carry: } 1000

1000=−81000 = -8, which is the correct answer. Both operands are negative and the result is negative, and the carry into the MSB (1) equals the carry out (1), so no overflow ✓.

Watch out: the carry out of the MSB is not the overflow test in two’s complement. Compare the signs, or compare carry-in with carry-out of the MSB.

Exercise Q4

[Floating point and operation]

a. Convert 64.12564.125 into binary floating point of the format 14-bit simple model with bias 16. Is there a truncation?

b. Multiply 10.101×2−310.101 \times 2^{-3} and 1.01×271.01 \times 2^{7} and represent the result using IEEE 754 single precision format.

PartDetail
Asks(a) Encode 64.125 in the 14-bit model and say if bits are lost. (b) Multiply two binary floating-point numbers and encode the product in IEEE 754 single.
Needs14-bit model: sign 1, exponent 5 (bias 16), significand 8 bits after the implicit 1. IEEE single: sign 1, exponent 8 (bias 127), fraction 23 bits after the implicit 1.
Look at(a) count the fraction bits after normalizing: more than 8 means truncation. (b) the product may need renormalizing.
If it changesAnother number or format: change the bias and the field widths, keep the steps.
Show solution ↓
Solution

How to solve

  1. Convert to binary and normalize to 1.f×2e1.f \times 2^e.
  2. Stored exponent =e+bias= e + \text{bias}.
  3. Fit the fraction into the field: pad with zeros, or truncate if too long.
  4. (b) Multiply the significands, add the exponents, then renormalize.
SignExponentFraction

Sample answer

(a) 64=1000000264 = 1000000_2 and 0.125=0.00120.125 = 0.001_2, so

64.125=1000000.0012=1.0000000012×2664.125 = 1000000.001_2 = 1.000000001_2 \times 2^6
  • Sign: 00.
  • Exponent: 6+16=22=101106 + 16 = 22 = 10110.
  • Fraction: 000000001000000001 has 9 bits, but the field holds 8, so the last bit is cut: 0000000000000000.
130sign (1 bit)
12–810110exponent (5 bits)
7–000000000significand (8 bits)

The code is 0 10110 00000000. Yes, there is a truncation: the final 1 (worth 2−92^{-9} of the significand) is lost, so the stored value is 6464, not 64.12564.125.

(b) Multiply the significands and add the exponents:

10.1012×1.012=11.010012,2−3×27=2410.101_2 \times 1.01_2 = 11.01001_2, \qquad 2^{-3} \times 2^{7} = 2^{4}

Check in decimal: 2.625×1.25=3.28125=11.0100122.625 \times 1.25 = 3.28125 = 11.01001_2 ✓.

Renormalize: 11.01001×24=1.101001×2511.01001 \times 2^4 = 1.101001 \times 2^5 (this equals 52.552.5).

  • Sign: 00.
  • Exponent: 5+127=132=100001005 + 127 = 132 = 10000100.
  • Fraction: 101001101001 padded with zeros to 23 bits: 1010010000000000000000010100100000000000000000.
310sign (1 bit)
30–2310000100exponent (8 bits)
22–010100100000000000000000fraction (23 bits)

The result is 0 10000100 10100100000000000000000 =4252000016= 42520000_{16}. No truncation is needed because the fraction has only 6 significant bits.

Exercise Q5

[Challenge case] Suppose a computer uses 4-bit one’s complement representation. Ignoring overflows, what value will be stored in the variable jj after the following pseudocode routine terminates?

-2 → j    // Store -2 in j.
 6 → k    // Store 6 in k.

while k ≠ -6
    j = j - 1    // Don't forget to operate in one's complement representation
    k = k + 1
end while

Hint: jj starts with −2=1101-2 = 1101 in one’s complement and k=6=0110k = 6 = 0110. Use one’s complement arithmetic and ignore any overflow effect. The test k≠−6k \ne -6 means k≠1001k \ne 1001.

PartDetail
AsksRun the loop by hand with 4-bit one’s complement numbers and give the final jj.
Needs−6=1001-6 = 1001, −1=1110-1 = 1110, +1=0001+1 = 0001. Add with end-around carry. 0111+1=1000=−70111 + 1 = 1000 = -7 (the count wraps).
Look atkk counts up from 6, so it passes through 7, wraps to −7-7, then reaches −6-6. Stop when k=1001k = 1001.
If it changesAnother start or stop value: trace again row by row.
Show solution ↓
Solution

How to solve

  1. Write the start values: j=1101j = 1101, k=0110k = 0110.
  2. Check the test k≠1001k \ne 1001. If true, run the body.
  3. j−1=j+1110j - 1 = j + 1110 with end-around carry. k+1=k+0001k + 1 = k + 0001.
  4. Repeat until k=1001k = 1001.
Passkk beforeTest k≠1001k \ne 1001jj afterkk after
1
2

Sample answer

Passkk beforeTest k≠1001k \ne 1001jj afterkk after
101100110 (+6+6)true1101+1110=1 1011→11001101 + 1110 = 1\,1011 \to 1100 (−3-3)01110111 (+7+7)
201110111 (+7+7)true1100+1110=1 1010→10111100 + 1110 = 1\,1010 \to 1011 (−4-4)10001000 (−7-7)
310001000 (−7-7)true1011+1110=1 1001→10101011 + 1110 = 1\,1001 \to 1010 (−5-5)10011001 (−6-6)
410011001 (−6-6)false: loop ends

The loop body runs 3 times, so jj goes down by 3 from −2-2: j=10102=−5j = 1010_2 = -5 ✓.

Watch out: 0111+0001=10000111 + 0001 = 1000 is −7-7 in one’s complement, not +8+8. The wrap-around is what lets kk reach −6-6 by counting up.