Binary Codes

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Page 1 of 12 SPH 3119 – Digital Systems and Circuits, Lecture Notes Jan – Apr 2020

 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.

o Straight binary code


→ Represents decimal numbers using the natural binary form.
→ Example: represent 11.312510 in straight binary.
Integer part Fractional part
quotient remainder product fraction carry
11 ÷ 2 = 5 1 0.3125 × 2 = 0.625 0625 0
5÷2 = 2 1 0.625 × 2 = 1.25 0.25 1
2÷2 = 1 0 0.25 × 2 = 0.5 0.5 0
1÷2 = 0 1 0.5 × 2 = 1.0 0 1
∴ (11)10 = (1011)2 ∴ (0.3125)10 = (0.0101)2

⇒ (11.3125)10 = (1011.0101)2

o Binary coded decimal (BCD code)


→ Instead of converting the whole decimal number into binary (as is the case with
straight binary), the individual digits in a decimal number are assigned an
equivalent 4-bit binary code.
→ Example: represent 11.312510 in BCD.
1 1 . 3 1 2 5
0001 0001 . 0011 0001 0010 0101

⇒ (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

→ Limitations of the BCD


− In converting a BCD code into its decimal number equivalent, a situation may
arise where a 4-bit binary code gives an equivalence of a decimal number that
cannot be represented in BCD such as 10, 11, 12, 13, 14, and 15.
Example: convert 011111000001BCD to its decimal equivalent.
0111 𝟏𝟏𝟎𝟎 0001
7 ? 1
But, 𝟏𝟏𝟎𝟎 does not exist in the BCD code.

− 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

ii. 510 + 510


BCD code for 510 : 0 1 0 1
BCD code for 510 : 0 1 0 1 +
Sum → 1 0 1 0 ≡ 1010 → not a BCD digit

→ Work around to the problem observed in ii above:


1. Adding 610 (≡ 01102 ) to the sum obtained i.e.
510 + 510
BCD code for 510 : 0 1 0 1
BCD code for 510 : 0 1 0 1 +
Sum → 1 0 1 0
Add 6: 0 1 1 0 +
𝟏 0 0 0 0
Take carry bit to the next BCD digit
∴ 510 + 510 = 0001 0000𝐵𝐶𝐷

2. Using the excess-3 code.

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

o Excess-3 code (XS-3 code)


→ Addresses the limitation observed in BCD arithmetic – when adding two
decimal digits whose sum exceeds 9.
→ 310 (00112) is added to each of the digits in a decimal number and the resulting
digits are assigned an equivalent 4-bit binary code.
Example: represent 11.312510 in XS-3.

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

→ NB: XS-3 code is a non-weighted code.

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.

→ Also called the reflected binary code:

→ Applications of the Gray code


1. In the transmission of digital signals since it minimizes the occurrence of
errors during transmission.
Illustration: In a Gray code sequence, only one bit changes when transiting
from one number to another (either before or after).
Example: consider decimal numbers 3 and 4.

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.

→ Binary to Gray code conversion


Procedure Illustrative example
1. The first msb of the Gray code equivalent is the same as Binary 1011
the first msb of the given binary number. Gray code 1- - -

2. The second msb of the Gray code is obtained by adding


Binary 1011
the first msb and the second msb in the binary number
Gray code 11- -
and ignoring a carry, if any.

3. The third msb of the Gray code is obtained by adding


Binary 1011
the second msb and the third msb in the binary number
Gray code 111-
and ignoring a carry, if any.

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

This is equivalent to the truth table of an XOR operation.

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)

→ Gray code to binary conversion


Example: convert the Gray code number 1110 into binary.

b(1) = g(1)
b(2) = b(1) ⊕ g(2)
b(3) = b(2) ⊕ g(3)
b(4) = b(3) ⊕ g(4)

→ NB: Gray code is a non-weighted code.

→ Exercise: software code implementation of the Gray code.

