Sub topic 1
Number Systems, 2's Complement and Codes (BCD, Gray)
Digital circuits store and process bit patterns. A bit pattern has meaning only after a representation is chosen. This chapter assumes binary and hexadecimal notation, basic Boolean operations, and elementary algebra.
Prerequisites
You should be able to convert small numbers between binary, decimal, and hexadecimal, and understand bitwise AND, XOR, and NOT.
Positional number systems
In base \(r\), each position has a weight that is a power of \(r\). For example,
\[ (101101)2 = 1\cdot2^5+0\cdot2^4+1\cdot2^3+1\cdot2^2+0\cdot2^1+1\cdot2^0=45{10}. \]
Hexadecimal groups binary digits in sets of four. Thus,
\[ (1011\ 0110)2 = (B6){16}. \]
For a fractional value, positions to the right of the radix point have negative powers. For example, \((0.101)_2=1/2+1/8=0.625\).
Signed numbers with 2's complement
For an \(n\)-bit 2's-complement number, the most significant bit has a negative weight:
\[ b{n-1}2^{n-1}(-1)+\sum{i=0}^{n-2}b_i2^i. \]
The representable range is
\[ -2^{n-1}\ \text{through}\ 2^{n-1}-1. \]
To find the negative of a positive number:
- Invert every bit.
- Add 1.
For example, using eight bits:
\[ +37=00100101 \]
\[ -37=\text{NOT}(00100101)+1=11011010+1=11011011. \]
To decode 11011011, invert it, add 1, and attach a minus sign:
\[ \text{NOT}(11011011)+1=00100101=37. \]
The all-zero pattern represents zero. The pattern 10000000 represents \(-128\) in an 8-bit system. It has no positive counterpart because the negative range is larger by one value.
When increasing the width of a signed number, use sign extension: copy the sign bit. For example, 8-bit 11111011 is \(-5\), and its 16-bit form is 11111111 11111011. Zero extension would change its value.
def twos_complement(value, bits):
mask = (1 << bits) - 1
return value & mask
x = -37
encoded = twos_complement(x, 8)
print(f"{encoded:08b}") # 11011011BCD representation
Binary-coded decimal (BCD) stores each decimal digit separately in four bits. Only 0000 through 1001 are valid digit codes.
The decimal number 59 is therefore:
\[ 59{10}\rightarrow 0101\ 1001{\text{BCD}}. \]
This differs from ordinary binary:
\[ 59{10}=1110112. \]
BCD simplifies decimal display hardware, but uses more bits. The six patterns from 1010 through 1111 are invalid for a BCD digit. In BCD addition, if a four-bit digit exceeds 9 or produces a carry, add 0110 to correct it.
Gray code
Gray code represents adjacent values so that only one bit changes between consecutive values. For binary value \(B\), the corresponding Gray value is
\[ G=B\oplus(B\mathbin{>>}1). \]
For 1011:
\[ 1011\oplus0101=1110. \]
To convert Gray code back to binary, copy the most significant bit, then repeatedly XOR the previous binary bit with the current Gray bit.
Gray code is useful in rotary encoders and asynchronous counters because a transition changes only one wire. It does not make arithmetic easier than binary.
Common errors
- Converting a decimal number directly to BCD as if BCD were ordinary binary.
- Forgetting that 2's-complement width determines the range.
- Zero-extending a negative signed value.
- Treating invalid BCD nibbles as decimal digits.
- Assuming Gray code bits can be added like binary bits.
Exercise
Represent -18 in 8-bit 2's complement, and represent decimal 46 in BCD.
Show answer
+18 is 00010010. Invert and add one: 11101101 + 1 = 11101110, so -18 is 11101110. Decimal 46 in BCD is 0100 0110.