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
| Formula | What it gives | Symbols |
|---|---|---|
d₂ × 100 + d₁ × 10 + d₀ = N | A BCD code's value | 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 |
o₂ × 64 + o₁ × 8 + o₀ = N | An octal number's value | group the bits in threes from the right; each group is one digit o from 0 to 7 |
b XOR b′ → g | Binary to Gray code, bit by bit | keep 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 bit | keep the first bit; each next binary bit b′ is the binary bit b just written XOR the next Gray bit g |
2ⁿ = P | The patterns n bits can hold | N values need the smallest n with 2ⁿ at least N |
y = x × 2ᵏ, y = x ÷ 2ᵏ | A shift by k places | left doubles each place, right halves it, as long as no 1 falls off the end |
−128 + … = −n | The value of an 8-bit two's complement byte | the 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ᵏ − 1 | The range of n bits in two's complement | k = n − 1; 8 bits hold −128 to 127 |
v = s − 256, v = 256 + s | What an 8-bit adder shows after an overflow | s = 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 = c | ASCII codes of letters and digits | k = 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 addition | when 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?
Answer: −79. The true sum is 177. That is outside −128 to 127, so the byte overflows and shows −79.
Show the working
- Write down what is given: 96 and 81 in 8-bit two’s complement.
- An 8-bit two’s complement byte holds −128 to 127.
- Work it out: 96 + 81 = 177.
- That is outside the range, so the sum overflows and the byte wraps round by 256: v = 177 − 256 = −79.
- 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.
- Writing the whole number in plain binary for BCD: 47 in BCD is 0100 0111, not 0010 1111.
- Flipping the bits but forgetting to add 1 when writing a negative number in two's complement.
- Reading a Gray code as plain binary: change it to binary first, bit by bit.
- Taking 2ⁿ − 1 as the largest two's complement value: the top bit is the sign, so 8 bits only reach 127.
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.