0% found this document useful (0 votes)
37 views33 pages

Lec4 - Intro. To Digital Logic Numbering Codes and Their Arithmetics

The document discusses digital logic numbering codes and their arithmetic. It introduces several commonly used binary codes, including BCD code, excess-3 code, Gray code, and ASCII code. It provides details on BCD code, including how decimal numbers are represented in BCD, examples of encoding decimal numbers in BCD, and the rules and process for adding two numbers in BCD code, including correcting sums greater than 9 by adding 0110. Arithmetic of other codes like excess-3 code are also briefly mentioned.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views33 pages

Lec4 - Intro. To Digital Logic Numbering Codes and Their Arithmetics

The document discusses digital logic numbering codes and their arithmetic. It introduces several commonly used binary codes, including BCD code, excess-3 code, Gray code, and ASCII code. It provides details on BCD code, including how decimal numbers are represented in BCD, examples of encoding decimal numbers in BCD, and the rules and process for adding two numbers in BCD code, including correcting sums greater than 9 by adding 0110. Arithmetic of other codes like excess-3 code are also briefly mentioned.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

Digital Logic & Design (DLD)

Logic Codes & their Arithmetic


Faculty of Engineering - UCP

Prof. Muhammad Salman


Faculty of Engineering
University of Central Punjab
Lecture No: 4
Intro. to Digital Logic numbering Codes & their
Arithmetics (BCD – Excess-3 – Gray – ASCII Codes)
Logic Codes & their Arithmetic - Introduction
 Besides the basic representations used for unsigned and signed
Logic Codes & their Arithmetic

integers and fractions, many other binary codes also exist for
Faculty of Engineering - UCP

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 data dropping
 Few of the most commonly encountered codes are:
 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
Logic Codes & their Arithmetic
Faculty of Engineering - UCP

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
Logic Codes & their Arithmetic
Faculty of Engineering - UCP

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


 Example: Encode the decimal Number N = (2573)10 in BCD
N = 0010 0101 0111 0011 in BCD.
► BCD is more human-readable than hexadecimal or binary,
► But arithmetic hardware for plain binary is simpler 4
Logic Codes & their Arithmetic - BCD Code
Tables of
Logic Codes & their Arithmetic
Faculty of Engineering - UCP

Signed
Decimal
Numbers
& their
BCD
Equivalents

5
Logic Codes & their Arithmetic
Faculty of Engineering - UCP

Logic Codes & their Arithmetic - BCD Code

6
Logic Codes & their Arithmetic - BCD Code
 Addition Process of BCD Code
Logic Codes & their Arithmetic

Many computers and calculators use the BCD Code to represent decimal
Faculty of Engineering - UCP

numbers. As mentioned earlier that this code takes each decimal digit and
represent it by a four-bit code ranging from 0000 to 1001.

 Rule for BCD Addition

1. Sum of two numbers equals 9 or less


۞ Then write down the BCD equivalents of the given decimal
digits and simply add them up to get the final result.

2. Sum of two numbers is greater than 9


۞ Whenever sum of two digits exceeds 9, the sum must be corrected by
the addition of six (0110) in that particular sum when the number
exceeds 9 to take into account the skipping of the six invalid code
groups. 7
Logic Codes & their Arithmetic - BCD Code
Examples for Rule 1: Add 45 + 33 using BCD Addition.
Logic Codes & their Arithmetic
Faculty of Engineering - UCP

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.
Logic Codes & their Arithmetic

Example 1
Faculty of Engineering - UCP

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 Add 756 + 245 using BCD Addition.
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
 Rule for BCD Subtraction
Logic Codes & their Arithmetic
Faculty of Engineering - UCP

Subtract the two BCD numbers using the rules for binary subtraction.

1. If subtraction difference is equal to 9 or less


۞ If subtraction result is equal or less than 9 or if the subtraction produces
a borrow, then it is a valid BCD number leave the result as it is.
2. If subtraction difference is greater than 9
۞ If subtraction result is greater than 9 or if a borrow is required from the
next group, subtract 6 (0110) from the result produced by four bit
subtraction. OR if the subtraction creates an invalid BCD number then
correct the digit by subtracting six where the error occurred.

10
Logic Codes & their Arithmetic - BCD Code
Examples for Rule 2: Sub 86 - 24 using BCD Subtraction.
Logic Codes & their Arithmetic

Example 1
Faculty of Engineering - UCP

86 1 0 0 0 0 1 1 0
- 24 - 0 0 1 0 0 1 0 0
62 0 1 1 0 0 0 1 0

