DigitalLogic Chapter 2
DigitalLogic Chapter 2
1
Compiled By: Deepesh Prakash Guragain |Unit 2 | References: Morris Mano
Number System and Codes Unit 2
1.3 Decimal-to-hexadecimal Conversion
The same steps are repeated to convert a number in decimal to a number in hexadecimal.
Only here we have to divide the decimal number by 16 repeatedly, until the quotient of
zero is obtained.
Example 1.3. Convert 34810 into a hexadecimal number.
Solution
Division Quotient Generated remainder
348 21 12
16
21 1 5
16
1 0 1
16
Hence the converted hexadecimal number is 15C16.
1.4 Binary-to-decimal Conversion
To convert binary, octal, or hexadecimal numbers to decimal numbers we have to keep in mind
that each number systems is a positional number system and has a positional weight.
Example Convert 101102 into a decimal number.
Solution
Hence, we find that here, for the sake of conversion, we have to multiply each bit with
its positional weights depending on the base of the number system.
1.5 Octal-to-decimal Conversion
Example Convert 34628 into a decimal number.
Solution
The octal number given is 3 4 6 2
Positional weights 3210
The positional weights for each of the digits are written in italics below each digit.
2
Compiled By: Deepesh Prakash Guragain |Unit 2 | References: Morris Mano
Number System and Codes Unit 2
Hence the decimal equivalent number is given as:
3 × 83 + 4 × 82 + 6 × 81 + 2 × 80
= 1536 + 256 + 48 + 2
= 184210
The positional weights for each of the digits are written in italics below each digit.
Hence the decimal equivalent number is given as:
4 × 163 + 2 × 162 + 10 × 161 + 13 × 160
= 16384 + 512 + 160 + 13
= 1706910.
2 Fractional Conversion
If the number contains the fractional part we have to deal in a different way when
converting the number from a different number system (i.e., binary, octal, or
hexadecimal) to a decimal number system or vice versa.
The positional weights for each of the digits are written in italics below each digit.
Hence the decimal equivalent number is given as:
3 × 82 + 6 × 81 + 2 × 80 + 3 × 8–1 + 5 × 8–2
3
Compiled By: Deepesh Prakash Guragain |Unit 2 | References: Morris Mano
Number System and Codes Unit 2
= 192 + 48 + 2 + 0.375 + 0.078125
= 242.45312510.
The positional weights for each of the digits are written in italics below each digit.
Hence the decimal equivalent number is given as:
2 1 0 –1 –2
4 × 16 + 2 × 16 + 10 × 16 + 1 × 16 + 1 × 16
= 1024 + 32 + 10 + 0.0625 + 0.00390625
= 1066.0664062510.
1 0 1
i.e., (0.625)10 = (0.101)2
4
Compiled By: Deepesh Prakash Guragain |Unit 2 | References: Morris Mano
Number System and Codes Unit 2
Solution
Division Quotient Generated remainder
34 4 2
8
4 0 4
8
Therefore, (34)10 = (42)8
Fractional Part
0.525 0.200 0. 600
×8 ×8 ×8
4.200 1.600 1.200
4 1 1
Therefore, (92)10=(5C)16
Fractional Part
0. 85 0.60
×16 ×16
13.60 9.60
13 9
i.e., (0.85)10 = (0.D9)16
5
Compiled By: Deepesh Prakash Guragain |Unit 2 | References: Morris Mano
Number System and Codes Unit 2
Therefore, (92.85)10 = (5C.D9)16
Since at the time of grouping the three digits in t h e above example starting from the
LSB, we find that the third group cannot be completed, since only one 1 is left out in
the third group, so we complete the group by adding two 0s in the MSB side. This is
called the left padding of the number with 0.
Now if the number has a fractional part then there will be two different classes of
groups—one for the integer part starting from the left of the decimal point and
proceeding toward the left and the second one starting from the right of the decimal
point and proceeding toward the right. If for the second class, and 1 is left out, we
complete the group by adding two 0s on the right side. This is called right-padding.
6
Compiled By: Deepesh Prakash Guragain |Unit 2 | References: Morris Mano
Number System and Codes Unit 2
Solution
Given octal number is 2 4 7
Binary equivalent is 010 100 111
= 010100111
Forming groups of 4 bits from the LSB 1010 0111
Hexadecimal equivalent A 7
3 Complements
Complements are used in digital computers for simplifying the subtraction operation
and for logical manipulations. There are two types of complements for each number
system of base - r:
1. The radix complements or r’s complement
2. The diminished radix complements or (r-1)’s complement
When we deal with a binary system the value of r is 2 and hence the complements
are 2’s and 1’s complements. Similarly, for a decimal system, the value of r is 10 and
we get 10’s and 9’s complements. With the same logic if the number system is octal
we get 8’s and 7’s complement, while it is 16’s and 15’s complements for t h e
hexadecimal system.
3.1 The r’s Complement or Radix Complement
If a positive number N is given in base r with an integer part of n digits, the r’s complement of N
is given as:
𝑟 − 𝑁 𝑓𝑜𝑟 𝑁 ≠ 0
r's complement =
0 𝑓𝑜𝑟 𝑁 = 0
7
Compiled By: Deepesh Prakash Guragain |Unit 2 | References: Morris Mano
Number System and Codes Unit 2
Integer part of digits (n) = total number of positive digits = 5
Given number (N) = 52520
0
The 10’s complement of (0.3245)10 is 10 – 0.3245 = 0.6755.
Similarly, for 23.324
2
The 10’s complement of (23.324)10 is 10 – 23.324 = 76.676.
= (6553610 – 1899210)
= 4654410
= B5D0 16
8
Compiled By: Deepesh Prakash Guragain |Unit 2 | References: Morris Mano
Number System and Codes Unit 2
Here, r = 10, n = 5, m = 0 (no fractional part) and N= (23450)10
The 9’s complement is given by 𝑟 − 𝑟 −𝑁
Therefore, (23450)10 is 10 – 10 – 23450 = 76549.
5 0
= 0.1111-0.0110= 0.1001
Note
➢ The 10’s complement of a decimal number can be formed by leaving all least significant
zeros unchanged, subtracting the first zero least significant digit from 10, and then
subtracting all other higher significant digits from 9.
➢ 2’s complement can be formed by leaving all least significant zeros and the first non-zero
digit unchanged, and then replacing 1’s by 0’s and 0’s by 1’s in all other higher significant
digits.
➢ 9’s complement of a decimal number is formed simply by subtracting every digit from 9
➢ 1’s complement of a binary number is formed simply by changing 1’s to 0’s and 0’s to 1’s.
9
Compiled By: Deepesh Prakash Guragain |Unit 2 | References: Morris Mano
Number System and Codes Unit 2
➢ r's complement can be obtained from the (r-1)’s complement after the addition of r-m to the
least significant digit.
4 Binary Subtraction
Binary subtraction is also carried out in a similar method to decimal subtraction. Binary
subtraction between two numbers can be performed in three ways.
1. the direct method,
2. the r’s complement method, and
3. the (r – 1)’s complement method.
4.1 Direct Method
In this method, we borrow a 1 from a higher significant position when the minuend digit
is smaller than the corresponding subtrahend digit.
Example Using the direct method to perform the subtraction 1001 – 1000.
Solution
1 0 0 1
(–) 1 0 0 0
0 0 0 1
10
Compiled By: Deepesh Prakash Guragain |Unit 2 | References: Morris Mano
Number System and Codes Unit 2
= 169282
Here, 1 is end carry value so it is discarded. Then the required answer = 69282
11
Compiled By: Deepesh Prakash Guragain |Unit 2 | References: Morris Mano
Number System and Codes Unit 2
The subtraction of two positive numbers M-N, both of base r may be done as follows.
1. Add the minuend M to (r-1)’s complement of N
2. Inspect the result obtained in step 1
a. If an end carry occurs, add 1 to the least significant digit (end – around carry). The
result is answer
b. If an end carry doesn’t occurs, take (r-1)’s complement of number obtained in step
1 and placed negative (-ve) sign.
Subtract using (r-1)’s complement
Example using (r-1)’s complement performs 72532-3250
Here, M = 72532, N= 3250
12
Compiled By: Deepesh Prakash Guragain |Unit 2 | References: Morris Mano
Number System and Codes Unit 2
Now, M+N(r-1)’s = 1000100 + 0101011 = 11011112
Here, no end carry is present so, (r-1)’s complement of M+N(r-1)’s is
(27- 20)10 – (1101111)2 = (1111111)2-(1101111)2
= (10000)2
Therefore, the required solution is (-10000)2
The code is also known as 8-4-2-1 code. This is because 8, 4, 2, and 1 are the weights of the four
bits of the BCD code. The weight of the LSB is 20 or 1, that of the next higher order bit is 21 or 2,
that of the next higher order bit is 22 or 4, and that of the MSB is 23 or 8. Since four binary bits are
used the maximum decimal equivalent that may be coded is 15 10 (i.e., 11112). But the maximum
decimal digit available is 910. Hence the binary codes 1010, 1011, 1100, 1101, 1110, 1111,
representing 10, 11, 12, 13, 14, and 15 in decimal are never being used in BCD code. So these six
codes are called forbidden codes and the group of these codes is called the forbidden group in BCD
code.
13
Compiled By: Deepesh Prakash Guragain |Unit 2 | References: Morris Mano
Number System and Codes Unit 2
Example Give the BCD equivalent for the decimal number 589.
Solution
The decimal number is 589
BCD code is 0101 1000 1001
Hence, (589)10 = (010110001001)BCD
Example Give the BCD equivalent for the decimal number 69.27.
Solution.
The decimal number 69.27
BCD code is 0110 1001 . 0010 0111
84-2-1 Code
It is also possible to assign negative weights to decimal codes, as shown by the 84-
2-1 code. In this case, the bit combination 0101 is interpreted as the decimal digit 3, as
obtained from 0 × 8 + 1 × 4 + 0 × (–2) + 1 × (–1) = 3. This is a self-complementary
code, that is, the 9’s complement of the decimal number is obtained just by changing
the 1s to 0s and 0s to 1s, or in effect by getting the 1’s complement of the
corresponding number. For example, if we change the 1s to 0s and 0s to 1s in the previous
example we have 1010, which is interpreted as decimal 6, as obtained from 1 × 8 + 0
× 4 + 1 × (–2) + 0 × (–1) = 6. And 6 is the 9’s complement of 3. This property is
useful when arithmetic operations are done internally with decimal numbers (in a binary
code) and subtraction is calculated by means of 9’s complement.
2421 Code
Another weighted code is 2421 code. The weights assigned to the four digits are 2, 4,
2, and 1. The 2421 code is the same as that in BCD from 0 to 4; however, it varies
from 5 to 9. For example, in this case the bit combination 0100 represents decimal 4;
whereas the bit combination 1101 is interpreted as the decimal 7, as obtained from 2
× 1 + 1 × 4 + 0 × 2 + 1 × 1 = 7. This is also a self-complementary code, that is, the
9’s complement of the decimal number is obtained by changing the 1s to 0s and 0s to 1s.
5.2 Non-weighted Codes
These codes are not positionally weighted. It basically means that each position
of the binary number is not assigned a fixed value. Excess-3 codes and Gray codes
are such non-weighted codes.
14
Compiled By: Deepesh Prakash Guragain |Unit 2 | References: Morris Mano
Number System and Codes Unit 2
Excess-3 Code
A decimal code that has been used in some old computers is Excess-3 code. This is a non-
weighted code. This code assignment is obtained from the corresponding value of 4-bit
binary code after adding 3 to the given decimal digit. Here the maximum value may
be 11002. Since the maximum decimal digit is 9 we have to add 3 to 9 and then get the
BCD equivalent. Like 84-2-1 and 2421 codes Excess-3 is also a self-complementary code,
that is, the 9’s complement of the decimal number is obtained by changing the 1s to 0s
and 0s to 1s. This self-complementary property of the code helps considerably in
performing subtraction operation in digital systems.
Converting the above sum into 4-bit binary equivalent, we have a 4-bit binary
equivalent of 0110 1001 1010
Hence, the Excess-3 code for (367)10 = 0110 1001 1010
Converting the above sum into 4-bit binary equivalent, we have a 4-bit binary
equivalent of 1000 1011 0111 0110
Hence, the Excess-3 code for (367)10 = 10001011.01110110
Table 1 Binary Code for Decimal Numbers
Decimal (BCD)
digit 8421 84-2-1 2421 Excess-3
0 0000 0000 0000 0011
15
Compiled By: Deepesh Prakash Guragain |Unit 2 | References: Morris Mano
Number System and Codes Unit 2
1 0001 0111 0001 0100
2 0010 0110 0010 0101
3 0011 0101 0011 0110
4 0100 0100 0100 0111
5 0101 1011 1011 1000
6 0110 1010 1100 1001
7 0111 1001 1101 1010
8 1000 1000 1110 1011
9 1001 1111 1111 1100
16
Compiled By: Deepesh Prakash Guragain |Unit 2 | References: Morris Mano
Number System and Codes Unit 2
Conversion of a Binary Number into Gray Code
Any binary number can be converted into equivalent Gray code by the following
steps:
➢ The MSB of the Gray code is the same as the MSB of the binary number;
➢ The second bit next to the MSB of the Gray code equals the Ex-OR of the
MSB and the second bit of the binary number; it will be 0 if there are same
binary bits or it will be 1 for different binary bits;
➢ The third bit for Gray code equals the exclusive-OR of the second and third
bits of the binary number, and similarly, all the next lower-order bits follow the
same mechanism.
Example Convert (101011)2 into Gray code.
Step 1. The MSB of the Gray code is the same as the MSB of the binary number.
1 0 1 0 1 1 Binary
1 Gray
Step 2. Perform the ex-OR between the MSB and the second bit of the binary.
1 ⨁ 0 1 0 1 1 Binary
1 1 Gray
Step 3. Perform the ex-OR between the second and the third bits of the binary. The
result is 1, which is the third bit of the Gray code.
1 0 ⨁ 1 0 1 1 Binary
1 1 1 Gray
Step 4. Perform the ex-OR between the third and the fourth bits of the binary.
The result is 1, which is the fourth bit of the Gray code.
1 0 1 ⨁ 0 1 1 Binary
1 1 1 1 Gray
17
Compiled By: Deepesh Prakash Guragain |Unit 2 | References: Morris Mano
Number System and Codes Unit 2
Step 5. Perform the ex-OR between the fourth and the fifth bits of the binary.
The result is 1, which is the fifth bit of the Gray code.
1 0 1 0 ⨁ 1 1 Binary
1 1 1 1 1 Gray
Step 6. Perform the ex-OR between the fifth and the sixth bits of the binary.
The result is 0, which is the last bit of the Gray code.
1 0 1 0 1 ⨁ 1 Binary
1 1 1 1 1 0 Gray
After completing the conversion, the Gray code of binary 101011 is 111110.
Solution
Step 1. Convert the decimal 564 into equivalent binary.
Decimal number 564
Binary number 1000110100
Step 2. Convert the binary number into equivalent Gray code.
1 ⨁ 0 ⨁ 0 ⨁ 0⨁ 1 ⨁ 1 ⨁ 0 ⨁ 1 ⨁ 0 ⨁ 0 Binary
1 1 0 0 1 0 1 1 1 0 Gray
18
Compiled By: Deepesh Prakash Guragain |Unit 2 | References: Morris Mano
Number System and Codes Unit 2
Example Convert the Gray code 101101 into a binary number.
Solution
Step 1. The MSB of the binary number is the same as the MSB of the Gray code.
1 0 1 1 0 1 Gray
1 Binary
Step 2. Perform the ex-OR between the MSB of the binary number and the second bit
of the Gray code and so on till last bit
1 0 1 1 0 1 Gray
⨁ ⨁ ⨁ ⨁ ⨁
1 1 0 1 1 0 Binary
After completing the conversion, the binary number of the Gray code 101101 is 110110
Alphanumeric
An alpha-numeric code is a binary code of a group of elements consisting of ten decimal digits.
The 26 letter of alphabets and certain number of special symbols such as #,& etc. The total number
of elements in an alpha-numeric code is greater than 36. It must be coded with the minimum of 6
bits. Two mostly used alpha-numeric code ra ASCII and EBCDIC.
ASCII (American Code for Information Interchange) Code
The ASCII code are widely used alpha-numeric code. It is basically a 7-bit code since it can create
27 = 128-bit pattern. The ASCII code can be used to encode both the lower case and upper-case
characters of alphabet and some symbol ‘#’, ‘&’ etc.
A→ 65 a→97 0→48 blank→32 and so on.
EBCDIC (Extended Binary Coded Decimal Interchange) Code
It is an 8-it alpha-numeric code. It can create 28 = 256 different bit patterns. EBCDIC cade can
encode all the symbols and characters found in ASCII code. It also encodes many other symbols
which are not encoded by ASCII code. In fact, many of the bit patterns in EBCDIC code are
unassigned.
A→193 a→129 0→240 blank → 64
19
Compiled By: Deepesh Prakash Guragain |Unit 2 | References: Morris Mano
Number System and Codes Unit 2
Character 7-bit 8-bit
ASCII code EBCDIC code
A 1000001 11000001
B 1000010 11000010
C 1000011 11000011
D 1000100 11000100
E 1000101 11000101
F 1000110 11000110
G 1000111 11000111
H 1001000 11001000
I 1001001 11001001
J 1001010 11010001
K 1001011 11010010
L 1001100 11010011
M 1001101 11010100
Instruction Code
An instruction code is a group of bits that instruct the computer to perform the specific operations.
It is divided into two parts: op-code and operands.
The most basic part of an instruction code is its operation part called as “op-code”. The operation
code of an instruction is a group of bits that defines particular operation add, substract, multiply,
divide etc. the number of bits required for the operation code of an instruction depends on the total
number of operation available in the computer. The operation code must consist of atleast n bits
for given 2n distinct operators.
Consider a computer with 64 distinct operation. One of then is add operation. The operation code
consist of six(6) bits with the bit configuration of “110010” assigned to the add operation. When
this operation is decoded in the control unit, the computer issue control signal to reas operatnd
from memory and add operand to the processor resistance.
Solved Problems
Example Encode the following decimal numbers in BCD code:
(a) 45 (b) 273.98 (c) 62.905
Solution
(a) Decimal number is 4 5
BCD code is 0100 0101
Hence the BCD coded form of 4510 is 0100 0101
20
Compiled By: Deepesh Prakash Guragain |Unit 2 | References: Morris Mano
Number System and Codes Unit 2
(b) Decimal number is 2 7 3 9 8
BCD code is 0010 0111 0011 1001 1000 Hence the
BCD coded form of
273.9810 is 0010 0111 0011.1001 1000
21
Compiled By: Deepesh Prakash Guragain |Unit 2 | References: Morris Mano
Number System and Codes Unit 2
Hence the Excess-3 coded form of 471.7810 is 0111 1010 0100.1010 1011
(c) Decimal number is 2 3 1 0 5
BCD code is 0010 0011 0001 0000 0101
Now adding 3 +0011 +0011 +0011 +0011 +0011
Excess-3 code is 0101 0110 0100 0011 1000
Hence the Excess-3 coded form of 23.10510 is 0101 0110.0100 0011 1000
Example Express the following Excess-3 codes as decimal numbers:
(a) 0101 1011 1100 0111 (b) 0011 1000 1010 0100 (c) 0101 1001 0011
Solution
(a) Excess-3 code is 0101 1011 1100 0111
Subtracting 3 from each digit 0011 –0011 –0011 –0011
BCD number is 0010 1000 1001 0100
Decimal number is 2 8 9 4
Hence the decimal number is 2894.
(b) Excess-3 code is 0011 1000 1010 0100
Subtracting 3 from each digit 0011 –0011 –0011 –0011
BCD number is 0000 0101 0111 0001
Decimal number is 0 5 7 1
Hence the decimal number is 571.
(c) Excess-3 code is 0101 1001 0011
Subtracting 3 from each digit 0011 –0011 –0011
22
Compiled By: Deepesh Prakash Guragain |Unit 2 | References: Morris Mano
Number System and Codes Unit 2
BCD number is 0010 0110 0000
Decimal number is 2 6 0
Hence the decimal number is 260.
Solution
23
Compiled By: Deepesh Prakash Guragain |Unit 2 | References: Morris Mano
Number System and Codes Unit 2
Review Questions
1. Convert the following decimal numbers to binary: 12.0625, 104 , 673.23, and 1998. 1-5.
2. Convert the following binary numbers to decimal: 10.10001, 101110.0101, 1110101.110,
1101101.111
3. Convert the following numbers from the given base to the bases indicated
(a) decimal 225.225 to binary, octal, and hexadecimal
(b) binary 11010111.110 to decimal, octal, and hexadecimal
(c) octal 623.77 to decimal, binary, and hexadecimal
(d) hexadecimal 2AC5.D to decimal, octal, and binary
4. Obtain the l’s and 2’s complement of the following binary numbers: 1010101, 0111000,
0000001, 10000, 00000.
5. Perform the subtraction with the following decimal numbers using (1) 10’s complement
and (2) 9’s complement. Check the answer by straight subtraction.
a. 5250 - 321
b. 753 – 864
c. 3570 - 2100
d. 20 – 1000
6. Represent the decimal number 8620 (a) in BCD, (b) in excess-3 code, (c) in 2, 4, 2, 1 code,
and (d) as a binary number.
7. Convert the following decimal numbers to the indicated bases.
a. 7562.45 to octal
b. 1938.257 to hexadecimal
24
Compiled By: Deepesh Prakash Guragain |Unit 2 | References: Morris Mano
Number System and Codes Unit 2
c. 175.175 to binary
8. Convert the following hexadecimal number to decimal and octal numbers
a. 0FFF
b. 3FFF
9. Subtract (1010100 – 1000100) using 1’s complement.
10. Using 2’s complement, subtract (1000100-1010100).
11. Why is Gray code called the reflected code? Explain
12. What are the different types of Binary codes? Explain each in brief
13. How can you find the r’s complement using (r-1)’s complement? Explain with example
14. Excess 3 code is self-complementary code, verify the statement?
15. What do you mean by the Gray code? What are its application?
16. What is decimal code? Differentiate between BCD and excess-3 code.
Declaration: This document is prepared only for academic purposes, contents from different
sources are subject to their own copyright.
25
Compiled By: Deepesh Prakash Guragain |Unit 2 | References: Morris Mano