Lecture 4
Lecture 4
Lecture 4
Design
Lecturer :
Engr Hasnain Ahmad
week No: 4
Different Logic Codes & their Arithmetic
(BCD – Excess-3 – Gray – ASCII Codes)
Logic Codes & their Arithmetic - Introduction
Besides the basic representations used for unsigned and signed
integers and fractions, many other binary codes also exist for
representing numbers, as well as letters, and all manner of other
things (colors, machine instructions, etc.) Other types of codes
are used to protect data from damage or eavesdropping
In this lecture we will cover a few of the most commonly
encountered codes: e.g.
BCD Code, Excess-3 Code, Gray code, ASCII Code
Introduction of some general concepts of error control codes and
Parity bits (Odd or Even) will also mentioned.
2
Logic Codes & their Arithmetic - Introduction
In computer engineering, digital telecommunications engineering,
and computer science, certain special numbers of bits that have
names commonly associated with them:
۞ 3 bits – An octal digit.
۞ 4 bits (1 hex digit) – A nibble (or nybble?).
۞ 8 bits (2 hex digits) – A byte (B), character, or octet.
۞ 16 bits (4 hex digits) – A half-word or a short int.
۞ 32 bits (8 hex digits) – A word or an int.
۞ 64 bits (16 hex digits) – A double-word or long int.
۞ 128 bits (32 hex digits) – A quad-word or long long int.
۞ 8,192 bits (2,048 hex digits) – A kilobyte (kB) or kibibyte (kiB).
And so forth,
۞with 1,024 KB = 1 MB, and 1,024 MB = 1 GB, etc. 3
Logic Codes & their Arithmetic - BCD Code
BCD Code is a way of representing decimal numbers directly
in binary.
۞The digits 0-9 are represented with their 4-bit equivalents
0000-1001 (same as hex digits 0-9).
۞However, codes 1010-1111 remain unused.
► The values of the digit positions are taken as powers of
10, rather than 16.
Example: 35810 = 0011 0101 1000 in BCD.
Table of
Signed Decimal Numbers
& their
BCD Equivalents
5
Logic Codes & their Arithmetic - BCD Code
Addition Process of BCD Code
Many computers and calculators use the BCD Code to represent decimal
numbers. As mentioned earlier that this code takes each decimal digit and
represent it by a four-bit code ranging from 0000 to 1001.
Subtract the two BCD numbers using the rules for binary
subtraction.
Example 1
45 0 1 0 0 0 1 0 1
+ 33 +0 0 1 1 0 0 1 1
78 0 1 1 1 1 0 0 0
Example 2
674 0 1 1 0 0 1 1 1 0 1 0 0
+ 325 +0 0 1 1 0 0 1 0 0 1 0 1
999 1 0 0 1 1 0 0 1 1 0 0 1
8
Logic Codes & their Arithmetic - BCD Code
Examples for Rule 2: Add 47 + 35 using BCD Addition.
Example 1
47 0 1 0 0 0 1 1 1
+ 35 + 0 0 1 1 0 1 0 1
82 0 1 1 1 1 1 0 0 Invalid Sum in 1st digit
+0 1 1 0 Add 6 to correct
1 0 0 0 0 0 1 0 Correct BCD Sum
Example 2
756 0 1 1 1 0 1 0 1 0 1 1 0
+ 245 + 0 0 1 0 0 1 0 0 0 1 0 1
1001 1 0 0 1 1 0 0 1 1 0 1 1 Invalid Sum in 1st digit
0 0 0 1+ 0 1 1 0 +0 1 1 0 +0 1 1 0 Add 6 in each bit to correct
0001 0 0 0 0 0 0 0 0 0 0 0 1 Correct BCD Sum 9
Logic Codes & their Arithmetic - BCD Code
Examples for Rule 2: Sub 86 - 24 using BCD Subtraction.
Example 1
86 1 0 0 0 0 1 1 0
- 24 - 0 0 1 0 0 1 0 0
82 0 1 1 0 0 0 1 0
Example 2
920 1 0 0 1 0 0 1 0 0 0 0 0
- 265 - 0 0 1 0 0 1 1 0 0 1 0 1
655 0 1 1 0 1 0 1 1 1 0 1 1 Invalid Sum in 1st digit
- 0 1 1 0 - 0 1 1 0 Add 6 in each bit to correct
01 1 0 01 0 1 0 1 0 1 Correct BCD Sum
10
Logic Codes & their Arithmetic – Excess-3 Code
Excess-3 Code is a type of a code in which a digit is represented
by adding 3 to the number and then converting it to a 4-bit
binary number. It can be used for the representation of multi-
digit decimal numbers as can BCD. This code have some
advantages for performing decimal arithmetic.
12
Logic Codes & their Arithmetic – Excess-3 Code
The primary advantage of XS-3 coding over BCD coding is
that a decimal number can be nines' complemented (for
subtraction) as easily as a binary number can be ones'
complemented; just invert all bits.
15
Logic Codes & their Arithmetic – Excess-3 Code
Example for Case 1: Add 43 + 36 using Excess-3 addition.
Example 1
43 0 1 1 1 0 1 1 0
+ 36 +0 1 1 0 1 0 0 1
79 1 1 0 1 1 1 1 1 XS-6 for 79
- 0 0 1 1 - 0 0 1 1 Subtract 3 to correct
1 0 1 0 1 1 0 0 XS-3 for 79
Example for Case 1: Add 674 + 325 using Excess-3 addition.
Example 2
674 1 0 0 1 1 0 1 0 0 1 1 1
+ 325 +0 1 1 0 0 1 0 1 1 0 0 0
999 1 1 1 1 1 1 1 1 1 1 1 1 XS-6 for 999
- 0 0 1 1 - 0 0 1 1 - 0 0 1 1 Subtract 3 to correct
1 1 0 0 1 1 0 0 1 1 0 0 XS-3 for 999 16
Logic Codes & their Arithmetic – Excess-3 Code
Example for Case 1: Add 29 + 39 using Excess-3 addition.
Example 1
29 0 1 0 1 1 1 0 0
+ 39 +0 1 1 0 1 1 0 0
68 1 1 0 0 1 0 0 0 XS-6 for 68
- 0 0 1 1 + 0 0 1 1 Add & Subtract 3 to correct
1 0 0 1 1 0 1 1 XS-3 for 68
Example for Case 2: Add 576 + 325 using Excess-3 addition.
Example 2
576 1 0 0 0 1 0 1 0 1 0 0 1
+ 325 +0 1 1 0 0 1 0 1 1 0 0 0
901 1 1 1 1 0 0 0 0 0 0 0 1 XS-6 for 901
-0 0 1 1 +0 0 1 1+0 0 1 1 Add & Subtract 3 to correct
17
1 1 0 0 0 0 1 1 0 1 0 0 XS-3 for 901
Logic Codes & their Arithmetic – Excess-3 Code
Example for Case 2: Sub 86 - 24 using Excess-3 subtraction.
Example 1
86 1 0 1 1 1 0 0 1
- 24 -0 1 0 1 0 1 1 1
62 0 1 1 0 0 0 1 0 XS-6 for 68
+ 0 0 1 1 + 0 0 1 1 Add & Subtract 3 to correct
1 0 0 1 0 1 0 1 XS-3 for 68
Example for Case 1: sub 920 - 265 using Excess-3 subtraction.
Example 2
920 1 1 0 0 0 1 0 1 0 0 1 1
- 265 -0 1 0 1 1 0 0 1 1 0 0 0
655 0 1 1 0 1 0 1 1 1 0 1 1 XS-6 for 901
+0 0 1 1 -0 0 1 1 -0 0 1 1 Add & Subtract 3 to correct
18
1 0 0 1 1 0 0 0 1 0 0 0 XS-3 for 901
Logic Codes & their Arithmetic – Gray Code
Gray Code is a form of binary that uses a different method
of incrementing from one number to the next. With Gray
Code, only one bit changes state from one position to
another. This feature allows a system designer to perform
some error checking (i.e. if more than one bit changes, the
data must be incorrect).
Table:1 Shows the difference between Natural Binary and Gray C2o0de:
Logic Codes & their Arithmetic – Gray Code
In the Table 2 below, note that even from position 7 to 8,
Gray Code only changes one bit state.
21
Logic Codes & their Arithmetic – Gray Code
Conversion of the Gray Code to Natural Binary
1) Write the Gray Code and copy the left most bit under itself.
2) Add the copied bit to the next Gray Code bit over (to the
right). Remember, 0+0=0, 0+1=1, 1+0=1, 1+1=0. Drop all
carries. Write result next to the copied digit from step 1.
22
Logic Codes & their Arithmetic – Gray Code
1 1 0 0 1 1 0 1 1
1 0 1 0 1 0 1 1 0
Example 2: Convert Binary 011001010 to its Gray equivalent
0 1 1 0 0 1 0 1 0
0 1 0 1 0 1 1 1 1 23
Logic Codes & their Arithmetic – Gray Code
Process of Gray code to Binary code conversion
Example 1: Convert Gray 101001010 to its Binary equivalent
Gray Code Binary Code
1 0 1 0 0 1 0 1 0
1 1 0 0 0 1 1 0 0
Example 2: Convert Gray 111011110 to its Binary equivalent
1 1 1 0 1 1 1 1 0
0 1 0 1 0 1 1 1 1 24
Logic Codes & their Arithmetic – ASCII Code
ASCII Code: The most widely used alphanumeric code which
has almost universal use is the American Standard Code for
Information Interchange (ASCII). This was originally a 7-bit
code, designed to be capable of representing the alphabet in
lower- and upper-case, all the numbers, the standard punctuation
symbols and also additional control characters. It is a seven bit
code and so it has 27=128 possible code this is more than
enough to represent all of the standard key board characters as
well as the control functions such as the return and line feed
functions.
The first 32 values in the code are used as control characters.
An exception to this is code 127 (7F in hex). This is used to
represent the DEL (Delete) key. The Space character is an
25
actual character. However it is labeled as space for clarity.
Logic Codes & their Arithmetic – ASCII Code
26
Logic Codes & their Arithmetic – ASCII Code
27
Logic Codes & their Arithmetic – ASCII Code
Example of writing a sentence in ASCII Code
Logic Design & Switching Theory
L o g i c sp
1001100 1101111 1100111 1101001 1100011 0100000
D e s i g n
1000100 1100101 1110011 1101001 1100111 1101110
sp & sp S w i
0100000 0100110 0100000 1010011 1110111 1101001
t c h i n g
1110100 1100011 1101000 1101001 1101110 1100111
T h e o r y
1010100 1101000 1100101 1101111 1110010 1111001 28
Logic Codes & their Arithmetic – Parity Bits
Error Detection and Correction Codes:
The distance between two code words I and J is equal to the
number of bit positions in which I and J differ. If the distance
between any code words of a code C is ≥ dmin, the code is
said to have minimum distance dmin.
• The binary information in a digital computer must have a physical existence in some
medium for storing individual bits.
• A binary cell is a device that possesses two stable states and is capable of storing one
bit (0 or 1) of information.
• The information stored in a cell is 1 when the cell is in one stable state and 0 when the
cell is in the other stable state.
1100001111001001
A register with 16 cells can be in one of 2^16 possible states.
Register Transfer