Computer Codes Blymo tech
Computer Codes
Blymo tech
EE201: Digital Circuits and Systems
Section 2 – Computer Codes
2.1 Binary Coded Decimal (BCD):
Definition
Widely used representation of numerical data in which each digit of a decimal number is represented by a 4-bit binary number
For N-digit decimal numbers, their BCD representations require 4 * N bits
Decimal BCD
N B0 B1 B2 B3
0 0 0 0 0
1 0 0 0 1
2 0 0 1 0
3 0 0 1 1
4 0 1 0 0
5 0 1 0 1
6 0 1 1 0
7 0 1 1 1
8 1 0 0 0
9 1 0 0 1
Example
2 5 9
0010 0101 1001
Decimal to BCD Encoder
10 inputs and 4 outputs
Homework: Implement the encoder using gates
S0 S1 S2 S3 S4 S5 S6 S7 S8 S9 B0 B1 B2 B3
1 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0 0 0 0 1
0 0 1 0 0 0 0 0 0 0 0 0 1 0
0 0 0 1 0 0 0 0 0 0 0 0 1 1
0 0 0 0 1 0 0 0 0 0 0 1 0 0
0 0 0 0 0 1 0 0 0 0 0 1 0 1
0 0 0 0 0 0 1 0 0 0 0 1 1 0
0 0 0 0 0 0 0 1 0 0 0 1 1 1
0 0 0 0 0 0 0 0 1 0 1 0 0 0
0 0 0 0 0 0 0 0 0 1 1 0 0 1
S0
Enc S1
S9
B0
B3
BCD to Decimal Decoder
4 inputs and 10 outputs
Homework: Implement the decoder using gates
B0 Dec
B1
B2
S0
S9 B3
Inputs Outputs
B0 B1 B2 B3 S0 S1 S2 S3 S4 S5 S6 S7 S8 S9
0 0 0 0 1 0 0 0 0 0 0 0 0 0
0 0 0 1 0 1 0 0 0 0 0 0 0 0
0 0 1 0 0 0 1 0 0 0 0 0 0 0
0 0 1 1 0 0 0 1 0 0 0 0 0 0
0 1 0 0 0 0 0 0 1 0 0 0 0 0
0 1 0 1 0 0 0 0 0 1 0 0 0 0
0 1 1 0 0 0 0 0 0 0 1 0 0 0
0 1 1 1 0 0 0 0 0 0 0 1 0 0
1 0 0 0 0 0 0 0 0 0 0 0 1 0
1 0 0 1 0 0 0 0 0 0 0 0 0 1
2.2 Text Coding
Characters o General term used for letters, digits and punctuation
o Each character is assigned a unique numerical code
o How many codes are needed?
o Digits (10): 0, 1, 2, 3,…, 9
o Lower case letters (26): a, b, c,…, z
o Upper case letters (26): A, B, C,…, Z
o Punctuation (16): . , ; : ? ! “ ‘ ` [ ] ( )
o Special characters (18): # $ € ¥ £ % ^ & * + - = \ / < > | ~
o Other characters: § © ® ¶ ± ¢ ¼
o Characters for other languages: ß â ç ê ţ Њ љ Ω פ
ASCII code
o 7 bits allocated to store each code
o 27 = 128 codes for 128 possible characters
o Covers digits, lower and upper case letters,
punctuation, special and other characters
o Does not cover characters for other languages (accents, umlauts, fadas)
0 1 2 3 4 5 6 7 8 9 A B C D E F
0 NUL SOH STX ETX EOT ENQ ACK BEL BS HT LF VT FF CR SO SI
1 DLE DC1 DC2 DC3 DC4 NAK SYN ETB CAN EM SUB ESC FS GS RS US
2 SP ! " # $ % & ' ( ) * + , - . /
3 0 1 2 3 4 5 6 7 8 9 : ; < = > ?
4 @ A B C D E F G H I J K L M N O
5 P Q R S T U V W X Y Z [ \ ] ^ _
6 ` a b c d e f g h i j k l m n o
7 p q r s t u v w x y z | ~ DEL
Extended ASCII o Specified by International Standards Organization-ISO
o A subset of ISO-8859 that includes several sets of
characters for writing in Cyrillic, Arabic, Hebrew, etc.
o Extends ASCII, including additional characters used in
some West European languages such as Irish, French
and German
o One byte (8 bits) allocated to store each code
o 28 = 256 codes for 256 possible characters
o Text encoded in Extended ASCII can be transmitted
through e-mail and be printed on any computer
system, being accepted as basis for every text file
formats
o E.g. ‘a’ <=> 9710 = 11000012 = 6116
Decimal 9 7
Binary 0 1 1 0 0 0 0 1
Hexadecimal 6 1
Words (Strings) o A word is represented as a sequence of 0 or more
characters in form of a string or an array of characters
o E.g. “ee201” = [‘e’, ‘e’, ‘2’, ‘0’, ‘1’] ASCII = [101, 101, 50,
48, 49] 10 = [65, 65, 32, 30, 31] 16
Binary 0 1 1 0 0 1 0 1 Hex 6 5
Binary 0 1 1 0 0 1 0 1 Hex 6 5
Binary 0 0 1 1 0 0 1 0 Hex 3 2
Binary 0 0 1 1 0 0 0 0 Hex 3 0
Binary 0 0 1 1 0 0 0 1 Hex 3 1
2.3 Gray Codes:
Definition
Counting sequences that change only one bit between adjacent steps
Decimal Gray Code
N G0 G1 G2 G3
0 0 0 0 0
1 0 0 0 1
2 0 0 1 1
3 0 0 1 0
4 0 1 1 0
5 0 1 1 1
6 0 1 0 1
7 0 1 0 0
8 1 1 0 0
9 1 1 0 1
10 1 1 1 1
11 1 1 1 0
12 1 0 1 0
13 1 0 1 1
14 1 0 0 1
15 1 0 0 0
Binary to Gray Code Conversion Observations
The 1-st bit of the Gray code (the most significant) is the same as the 1-st bit of the corresponding binary code
The 2-nd bit of the Gray code is 1 if the 1-st and the 2-nd bit of the corresponding binary code are different and 0 if they are the same
…
The N-th bit of the Gray code is 1 if the (N-1)-th and the N-th bit of the corresponding binary code are different and 0 if they are the same
Example
1 0 1 1 0
1 1 1 0 1
Rules:
G0 = B0; GN = BN-1 BN
Implementation: B 0
G 0
B 1 B 2 B 3 B 4
G 1 G 2 G 3 G 4
Binary Number Gray Code
B0 B1 B2 B3 G0 G1 G2 G3
0 0 0 0 0 0 0 0
0 0 0 1 0 0 0 1
0 0 1 0 0 0 1 1
0 0 1 1 0 0 1 0
0 1 0 0 0 1 1 0
0 1 0 1 0 1 1 1
0 1 1 0 0 1 0 1
0 1 1 1 0 1 0 0
1 0 0 0 1 1 0 0
1 0 0 1 1 1 0 1
1 0 1 0 1 1 1 1
1 0 1 1 1 1 1 0
1 1 0 0 1 0 1 0
1 1 0 1 1 0 1 1
1 1 1 0 1 0 0 1
1 1 1 1 1 0 0 0
Gray Code to Binary Conversion
The 1-st bit of the binary code (the most significant) is the same as the 1-st bit of the corresponding Gray code The 2-nd bit of the binary code is 1 if the 1-st bit of the binary code and the 2-nd bit of the Gray code are different and 0 if they are the same
… The N-th bit of the binary code is 1 if the (N-1)-th bit of the binary code and the N-th bit of the Gray code are different and 0 if they are the same
Example
1 1 1 0 1
1 0 1 1 0
Rules: B0 = G0; BN = BN-1 GN
G 0
B 0
G 1 G 2 G 3 G 4
B 1 B 2 B 3 B 4
2.4 Parity Codes:
Motivation
When transmitting or storing data, errors can occur and it is highly significant to be able to detect them
Definition
Parity codes are binary sequences resulted from the original sequence of bits (data in binary representation) after the addition of a single parity bit
Types
Even Parity
The even parity bit is chosen such as the total number of ‘1’ bits in the sequence is even e.g.:
Odd Parity
The parity bit is such set that the total number of bits equal to ‘1’ in the sequence is odd
e.g.:
1 0 1 1 1 0 1 1
1 0 1 0 1 0 0 0
Even
parity bit
Odd parity bit
Examples
Even Parity
o E.g. ‘a’ <=ASCII=> 9710 = 11000012
o E.g. 1110100111000012
Odd Parity
o E.g. ‘a’ <=ASCII=> 9710 = 11000012
ASCII Decimal 9 7
ASCII Binary 1 1 0 0 0 0 1
ASCII Hexa 6 1
Even Parity Code 1 1 1 0 0 0 0 1
Even Parity Code Hex E 1
Binary 1 1 1 0 1 0 0 1 1 1 0 0 0 0 1
Hexa 7 4 E 1
Even Parity Code 0 1 1 1 0 1 0 0 1 1 1 0 0 0 0 1
Even Parity Code Hex 7 4 E 1
ASCII Decimal 9 7
ASCII Binary 1 1 0 0 0 0 1
ASCII Hex 6 1
Even Parity Code 0 1 1 0 0 0 0 1
Even Parity Code Hex 6 1
Even
parity bit
Even
parity bit
Odd
parity bit
Application
Transmission Error Detection
P a r ity
G e n e ra to rP a r ity
C h e c k e r
A
B
C
D
A
B
C
D
P a r ity B itE rro r
In d ic a to r
Notes:
o Using a single parity bit, errors in transmission of binary data sequences (words) can be detected
o Only singular errors can be detected
(errors that affect only one bit) or multiple errors if odd number of bits are affected
o This setup cannot detect where the error
takes place and therefore it cannot be corrected
Error Correction:
o Using a matrix-like setup that makes use
of two sets of parity bits: on each word and on corresponding bits from the run of words respectively can detect and correct singular errors
H Parity S0 S1 S2 S3 S4 S5 S6 S7 Value
0 0 1 0 0 0 0 0 1 ‘A’
0 0 1 0 0 0 0 1 0 ‘B’
1 0 1 0 0 0 0 1 1 ‘C’
0 0 1 0 0 0 1 0 0 ‘D’
1 0 1 0 0 0 1 0 1 ‘E’
1 0 1 0 0 0 1 1 0 ‘F’
0 0 1 0 0 0 1 1 1 ‘G’
0 0 1 0 0 1 0 0 0 ‘H’
1 0 0 0 0 1 0 0 0 V Parity
Note: Even parity was used
2.5 Hamming Code:
Motivation
Hamming code enables single errors to be detected and corrected
Description
Employs P parity bits for M data bits, where:
Parity bits are placed in positions power of 2, between data bits:
P1, P2, P4, P8, P16, …
Each parity bit checks those data bits located in positions that, expressed in binary, have ‘1’-s on columns that correspond to that parity bit
Bit position 8 4 2 1 Parity bit
1 0 0 0 1 P1
2 0 0 1 0 P2
3 0 0 1 1 P1, P2
4 0 1 0 0 P4
5 0 1 0 1 P1, P4
6 0 1 1 0 P2, P4
7 0 1 1 1 P1, P2, P4
8 1 0 0 0 P8
9 1 0 0 1 P1, P8
10 1 0 1 0 P2, P8
12 MPP
Therefore parity bits check the following: P1 -> P1, D3, D5, D7, D9, …
P2 -> P2, D3, D6, D7, D10, D11, …
P4 -> P4, D5, D6, D7, D12, D13, D14, D15, …
P8 -> P8, D9, D10, D11, D12, D13, D14, D15, …
Encoding and Transmission
Count number of data bits: M
Determine number of parity bits P, such as:
12 MPP
For each of the P parity bits list the data bits checked by it and determine its value based on the values of the data bits and of the even or odd parity used
Place the parity bits between data bits in their locations forming a Hamming coded sequence:
P1, P2, D3, P4, D5, D6, D7, P8, D9, D10, D11, …
Transmit the Hamming coded sequence of bits
Detection and Correction
Compute the number of correction bits P given N the length of the Hamming coded sequence, such as:
12 NP
Compute the value of the correction bits taking into account the fact that even or odd parity is employed
Each correction bit checks the same bits from the code as the parity bits used during encoding
C1 -> P1, D3, D5, D7, D9, …
C2 -> P2, D3, D6, D7, D10, D11, …
C4 -> P4, D5, D6, D7, D12, D13, D14, D15, …
C8 -> P8, D9, D10, D11, D12, D13, D14, D15, …
…
Computes the detection code:
… C8 C4 C2 C1
This code expressed in binary indicates the position of the bit affected by an eventual singular error and can be used for the correction
Correction is performed by inverting the value of the error bit
Extract original data
Example Encoding and Transmission
Data bits: 1010
Count number of data bits: M = 4
Number of parity bits P = 3, such as:
814323
Hamming coded sequence:
P1 P2 D3 P4 D5 D6 D7
1 0 1 0
C8 C4 C2 C1 Error bit
0 0 0 0 None
0 0 0 1 P1
0 0 1 0 P2
0 0 1 1 D3
0 1 0 0 P4
0 1 0 1 D5
0 1 1 0 D6
0 1 1 1 D7
1 0 0 0 P8
1 0 0 1 D9
1 0 1 0 D10
Compute the P parity bits based on even parity: P1 -> P1, D3, D5, D7 <=> P1, 1, 0, 0 => P1 = 1
P2 -> P2, D3, D6, D7 <=> P2, 1, 1, 0 => P2 = 0
P4 -> P4, D5, D6, D7 <=> P4, 0, 1, 0 => P4 = 1
Place the parity bits between data bits in their locations forming a Hamming coded sequence:
Transmit the Hamming coded sequence of bits
Error Detection and Correction
Assume received code: 1001010
Length of the Hamming coded sequence N = 7
Number of correction bits: P = 3, such as:
81223
NP
P1 P2 D3 P4 D5 D6 D7
1 0 1 1 0 1 0
P1 P2 D3 P4 D5 D6 D7
1 0 0 1 0 1 0
Compute the value of the correction bits taking into account the even parity used:
C1 -> P1, D3, D5, D7 <=> 1, 0, 0, 0 => C1 = 1
C2 -> P2, D3, D6, D7 <=> 0, 0, 1, 0 => C2 = 1
C4 -> P4, D5, D6, D7 <=> 1, 0, 1, 0 => C4 = 0
Computes the detection code:
C4 C2 C1 <=> 0 1 1 2 = 3 10 => Error bit D3
Perform the correction by inverting the value of the error bit
D3 = 1
Extract the original data:
D3 D5 D6 D7 <=> 1 0 1 0
Implementation
P 1
P 2
D 3
P 4
D 5
D 6
D 7
C 1
C 2
C 4