Lec4 - Intro. To Digital Logic Numbering Codes and Their Arithmetics
Lec4 - Intro. To Digital Logic Numbering Codes and Their Arithmetics
integers and fractions, many other binary codes also exist for
Faculty of Engineering - UCP
Signed
Decimal
Numbers
& their
BCD
Equivalents
5
Logic Codes & their Arithmetic
Faculty of Engineering - UCP
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.
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.
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
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
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
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
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
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 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
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
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
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
28
Logic Codes & their Arithmetic
Faculty of Engineering - UCP
29
Logic Codes & their Arithmetic – ASCII Code
Example of writing a sentence in ASCII Code
Logic Codes & their Arithmetic
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