0% found this document useful (0 votes)
49 views

DLD Week 3

The document is a lecture summary on binary coded decimal (BCD) codes and arithmetic. It discusses BCD, 2421 code, excess-3 code, and Gray code. For BCD, it explains the 4-bit coding for each decimal digit and how to represent and perform addition of decimal numbers. It also discusses other coding schemes like 2421 and excess-3 codes and how they work. Finally, it briefly introduces Gray code and converting between binary and Gray code.

Uploaded by

noreplypet202
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

DLD Week 3

The document is a lecture summary on binary coded decimal (BCD) codes and arithmetic. It discusses BCD, 2421 code, excess-3 code, and Gray code. For BCD, it explains the 4-bit coding for each decimal digit and how to represent and perform addition of decimal numbers. It also discusses other coding schemes like 2421 and excess-3 codes and how they work. Finally, it briefly introduces Gray code and converting between binary and Gray code.

Uploaded by

noreplypet202
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

Binary Coded Decimal (BCD)

CSE 1125: Digital Logic Design – Week 2


Lec Raiyan Rahman
Dept of CSE, UIU
[email protected]

7/26/2023 LEC RAIYAN RAHMAN, DEPT OF CSE, UIU 1


Lecture Outline
Binary Coding
BCD and BCD Arithmetic
2421 Code
Excess 3 Code
Gray Code

2/4/2023 Lec Raiyan Rahman, Dept. of CSE, UIU 2


Binary Coding
Codes

Weighted Error Detection


Non- Reflective Sequential Alphanumeric
Codes And
Weighted Code Code Code Correction Code
Codes

8421 Excess3 Excess3 8421 Hamming


2421 Gray Excess3 ASCII
2421 Code

2/4/2023 Lec Raiyan Rahman, Dept. of CSE, UIU 3


Binary Coded Decimal (BCD)
Warning: Conversion or Coding?
• Do NOT mix up conversion of a decimal number to a binary number
with coding a decimal number with a BINARY CODE.
• 1310 = 11012 (This is conversion)
• 13 ⇔ 0001|0011 (This is coding)

2/4/2023 Lec Raiyan Rahman, Dept. of CSE, UIU 4


Binary Coded Decimal
• Each decimal digit is represented by a 4 bit binary number.
• 10 base system or radix 10 system
• It has binary representations for 10 different digits 0 to 9
• Weighted code with positional weights 8 4 2 1

2/3/2023 Lec Raiyan Rahman, Dept. of CSE, UIU 5


Binary Coded Decimal
Decimal BCD (8421)
0 0000
1 0001
2 0010
3 0011
4 0100
5 0101
6 0110
7 0111
8 1000
9 1001

2/3/2023 Lec Raiyan Rahman, Dept. of CSE, UIU 6


Binary Coded Decimal
Decimal BCD (8421) There are more binary numbers remaining
0 0000
Decimal BCD (8421)
1 0001
10 1010
2 0010
11 1011
3 0011
12 1100
4 0100
13 1101
5 0101
14 1110
6 0110
15 1111
7 0111
8 1000 THESE ARE INVALID BCD CODES
9 1001 Must not be used

2/3/2023 Lec Raiyan Rahman, Dept. of CSE, UIU 7


Binary Coded Decimal
Representation of a decimal number with Binary Coded Decimals

• (17)10 =>0001 0111


• (12)10 =>0001 0010
• (156)10 =>0001 0101 0110

2/3/2023 Lec Raiyan Rahman, Dept. of CSE, UIU 8


Binary Coded Decimal
BCD number to decimal number

• (1 0100)10 = (?)BCD
• 100100110 = (?)BCD

2/3/2023 Lec Raiyan Rahman, Dept. of CSE, UIU 9


Binary Coded Decimal
BCD number to decimal number

• (1 0100)10 = (1 4)BCD
• 100100110 = (4 9)BCD

PACKED
BCD

2/3/2023 Lec Raiyan Rahman, Dept. of CSE, UIU 10


Binary Coded Decimals
Tradeoffs between binary number and packed BCD (BCD coded
number)

• BCD takes more storage


• Addition takes additional steps
• Decimal to BCD does not take much time

2/3/2023 Lec Raiyan Rahman, Dept. of CSE, UIU 11


BCD Arithmetic

Expectation vs Reality:

2/3/2023 Lec Raiyan Rahman, Dept. of CSE, UIU 12