Decimal digit 4-bit straight binary code BCD code XS-3 code Gray code

0 0000 0000 0011 0000

1 0001 0001 0100 0001

2 0010 0010 0101 0011

3 0011 0011 0110 0010

4 0100 0100 0111 0110

5 0101 0101 1000 0111

6 0110 0110 1001 0101

7 0111 0111 1010 0100

8 1000 1000 1011 1100

9 1001 1001 1100 1101

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

o American Standard Code for Information Interchange (ASCII)


→ The industry standard alphanumeric code.
→ Encodes the following characters:
− upper-case and lower-case letters (A to Z and a to z respectively)
− numerals 0 to 9
− special characters (@, ", #, $, *, +, ; , ?, &, space, etc)

→ 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).

→ Other not commonly used character-encoding schemes include:


i. The EBCDIC code
− An 8-bit alphanumeric code (can represent 256 possible characters).
− Mostly used in large IBM computers.
ii. The UNICODE code
− A 16-bit alphanumeric code (can represent 65,536 possible characters).
− Still under development.

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

Illustrative example: Consider the binary data (1000010)ASCII i.e. uppercase


B.
Even parity coded ASCII B = 01000010 (where 0 has been added as msb).
Odd parity coded ASCII B = 11000010 (bit 1 has been added as msb).

→ 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.

→ Limitations of parity checking:


i. It cannot identify the corrupted bit.
ii. The parity bit may as well be corrupted.
iii. If multiple bits are corrupted, the resulting parity status may be the same as
that with which it was sent. This may lead to a wrong conclusion.

→ Errors in digital signals occur during transmission due to noise or other


impairments. Parity checking can only detect an error, but it cannot localize to
identify the error bit.

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).

Code word sequence 𝑷𝟏 𝑷𝟐 𝐷1 𝑷𝟑 𝐷2 𝐷3 𝐷4


Data bits without parity 0 1 1 0
𝑃1 and allotted data bits 1 0 1 0
𝑃2 and allotted data bits 1 0 1 0
𝑃3 and allotted data bits 0 1 1 0
Data bits with parity 1 1 0 0 1 1 0
Thus, the Hamming code for 0110 with even parity is 1100110

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

The developed Hamming code is sent with even parity.


On the receiving end this code has to be checked for errors, the corrupted error
bit has to be localized and corrected.

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:

Transmission end - generating the Hamming code to be transmitted. Hamming


(11, 7) code ⇒ code word length is 11 bits, the message is 7 bits and 4 parity bits.
Thus,

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

Code word sequence 𝑷𝟏 𝑷𝟐 𝐷1 𝑷𝟑 𝐷2 𝐷3 𝐷4 𝑷𝟒 𝐷5 𝐷6 𝐷7


Data bits without parity 1 0 0 0 0 0 1
Data bits with parity 0 1
0 1 0 0
𝑃1
Data bits with parity 0 1
0 1 0 0
𝑃2
Data bits with parity
0 0 0 0
𝑃3
Data bits with parity 1 0 0 1
𝑃4
Data bits without parity 0 0 1 0 0 0 0 1 0 0 1
Thus, the transmitted Hamming code for 1000001 with even parity is
00100001001

Receiving end – detecting an error, localizing it and correcting it.


The received Hamming code is 00100101001.
Received code word
𝑷𝟏 𝑷𝟐 𝐷1 𝑷𝟑 𝐷2 𝐷3 𝐷4 𝑷𝟒 𝐷5 𝐷6 𝐷7 Parity status
sequence
Data bits without parity 0 0 1 0 0 1 0 1 0 0 1
Data bits with parity 𝑃1 0 1 0 0 0 1 even = 0
Data bits with parity 𝑃2 0 1 1 0 0 1 odd = 1
Data bits with parity 𝑃3 0 0 1 0 odd = 1
Data bits with parity 𝑃4 1 0 0 1 even = 0
1 2
Adding the check digit values for the disturbed lines gives 2 + 2 = 6.

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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy