Skip to content
Tutorials Center
My learning

Topics

Lesson 63 of 67 ยท Practice and Interview

Practice: Data Representation and Digital Logic

Solve exercises on base conversion, two's complement, IEEE 754 floats, logic gates and adders, then check every answer with short Python programs.

Reading about number systems and gates is one thing; doing the arithmetic by hand is what makes it stick. This lesson gives you a set of exercises, grouped by topic, with worked solutions. For each group, try the problems on paper first, then run the Python checker to see whether your answers agree.

The exercises revisit Number Systems and Conversions, Signed Integers and Two's Complement, Floating-Point Numbers and IEEE 754, Logic Gates and Boolean Algebra and Combinational Circuits. If a step feels shaky, jump back to that lesson and return here.

Part 1: Conversions

Exercise 1.1. Convert 0xB7 to decimal.

Exercise 1.2. Convert decimal 202 to 8-bit binary.

Exercise 1.3. Convert 0b11010110 to hexadecimal.

Exercise 1.4. Convert octal 755 to decimal.

Solutions.

  • 1.1: B is 11, so 0xB7 = 11 x 16 + 7 = 176 + 7 = 183.
  • 1.2: 202 = 128 + 64 + 8 + 2, so the bits are 1100 1010.
  • 1.3: Split into nibbles: 1101 is D and 0110 is 6, so the answer is 0xD6.
  • 1.4: 7 x 64 + 5 x 8 + 5 = 448 + 40 + 5 = 493.

A quick trick for hex: each hex digit is exactly four bits, and each octal digit is exactly three bits, so converting between binary and either of them is a regrouping, not a calculation.

Part 2: Two's complement

Exercise 2.1. Write -45 as an 8-bit two's complement number.

Exercise 2.2. What decimal value does the 8-bit pattern 0x9C have if it is read as signed?

Exercise 2.3. What is the range of a 12-bit two's complement integer?

Exercise 2.4. An 8-bit signed adder computes 100 + 50. What does it produce, and is the result correct?

Exercise 2.5. Sign-extend the 4-bit value 1011 to 8 bits.

Solutions.

  • 2.1: 45 = 0010 1101. Flip: 1101 0010. Add 1: 1101 0011, which is 0xD3.
  • 2.2: As unsigned, 0x9C is 9 x 16 + 12 = 156. Subtract 256 to read it as signed: 156 - 256 = -100.
  • 2.3: With n = 12 bits the range is -2^11 to 2^11 - 1, which is -2048 to 2047.
  • 2.4: 100 + 50 = 150, but the largest 8-bit signed value is 127. The bit pattern is 1001 0110, which reads as -106. This is signed overflow: two positive operands gave a negative result, which is exactly how the hardware's overflow flag detects it.
  • 2.5: 1011 is -5 in 4 bits. Copy the sign bit into the new upper bits: 1111 1011, which is still -5 in 8 bits.
Python Check Parts 1 and 2

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Part 3: IEEE 754 floating point

Exercise 3.1. Encode -6.25 as a 32-bit float and give the result in hex.

Exercise 3.2. Decode the single-precision pattern 0x41200000.

Exercise 3.3. Why does 0.1 + 0.2 not equal 0.3 in double precision?

Exercise 3.4. How many distinct finite values can a half-precision float (1 sign bit, 5 exponent bits, 10 fraction bits) represent, ignoring the two zeros being equal?

Solutions.

  • 3.1: Drop the sign: 6.25 = 110.01 in binary = 1.1001 x 2^2. The sign bit is 1. The stored exponent is 2 + 127 = 129 = 1000 0001. The fraction is 1001 followed by zeros. Together: 1 10000001 10010000000000000000000, regrouped as 1100 0000 1100 1000 0000... which is 0xC0C80000.
  • 3.2: 0 10000010 01000000000000000000000. The exponent field is 130, so the true exponent is 3. The significand is 1.01 in binary = 1.25. The value is 1.25 x 8 = 10.0.
  • 3.3: 0.1 and 0.2 have infinite binary expansions (0.1 is 0.000110011...), so each is rounded to the nearest representable value. The two rounding errors do not cancel, and the sum lands one step away from the double nearest to 0.3. Compare floats with a tolerance, not with ==.
  • 3.4: The 5-bit exponent field has 32 patterns; the all-ones pattern is reserved for infinity and NaN, leaving 31 for zero, subnormals and normal numbers. So there are 31 x 1024 x 2 = 63,488 patterns for finite values (zero, subnormals and normals, both signs), and since +0 and -0 are the same value, 63,487 distinct finite values.
Python Check Part 3

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Part 4: Gates and adders

Exercise 4.1. Build XOR from NAND gates only. How many NAND gates does the smallest standard construction use?

Exercise 4.2. Use De Morgan's law to rewrite NOT(A AND B) using OR.

Exercise 4.3. A full adder receives A = 1, B = 1 and carry-in = 1. What are the sum and carry-out?

Exercise 4.4. Add 0111 and 0101 with a 4-bit ripple-carry adder. Give the result and whether signed overflow occurs.

Exercise 4.5. A 4-to-1 multiplexer has how many select lines?

Solutions.

  • 4.1: With n = NAND(A, B), the XOR is NAND(NAND(A, n), NAND(B, n)). That uses 4 NAND gates.
  • 4.2: NOT(A AND B) = (NOT A) OR (NOT B).
  • 4.3: The sum is A xor B xor Cin = 1 xor 1 xor 1 = 1, and the carry-out is 1 (at least two inputs are 1). Together they encode the count of ones, 3 = 11.
  • 4.4: 7 + 5 = 12 = 1100. Unsigned that is correct. As signed 4-bit numbers 1100 is -4, but both operands were positive, so signed overflow occurs (carry into the sign bit differs from carry out of it).
  • 4.5: Four inputs need log2(4) = 2 select lines.
Python Check Part 4 with gate functions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

How to use this set

If you got most answers right, move on. If an exercise went wrong, do not just read the solution: redo the step that failed with a new number. For instance, repeat 2.1 with -100 or 3.1 with 0.15625. The Python checkers are meant to be changed: swap in your own values and compare.

When you check a hand calculation, find where it first differs from the program rather than comparing only the final answer. In binary work, most mistakes are a dropped carry or a forgotten "add 1".

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which pattern is -1 in 8-bit two's complement?

    Flip 0000 0001 to get 1111 1110, then add 1 to get 1111 1111. Adding 1 to this pattern wraps to 0000 0000, as -1 + 1 should.
  2. 2. A 32-bit float has the exponent field 130. What is the true exponent?

    Single precision uses a bias of 127, so the true exponent is 130 - 127 = 3.
  3. 3. In the exercise above, 100 + 50 in 8-bit signed arithmetic gives -106. What is the cleanest way to detect that this is wrong?

    Two positive operands can never sum to a negative number mathematically. A sign flip between inputs and result is the signed-overflow test.
  4. 4. Which gate is enough, on its own, to build any other gate?

    NAND (and NOR) are universal: NOT, AND, OR and everything else can be built from NAND gates alone.

Summary

  • Hex and octal conversions are regroupings of bits; decimal conversions use place values.
  • To negate in two's complement: flip the bits and add 1. To read a pattern as signed, subtract 2^n when the top bit is set.
  • Signed overflow means two same-sign operands produced a result of the opposite sign.
  • A float is sign, biased exponent and fraction; decode and encode them with struct to check your work by hand.
  • NAND is universal; a full adder is two XORs plus carry logic, and a ripple-carry adder chains them.

Continue with Practice: CPU, Pipeline, Cache and Virtual Memory.