BCD Arithmetic
Given a BCD code, we use binary arithmetic to add the digits:
8 1000 Eight
+5 +0101 Plus 5
13 1101 is 13 (> 9)
Note that the result is MORE THAN 9, so must be represented by two digits!
To correct the digit, add 6
8 1000 Eight
+5 +0101 Plus 5
13 1101 is 13 (> 9)
+0110 so add 6
carry = 1 0011 leaving 3 + cy
0001 | 0011 Final answer (two digits)
If the digit sum is > 9, add one to the next significant digit
2/3/2023 Lec Raiyan Rahman, Dept. of CSE, UIU 13
BCD Arithmetic

2/3/2023 Lec Raiyan Rahman, Dept. of CSE, UIU 14


BCD Arithmetic
• Add 2905BCD to 1897BCD showing carries and digit corrections.

0001 1000 1001 0111


+ 0010 1001 0000 0101

2/3/2023 Lec Raiyan Rahman, Dept. of CSE, UIU 15


Decimal Digit Set I 2421
2421 Code 0 0000
1 0001
• This is also a 2 0010
scheme for
another BCD 3 0011
• It is to be built 4 0100
using self
complementing
5 0101
properties 6 0110
7 0111
8 1110
2/3/2023
9
Lec Raiyan Rahman, Dept. of CSE, UIU
1111 16
Decimal Digit Set II 2 4 2 1
2421 Code 0 0000
1 0001
• This is also a 2 0010
scheme for
another BCD 3 0011
• It is to be built 4 0100
using self
complementing 5 1011
properties 6 1100
7 1101
8 1110
2/3/2023
9
Lec Raiyan Rahman, Dept. of CSE, UIU
1111 17
2421 Code
How to determine if a coding method is self complimenting?
• 8 4 2 1 => 8+4+2+1=15 which is not equal to 9 – not self complimenting
• 2 4 2 1 => 2+4+2+1 =9, which is equal to 9 – self complimenting

Find out yourself:


Are these coding methods self complimenting?
•3311
•4311
2/3/2023 Lec Raiyan Rahman, Dept. of CSE, UIU 18
Excess 3 Code
• You know how to
convert to binary
• You know how to
add
• So convert to
binary and just
add 0011 (3)

2/3/2023 Lec Raiyan Rahman, Dept. of CSE, UIU 19


Excess 3 Code
• You know how to
convert to binary
• You know how to
add
• So convert to
binary and just
add 0011 (3)

2/4/2023 Lec Raiyan Rahman, Dept. of CSE, UIU 20


Excess 3 Code
Decimal to XS-3
• (24)10= (?)XS-3
• (658)10= (?)XS-3

2/4/2023 Lec Raiyan Rahman, Dept. of CSE, UIU 21


Excess 3 Code
Decimal to XS-3
• (24)10= (0101 0111)XS-3
• (658)10= (1001 1000 1011) XS-3

2/4/2023 Lec Raiyan Rahman, Dept. of CSE, UIU 22


Excess 3
Is XS 3 Self Complimenting?

2/4/2023 Lec Raiyan Rahman, Dept. of CSE, UIU 23


Gray Code
Discovered by Frank Gray

Unweighted code

Two successive values differ in only 1 bit

Binary numbers are converted to gray code, to


reduce switching operations

Also known as Unit distance code

2/4/2023 Lec Raiyan Rahman, Dept. of CSE, UIU 24


Gray Code Table

2/4/2023 Lec Raiyan Rahman, Dept. of CSE, UIU 25


Binary to Gray Code
• Step1 : Record the MSB as it is
• Step 2 : Add the MSB to the next bit, record the sum and neglect the
carry
• Step 3 : Repeat the process

2/4/2023 Lec Raiyan Rahman, Dept. of CSE, UIU 26


Gray Code to Binary
Step 1: Record the MSB as it is
Step 2 : Add MSB to the next bit of Gray code, record the sum and
neglect the carry
Step3 : Repeat

2/4/2023 Lec Raiyan Rahman, Dept. of CSE, UIU 27


Explore yourself
• BCD subtraction
• Excess 3 Subtraction
• Division of fractional numbers

Reference Link

2/4/2023 Lec Raiyan Rahman, Dept. of CSE, UIU 28


Thank You

2/3/2023 Lec Raiyan Rahman, Dept. of CSE, UIU 29


Data arithmetic
How can we make the computer understand negative as well as positive numbers?

Data
Representation

Magnitude Complement