Example 2 Sub 920 - 265 using BCD Subtraction.


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 Diff. in 1st digit
- 0 1 1 0 - 0 1 1 0 Sub 6 in each bit to correct
01 1 0 01 0 1 0 1 0 1 Correct BCD Diff.

11
Logic Codes & their Arithmetic – Excess-3 Code
 Excess-3 (XS3) Code is the same code as BCD but in this case before
Logic Codes & their Arithmetic

the conversion takes place three (3) is added to the decimal number.
Faculty of Engineering - UCP

In XS-3 a digit is represented by adding 3 to the actual 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. This
means the following combinations do not exist or invalid states in XS3
numbers.
0000 0001 0010 1101 1110 and 1111
This representation simplifies the addition process.
 Rule:
if the addition stage generates a carry add 3,
if it does not subtract 3
 It is Non-Weighted Code, and also a Sequential code which van be used
for arithmetic operation. 12
Logic Codes & their Arithmetic – Excess-3 Code
 In Excess-3 Code The smallest binary number represents the smallest
Logic Codes & their Arithmetic

value. (i.e. 0 − Excess Value) and The greatest binary number


Faculty of Engineering - UCP

represents the largest value. (i.e. 2 N+1 − Excess Value − 1)


Table for Excess-3 Code

13
Logic Codes & their Arithmetic – Excess-3 Code
 The primary advantage of XS-3 coding over BCD coding is that a
Logic Codes & their Arithmetic

decimal number can be nines' complemented (for subtraction) as easily


Faculty of Engineering - UCP

as a binary number can be ones' complemented; just invert all bits.

 Adding Excess-3 works on a different algorithm than BCD coding or


regular binary numbers. When you add two XS-3 numbers together,
the result is not an XS-3 number. For instance, when you add 1 and 0
in XS-3 the answer seems to be 4 instead of 1. In order to correct this
problem, when you are finished adding each digit, you have to subtract
3 (binary 11) if the digit is less than decimal 10 and add 3 (binary 11) if
the number is greater than or equal to decimal 10 (thus causing the
number to wrap).

14
Logic Codes & their Arithmetic – Excess-3 Code
 Addition is Excess-3 (XS-3)
Logic Codes & their Arithmetic

 Normally the problem arises when we are trying to add 8421 numbers
Faculty of Engineering - UCP

whose decimal sum exceeds 9. The XS-3 code was designed to get rid of
this. There are two cases in XS-3 Addition

 Rule for Excess-3 (XS-3) Addition


Case 1. Sum of two numbers equals 9 or less
۞ Then the results is an Excess-6 number. To return to XS-3 form we
must subtract 3 (0011) to get the required result.
Case 2. Sum of two numbers is greater than 9
۞ Whenever sum of two digits exceeds 9, there will be a carry from
one group into the next. When this happened, the group that
produced the carry will revert to 8421 form. To restore the answer
to XS-3 code we must add 3 to the group that produced the carry.

15
Logic Codes & their Arithmetic – Excess-3 Code
Example for Case 1: Add 43 + 36 using Excess-3 addition.
Logic Codes & their Arithmetic

Example 1
Faculty of Engineering - UCP

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.
Logic Codes & their Arithmetic

Example 1
Faculty of Engineering - UCP

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
1 1 0 0 0 0 1 1 0 1 0 0 XS-3 for 901 17
Logic Codes & their Arithmetic – Excess-3 Code
 Subtraction is Excess-3 (XS-3)
Logic Codes & their Arithmetic

 Normally the problem arises when we are trying to add 8421


Faculty of Engineering - UCP

numbers whose decimal sum exceeds 9. The XS-3 code was


designed to get rid of this. There are two cases in XS-3 Addition

 Rule for Excess-3 Subtraction


Case 1. when there is borrow
۞ When there is a borrow when we subtract two numbers then
subtract 3 (0011)

Case 2. Sum of two numbers is greater than 9


۞ When there is no borrow when we subtract two numbers
then add 3 (0011)

18
Logic Codes & their Arithmetic – Excess-3 Code
Example for Case 2: Sub 86 - 24 using Excess-3 subtraction.
Logic Codes & their Arithmetic

Example 1
Faculty of Engineering - UCP

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 62
+0 0 1 1+0 0 1 1 Add & Subtract 3 to correct
1 0 0 1 0 1 0 1 XS-3 for 62
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 655
+0 0 1 1 -0 0 1 1 -0 0 1 1 Add & Subtract 3 to correct
1 0 0 1 1 0 0 0 1 0 0 0 XS-3 for 655 19
Logic Codes & their Arithmetic – Gray Code
 Gray Code is a form of binary that uses a different method of
Logic Codes & their Arithmetic

