Binary Codes
Binary Codes
Binary Codes
Binary codes
o Represent numeric and non-numeric data as a string of bits (i.e. as a code).
→ Numeric data represented in straight binary code, BCD code, XS-3 code, Gray
code.
→ Both numeric and non-numeric (letters of the alphabet and special characters)
data represented in alphanumeric codes such as ASCII, EBCDIC and
UNICODE.
o Used to detect and correct errors in digital data transmission.
⇒ (11.3125)10 = (1011.0101)2
⇒ (11.3125)10 = (00010001.0011000100100101)𝐵𝐶𝐷
→ NB:
1. Though BCD allows easy conversion of decimal numbers, it is inefficient in
that it represents a decimal number using a larger number of bits compared
to straight binary.
2. BCD is also referred to as the 8421 weighted code.
NB: Supplement your learning from the lecture and lecture notes by independent study and discussions.
Page 2 of 12 SPH 3119 – Digital Systems and Circuits, Lecture Notes Jan – Apr 2020
− BCD arithmetic fails when adding two decimal digits whose sum exceeds 9.
Example: using BCD perform the operations:
i. 410 + 110
BCD code for 410 : 0 1 0 0
BCD code for 110 : 0 0 0 1 +
Sum → 0 1 0 1 ≡ BCD code for 510
NB: Supplement your learning from the lecture and lecture notes by independent study and discussions.
Page 3 of 12 SPH 3119 – Digital Systems and Circuits, Lecture Notes Jan – Apr 2020
1 1 . 3 1 2 5
+ + + + + +
3 3 3 3 3 3
4 4 6 4 5 8
0100 0100 . 0110 0100 0101 1000
⇒ (11.3125)10 = (01000100.0110010001011000)𝑋𝑆−3
→ With the XS-3 code the arithmetic operation 510 + 510 becomes:
XS-3 code for 510 1 0 0 0
XS-3 code for 510 1 0 0 0 +
1 0 0 0 0
∴ 510 + 510 = 0001 0000𝑋𝑆−3
o Gray code
→ Binary codes for consecutive numbers vary in one bit position only.
→ 1-bit, 2-bit and 3-bit Gray code lists:
Decimal 1-bit 2-bit 3-bit
0 0 00 000
1 1 01 001
2 11 011
3 10 010
4 110
5 111
6 101
7 100
NB: Supplement your learning from the lecture and lecture notes by independent study and discussions.
Page 4 of 12 SPH 3119 – Digital Systems and Circuits, Lecture Notes Jan – Apr 2020
→ Generating an n-bit Gray code list from an (n-1)-bit Gray codes list:
Steps:
1. Let the list of (n-1)-bit Gray codes be L1. Create another list L2 which is
reverse of L1.
2. Modify the list of L1 by prefixing a ‘0’ in all codes of L1.
3. Modify the list of L2 by prefixing a ‘1’ in all codes of L2.
4. Concatenate L1 and L2. The concatenated list is the required list of n-bit Gray
codes.
Example: Generating a 3-bit Gray code list from a 2-bit Gray code list.
i. L1={00, 01, 11, 10} (list of 2-bit Gray codes)
L2={10, 11, 01, 00} (reverse of L1)
ii. Prefix all entries of L1 with ‘0’, L1 becomes {000, 001, 011, 010}
iii. Prefix all entries of L2 with ‘1’, L2 becomes {110, 111, 101, 100}
iv. Concatenate L1 and L2, to get {000, 001, 011, 010, 110, 111, 101, 100}
→ Generally, to generate n-bit Gray codes, start from the list of 1-bit Gray codes.
Perform the procedure described above to generate 2-bit Gray codes from 1-bit
Gray codes, then 3-bit Gray codes from 2-bit Gray codes till the number of bits
equal to n.
NB: Supplement your learning from the lecture and lecture notes by independent study and discussions.
Page 5 of 12 SPH 3119 – Digital Systems and Circuits, Lecture Notes Jan – Apr 2020
When converted to binary, this gives 0011 and 0100 for decimal 3 and 4
respectively. When transiting from 0011 to 0100 (or 0100 to 0011), three bits
change.
When converted to Gray code, this gives 0010 and 0110 for decimal 3 and 4
respectively. When transiting from 0010 to 0110 (or 0110 to 0010), only one
bit changes.
The probability of errors occurring is higher in the case of three bits change
compared to that of a single bit change.
2. In the minimization of Boolean functions (and hence logic circuits) -
Karnaugh maps.
3. Etc.
4. The process continues until the lsb of the Gray code is Binary 1011
obtained. Gray code 1110
Note: the addition of a bit with another and ignoring the carry results in the
following possibilities:
augend addend sum (ignore carry)
0 0 0
0 1 1
1 0 1
1 1 0
If b(1), b(2), b(3) and b(4) are the bits in the binary number, where b(1) is the msb
and b(4) is the lsb, the conversion of a binary number into a Gray code number
NB: Supplement your learning from the lecture and lecture notes by independent study and discussions.
Page 6 of 12 SPH 3119 – Digital Systems and Circuits, Lecture Notes Jan – Apr 2020
can be summarized as shown below, where g(1), g(2), g(3) and g(4) are the bits
in the Gray code number, g(1) is the msb and g(4) is the lsb:
g(1) = b(1)
g(2) = b(1) ⊕ b(2)
g(3) = b(2) ⊕ b(3)
g(4) = b(3) ⊕ b(4)
b(1) = g(1)
b(2) = b(1) ⊕ g(2)
b(3) = b(2) ⊕ g(3)
b(4) = b(3) ⊕ g(4)
Decimal digit 4-bit straight binary code BCD code XS-3 code Gray code
NB: Supplement your learning from the lecture and lecture notes by independent study and discussions.
Page 7 of 12 SPH 3119 – Digital Systems and Circuits, Lecture Notes Jan – Apr 2020
→ ASCII comes in two variants, ASCII-7 (7-bit code, can encode up to 128
i.e. 27 unique characters) and ASCII-8 (8-bit code, can encode up to 256
unique characters).
→ Table below – US ASCII-7 code:
NB: Supplement your learning from the lecture and lecture notes by independent study and discussions.
Page 8 of 12 SPH 3119 – Digital Systems and Circuits, Lecture Notes Jan – Apr 2020
→ ASCII examples:
1. ASCII encoding of 23
011 0010 011 0011
2. ASCII encoding of BAD
1000010 1000001 1000100
NB:
− The 8th bit is used for error coding (referred to as a parity bit).
− Other ASCII-7 and ASCII-8 variants have been developed to include non-
English letters (e.g. ä, ü, etc) and symbols (e.g. €, ¥, etc).
o Parity codes
→ Parity code – the data string that results after a single bit (called parity bit) is
included as msb in the data being transmitted in an attempt to detect an error in
case it occurs during transmission.
→ The assigned parity bit gives the data being transmitted a unique characteristic
such that,
− The data is said to have an even parity status if, after including the parity bit,
the number of ones is even.
− The data is said to have an odd parity status if, after including the parity bit,
the number of ones is odd.
NB: Supplement your learning from the lecture and lecture notes by independent study and discussions.
Page 9 of 12 SPH 3119 – Digital Systems and Circuits, Lecture Notes Jan – Apr 2020
→ On the receiving end the parity status of the received code is checked – if it is
same as that with which it was sent, then it is assumed to be okay i.e. it was not
corrupted during transmission.
o Hamming codes
→ Hamming code – the data string that results after several parity bits are included
in a message data being transmitted in an attempt to detect, localize and hence
correct an error in case it occurs during transmission.
→ Parity bits are placed at all bit positions that are powers of 2 i.e. positions 1, 2,
4, 8, 16, … and all the other bit positions (positions 3, 5, 6, 7, 9, 10, 11, …) are
data bits.
→ The resulting code is referred to as the Hamming code and it takes the following
form:
𝑷𝟏𝑷𝟐𝐷1𝑷𝟑𝐷2𝐷3𝐷4𝑷𝟒𝐷5𝐷6𝐷7𝐷8𝐷9𝐷10𝐷11𝑷𝟓 …,
Where, 𝑃 ≡ parity bits, and 𝐷 ≡ data bits.
→ Each parity bit is allotted a group of data bits such that if 𝑛 is the position of
the parity bit, a group is formed by,
i. Firstly, checking 𝑛 − 1 bits.
ii. Alternately skipping and checking 𝑛 bits following the parity bit.
Thus,
NB: Supplement your learning from the lecture and lecture notes by independent study and discussions.
Page 10 of 12 SPH 3119 – Digital Systems and Circuits, Lecture Jan – Apr 2020
Notes
𝑷𝟏𝐷1𝐷2𝐷4𝐷5𝐷7𝐷9𝐷11...
𝑷𝟐𝐷1𝐷3𝐷4𝐷6𝐷7𝐷10𝐷11 …
etc
→ The value of the parity bit (0 or 1) is used to give its group an even or odd
parity status.
→ Illustrative example: develop the Hamming code, with even parity, for a four-
bit message 0110 (≡ 610).
NB: The parity status for all the groups is even i.e.
− 𝑃1𝐷1𝐷2𝐷4 = 𝟏 0 1 0
− 𝑃2𝐷1𝐷3𝐷4 = 𝟏 0 1 0
− 𝑃3𝐷2𝐷3𝐷4 = 𝟎 1 1 0
This is an example of the so-called Hamming (7, 4) code. Meaning that the code
has a code word length of seven bits with four message bits (and of course three
parity bits).
Generally, the code word sequence for the Hamming (7, 4) code is written as
𝑃1𝑃2𝐷1𝑃3𝐷2𝐷3𝐷4, where 𝑃1, 𝑃2 and 𝑃3 are parity bits and 𝐷1, 𝐷2, 𝐷3 and
𝐷4 are data bits.
NB: Supplement your learning from the lecture and lecture notes by independent study and discussions.
Page 11 of 12 SPH 3119 – Digital Systems and Circuits, Lecture Jan – Apr 2020
Notes
→ Now, suppose that the data bit 𝐷1 gets corrupted during transmission so that
the received code is 1110110.
In order to detect the error, the parity is checked by checking the parity status
of the three groups. Thus,
𝑃1𝐷1𝐷2𝐷4 = 1110 ⇒ odd parity ⇒ parity disturbed
𝑃2𝐷1𝐷3𝐷4 = 1101 ⇒ odd parity ⇒ parity disturbed
𝑃3𝐷2𝐷3𝐷4 = 0110 ⇒ even parity ⇒ parity OK
The data was sent with even parity. However, we have a mixture of even and
odd parity. This means that there is an error in the received data.
Since the data was sent with even parity, this error has something to do with the
lines that have odd parity and the bit that has been corrupted is found by adding
the check digit values, i.e. 1 (20 ) and 2 (21 ), of the two lines together. Therefore,
1 + 2 = 3 ⟹ the bit corrupted is in the 3rd position from the msb.
In order to correct the error flip the bit in the 3rd digit (i.e. 1 → 0) to give the
original correct code 1100110.
Example:
Develop a Hamming (11, 7) code for the 7-bit ASCII code for letter ‘A’ i.e.
1000001. Use even parity.
Assuming that the data word gets corrupted to 1010001 in a transmission
channel, show how the Hamming code can be used to identify the error.
Solution:
NB: Supplement your learning from the lecture and lecture notes by independent study and discussions.
Page 12 of 12 SPH 3119 – Digital Systems and Circuits, Lecture Jan – Apr 2020
Notes
Bit corrupted is in the 6th position from the msb. Therefore the corrected
Hamming code becomes 00100001001.
Exercise:
i. The data 10101100011 has been sent with even parity. Check whether it is
correct.
ii. Detect and correct the error in the data 11111101000 sent with odd parity.
NB: Supplement your learning from the lecture and lecture notes by independent study and discussions.