1’s 2’s
Unsigned Signed
Complement Complement

10/9/2023 Lec Raiyan Rahman, Dept. of CSE, UIU 30


Binary arithmetic
Unsigned representation
• +6 = 110

• -6 = No representation

Range: 0 to 2n

Signed representation
• +6=0 110

• -6= 1 110

Range: -(2n-1-1) to +(2n-1-1)


10/9/2023 Lec Raiyan Rahman, Dept. of CSE, UIU 31
Using Complements
• r‘s complement / radix complement
• (r-1)’s complement / diminished radix complement

• Let us at first understand complements in our number system:


• 10’s complement of a number = 10len – the number
• 9’s complement of a number – traverse the number and subtract every
digit from 9
• Also : 10’s complement = 9’s complement + 1

10/9/2023 Lec Raiyan Rahman, Dept. of CSE, UIU 32


Using Complements

• 10's complement of 25 is = 75
• 9's complement of 25 is = 74

10/9/2023 Lec Raiyan Rahman, Dept. of CSE, UIU 33


1’s Complement and 2’s Complement
• Let us find the 1’s complement of 1101
• Let us find the 2’s complement of 10111000
• A shortcut for 2’s complement?
• Range for 1’s complement : - (2n-1-1) to + (2n-1-1)
• Range for 2’s complement : - (2n-1) to + (2n-1-1)
To get 1’s complement of a binary number, simply invert the given number. For
example, 1’s complement of binary number 110010 is 001101. To get 2’s
complement of binary number is 1’s complement of given number plus 1 to the
least significant bit (LSB). For example 2’s complement of binary number 10010 is
(01101) + 1 = 01110.

10/9/2023 Lec Raiyan Rahman, Dept. of CSE, UIU 34


1’s Complement and 2’s Complement
• To get 1’s complement of a binary number, simply invert the
given number.

• For example, 1’s complement of binary number 110010 is


001101.

• To get 2’s complement of binary number is 1’s complement of


given number plus 1 to the least significant bit (LSB).

• For example 2’s complement of binary number 10010 is


(01101) + 1 = 01110.

10/9/2023 Lec Raiyan Rahman, Dept. of CSE, UIU 35


1’s Complement

10/9/2023 Lec Raiyan Rahman, Dept. of CSE, UIU 36


Binary subtraction using 1’s and 2’s
complement
Subtract :
Use 1’s complement 1 1 0 0 (12)
1. Take 1’s complement of the subtrahend
2. Add with minuend (-) 0 1 0 1 (5)
3. If the result of above addition has carry bit 1, then add it to ———————–
the least significant bit (LSB) of given result
4. If there is no carry bit 1, then take 1’s complement of the
result which will be negative Subtract :
0 1 0 1 (5)
Be careful about the assigning of sign bits and (-) 1 1 0 0 (12)
Value bits ———————–

10/9/2023 Lec Raiyan Rahman, Dept. of CSE, UIU 37


Binary subtraction using 1’s and 2’s
complement
Subtract :
Use 2’s complement 1 0 0 1 (9)
1. Take 2’s comp of no you are subtracting (-) 0 1 0 0 (4)
2. Add it ———————–
3. If the final carry is 1, the result is positive
4. If the carry is 0, the result is negative Subtract :
0 1 0 1 (6)
(-) 1 1 0 0 (11)
———————–
Be careful about the assigning of sign bits and
Value bits
10/9/2023 Lec Raiyan Rahman, Dept. of CSE, UIU 38
BCD Subtraction

A-B
Steps
1. Take 10′s complement for B
2. Add it to A using BCD addition
3. If addition is invalid BCD then add 6
4. If carry then add it to the next bits
5. In final result,
a. if carry is occurred then it is ignored
b. If there is no any carry over, then take 10′s complement of the result and it
is negative.
Lec Raiyan Rahman, Dept. of CSE 39
10/9/2023
BCD Subtraction

A Review of Binary Subtraction

Lec Raiyan Rahman, Dept. of CSE 40


10/9/2023
BCD Subtraction

Lec Raiyan Rahman, Dept. of CSE 41


10/9/2023
BCD Subtraction

Lec Raiyan Rahman, Dept. of CSE 42


10/9/2023
BCD Subtraction

Lec Raiyan Rahman, Dept. of CSE 43


10/9/2023
Thank You

10/9/2023 Lec Raiyan Rahman, Dept. of CSE 44

You might also like