incrementing from one number to the next. With Gray Code, only one
Faculty of Engineering - UCP

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

 Gray Code is a unweighted code not suited to arithmetic operations but


useful for input / output devices, Analog to Digital converters and other
peripheral equipments. The important characteristic of the Gray code
is that only one digit changes as you count from top to bottom.
20
Logic Codes & their Arithmetic – Gray Code
 Why use Gray Code
Logic Codes & their Arithmetic

Gray Code is the most popular absolute encoder output type because its use
Faculty of Engineering - UCP

prevents certain data errors which can occur with Natural binary during
state changes. For example, in a highly capacitive circuit (or sluggish
system response), a natural binary state change from 0011 to 0100 could
cause the counter/PLC to see 0111. This sort of error is not possible with
Gray Code, so the data is more reliable.

21
Table:1 Shows the difference between Natural Binary and Gray Code
Logic Codes & their Arithmetic – Gray Code
In the Table 2 below, note that even from position 7 to 8, Gray
Logic Codes & their Arithmetic

Code only changes one bit state.


Faculty of Engineering - UCP

22
Logic Codes & their Arithmetic – Gray Code
 Conversion of Natural Binary Code to Gray Code
Logic Codes & their Arithmetic
Faculty of Engineering - UCP

 1) Write the Binary Code and copy the left most bit under itself because
MSB does not change as a result of conversion.

 2) Start with MSB of binary number and add it to the neighboring binary bit
to get the next gray code bit. Remember, 0+0=0, 0+1=1, 1+0=1, 1+1=0.
Repeat for subsequent Gray coded bits.

23
Logic Codes & their Arithmetic – Gray Code
 Conversion of Gray Code to Natural Binary Code
Logic Codes & their Arithmetic
Faculty of Engineering - UCP

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

24
Logic Codes & their Arithmetic – Gray Code
Logic Codes & their Arithmetic

 Process of Binary code to Gray code conversion


Faculty of Engineering - UCP

Example 1: Convert Binary 110011011 to its Gray equivalent

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 25
Logic Codes & their Arithmetic – Gray Code
 Process of Gray code to Binary code conversion
Logic Codes & their Arithmetic
Faculty of Engineering - UCP

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

1 0 1 1 0 1 0 1 1 26
Logic Codes & their Arithmetic – ASCII Code
 ASCII Code: The most widely used alphanumeric code which
Logic Codes & their Arithmetic

has almost universal use is the American Standard Code for


Faculty of Engineering - UCP

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
actual character. However it is labeled as space for clarity. 27
Logic Codes & their Arithmetic
Faculty of Engineering - UCP

Logic Codes & their Arithmetic – ASCII Code

28
Logic Codes & their Arithmetic
Faculty of Engineering - UCP

Logic Codes & their Arithmetic – ASCII Code

29
Logic Codes & their Arithmetic – ASCII Code
Example of writing a sentence in ASCII Code
Logic Codes & their Arithmetic

Logic Design & Switching Theory


Faculty of Engineering - UCP

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 30
Logic Codes & their Arithmetic – Parity Bits
 Error Detection and Correction Codes:
Logic Codes & their Arithmetic

 The distance between two code words I and J is equal to the


Faculty of Engineering - UCP

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.

A code provides t error correction plus detection of s


additional errors if and only if
2t + s + 1 ≤ dmin.
• For single error detection codes (s=1, t=0), dmin ≥ 2.
• For single error detection codes (s=0, t=1), dmin ≥ 3.
31
Logic Codes & their Arithmetic – Parity Bits
 Simple Parity Codes
Logic Codes & their Arithmetic

 Even Parity: Total number of 1’s in a code word is even.


Faculty of Engineering - UCP

 Odd Parity: Total number of 1’s in a code word is odd.

Binary Even parity Odd parity


000 0000 0001
001 0011 0010
010 0101 0100
011 0110 0111
100 1001 1000
101 1010 1011
110 1100 1101
111 1111 1110
32
Logic Codes & their Arithmetic – Parity Bits
 Example of ASCII code with Odd & Even Parity Bits
 Even Parity: Total number of 1’s in a code word is even.
Logic Codes & their Arithmetic
Faculty of Engineering - UCP

 Write Pakistan in ASCII code with Even Parity.


P a k i
01010000 11100001 11101011 01101001
s t a n
11110011 01110100 11100001 11101110

 Odd Parity: Total number of 1’s in a code word is odd.


 Write Pakistan in ASCII code with Odd Parity.
P a k i
11010000 01100001 01101011 11101001
s t a n
33
01110011 11110100 01100001 01101110

You might also like