EngineerPlay

BCD, Gray code and two's complement rules

Plain binary is not the only way to store a number. BCD keeps each decimal digit in its own 4 bits, Gray code changes one bit per step, and two's complement gives the top bit a negative place value so the same adder works for negative numbers.

The formulas

FormulaWhat it givesSymbols
d₂ × 100 + d₁ × 10 + d₀ = NA BCD code's valueeach group of 4 bits is one digit d from 0 to 9, read with place values 8 4 2 1; 1010 to 1111 are never used
o₂ × 64 + o₁ × 8 + o₀ = NAn octal number's valuegroup the bits in threes from the right; each group is one digit o from 0 to 7
b XOR b′ → gBinary to Gray code, bit by bitkeep the first bit; each next Gray bit g is the binary bit b′ XOR the binary bit b to its left
b XOR g → b′Gray code to binary, bit by bitkeep the first bit; each next binary bit b′ is the binary bit b just written XOR the next Gray bit g
2ⁿ = PThe patterns n bits can holdN values need the smallest n with 2ⁿ at least N
y = x × 2ᵏ, y = x ÷ 2ᵏA shift by k placesleft doubles each place, right halves it, as long as no 1 falls off the end
−128 + … = −nThe value of an 8-bit two's complement bytethe top bit is worth −128 and the others their usual place values; to write −n, flip every bit of n and add 1
v_min = −2ᵏ, v_max = 2ᵏ − 1The range of n bits in two's complementk = n − 1; 8 bits hold −128 to 127
v = s − 256, v = 256 + sWhat an 8-bit adder shows after an overflows = the true sum outside −128 to 127; two positives that give a negative byte, or two negatives that give a positive one, overflowed
65 + k = C, C + 32 = c, 48 + k = cASCII codes of letters and digitsk = places after A, or after 0 for a digit; a small letter c is its capital C plus 32
s + 6 = s′The BCD correction in additionwhen a digit sum s is above 9, add 6 to that group and carry 1 to the next digit

Worked example

This example is made by the same code as the practice questions. Open the working to see every step.

An 8-bit adder adds 96 and 81, both in two’s complement. What value does the 8-bit result show?

Two bytes in two’s complement added in an 8-bit adderLabels shown, in drawing order: 96, −128, 0, 64, 1, 32, 1, 16, 0, 8, 0, 4, 0, 2, 0, 1, 0, 81, +, 0, 1, 0, 1, 0, 1, ?.96−12806413211608040201081+01010001????????
Show the working
  1. Write down what is given: 96 and 81 in 8-bit two’s complement.
  2. An 8-bit two’s complement byte holds −128 to 127.
  3. Work it out: 96 + 81 = 177.
  4. That is outside the range, so the sum overflows and the byte wraps round by 256: v = 177 − 256 = −79.
  5. Check: two positive numbers gave a negative byte, the sign of an overflow. Answer: −79.
Why not the others?
  • 49 That reads only the low 7 bits and leaves out the top bit, which is worth −128.

Remember: A sum that falls outside what eight bits can hold in two’s complement overflows and wraps round. Formula sheet

Common mistakes

These are the wrong answers the practice questions offer, and the slip that leads to each.

Questions

What is the formula for a BCD code's value?

d₂ × 100 + d₁ × 10 + d₀ = N. Symbols: each group of 4 bits is one digit d from 0 to 9, read with place values 8 4 2 1; 1010 to 1111 are never used.

What is the formula for an octal number's value?

o₂ × 64 + o₁ × 8 + o₀ = N. Symbols: group the bits in threes from the right; each group is one digit o from 0 to 7.

What is the formula for binary to Gray code, bit by bit?

b XOR b′ → g. Symbols: keep the first bit; each next Gray bit g is the binary bit b′ XOR the binary bit b to its left.