EngineerPlay

BCD, Gray code and two's complement practice

BCD stores each decimal digit in its own 4 bits, Gray code changes one bit per step, and two's complement gives the top bit a value of −2ⁿ⁻¹ so negative numbers add like any others. Every item is drawn as bit cells, a code table or an encoder disk.

Write a number in BCD and read a BCD code, find a parity bit, and the ASCII codes of letters and digits.

This byte is sent with even parity. What must the parity bit be?

A byte and its parity bit (even parity)Labels shown, in drawing order: Even parity, 0, 1, 0, 1, 0, ?, parity bit.Even parity01100110?parity bit

How to solve these

BCD. 8421 BCD writes each decimal digit in 4 bits: 47 is 0100 0111. The codes 1010 to 1111 are never used, and when two digits add to more than 9 the adder adds 0110 to skip them.

Gray code. Keep the top bit, then XOR each pair of neighbouring binary bits: 1011 becomes 1110. Going back, each binary bit is the bit just written XOR the next Gray bit. Consecutive Gray codes differ in one bit, so a position disk read halfway between two sectors is at most one step out.

Two's complement. In n bits the top bit is worth −2ⁿ⁻¹, so 8 bits hold −128 to 127. To write −20, take 20 = 0001 0100, flip every bit (1110 1011) and add 1: 1110 1100. A sum outside the range overflows and wraps round by 256: 100 + 50 shows −106.

Also here. Sign-magnitude, octal in groups of three, the bits for N values (the smallest n with 2ⁿ ≥ N), parity bits, ASCII codes (A = 65, a = 97, 0 = 48) and shifts (left doubles, right halves).

Worked example

Change the Gray code 10001 to binary.

Gray code 10001 and its binary to fill inLabels shown, in drawing order: Gray, 1, 0, 1, Binary, ?.Gray10001Binary?????
Show the working
  1. Write down what is given: the Gray code 10001.
  2. Keep the first bit. Each next binary bit is the binary bit you just wrote XOR the next Gray bit: a Gray 1 flips it, a Gray 0 keeps it.
  3. Bit by bit from the left: 1 → 1, 1 XOR 0 → 1, 1 XOR 0 → 1, 1 XOR 0 → 1, 1 XOR 1 → 0.
  4. Check: changing the binary back to Gray gives the code you started with. Answer: 11110.
Why not the others?
  • 10001 That copies the Gray bits unchanged; each binary bit is the binary bit before it XOR the next Gray bit.
  • 11001 That XORs neighbouring Gray bits, which is the binary-to-Gray rule; use the binary bit you just wrote.

Remember: Back from Gray: keep the top bit, then each next binary bit is the binary bit before it XOR the next Gray bit. Formula sheet

Questions

How do you write a negative number in two's complement?

Write the size in binary, flip every bit and add 1. −20 in 8 bits: 20 = 0001 0100, flipped 1110 1011, plus 1 gives 1110 1100. Check: −128 + 64 + 32 + 8 + 4 = −20.

Why are 1010 to 1111 invalid in BCD?

Each BCD group holds one decimal digit, 0 to 9. The six patterns worth 10 to 15 have no digit, so they never appear in a valid code.

Why do Gray codes change only one bit at a time?

The code is built that way so that a sensor reading between two positions can only be wrong by one step. In plain binary, 0111 to 1000 changes all four bits at once.

How are these answers checked?

Each question is generated fresh and its answer computed by code. A separate test reads the drawn bits, lamps and encoder disk and recomputes every answer with its own bit arithmetic.