numbers

King Abdul Aziz University
Faculty of Computing and Information Technology
Computer Science Department
LAB MANUAL
CPCS 211
Digital Logic Design
Prepared By:
Aman Ullah
Spring 2012 - 1433
Term
Spring 2012
Lab 2: Number Systems and their conversion
Statement Purpose:
In this lab we shall discuss about various number systems, conversion of numbers from one base to the
other, complements and binary codes. In addition to this, the students will also be familiarized with
subtracting two numbers using complements.
Activity Outcomes:
The students will be able to represent any number in any base. They will also learn how to convert a number
from one base to any other base. The students will be acquainted with the subtraction of numbers using
complements.
Instructor Note:
Read the lab notes carefully and thoroughly. All the examples have been solved in an easy to understand
manner. You should not find it difficult to learn the topics given in these lab notes. Try to solve all the
exercises yourself that are given at the end of notes. These exercises will certainly help you in achieving the
objectives of this lab.
CPCS-211 Digital Logic Design
Term
Spring 2012
Lab 2: Number Systems and their conversion
Number System
A number system is a set of numbers together with one or more operations (e.g. add,
subtract). Commonly used number systems are decimal, binary, octal and hexadecimal
number systems. All these number systems are also called weighted number systems.
Decimal Number System or Base (or Radix) 10 System
Before digital computers, the only known number system was the decimal number
system.
-It has a total of ten digits: {0,1,2,….,9}
A decimal number such as 7392 represents a quantity equal to 7 thousands plus 3
hundreds plus 9 tens plus 2 units. The thousands, hundreds etc are powers of 10 (in
decimal number system representing the position of the coefficients. To be more exact,
7392 should be written as 103 x 7 + 102 x 3 + 101 x 9 +100 x 2
In general if a decimal number is represented as ……a4 a3 a2 a1 a0 . a-1 a-2 a-3 a-4………..
So this decimal number can be represented by multiplying coefficients of integral part by
increasing powers of 10 (from right to left) and multiplying coefficients of fractional part
by decreasing powers of 10 (from left to right) and then adding all these terms as
……..+103 x a3 + 102x a2+101 x a1 +100 x a0 +10-1 x a-1 +10-2x a-2 + 10-3 x a-3 + 10-4 x a-4 +……..
Example
9375
CPCS-211 Digital Logic Design
Term
Spring 2012
Lab 2: Number Systems and their conversion
Binary Number System (base-2)
Binary number system is a different number system. This number system has only two
possible values i.e. 0 and 1. The decimal equivalent of a binary number can be found by
multiplying coefficients of integral part by increasing powers of 2 (from right to left) and
multiplying coefficients of fractional part by decreasing powers of 2 (from left to right)
and then adding all these terms as
……..+23 x a3 + 22x a2+21 x a1 +20 x a0 +2-1 x a-1 +2-2x a-2 + 2-3 x a-3 + 2-4 x a-4 +…
where ……a4 a3 a2 a1 a0 . a-1 a-2 a-3 a-4……….. are coefficients of binary number.
Example
Example
Write decimal equivalent of binary number (101011.1011)2
CPCS-211 Digital Logic Design
Term
Spring 2012
Lab 2: Number Systems and their conversion
Octal Number System (or Base-8 System)
This number system has only eight possible values i.e. 0, 1, 2, 3, 4 ,5 ,6 and 7. The
decimal equivalent of an octal number can be found by multiplying coefficients of
integral part by increasing powers of 8 (from right to left) and multiplying coefficients of
fractional part by decreasing powers of 8 (from left to right) and then adding all these
terms as
……..+83 x a3 + 82x a2+81 x a1 +80 x a0 +8-1 x a-1 +8-2x a-2 + 8-3 x a-3 + 8-4 x a-4 +…
where ……a4 a3 a2 a1 a0 . a-1 a-2 a-3 a-4……….. are coefficients of octal number.
Example: Convert the octal number (375)8 into its equivalent decimal number.
Example
Write decimal equivalent of octal number (630.45)8
CPCS-211 Digital Logic Design
Term
Spring 2012
Lab 2: Number Systems and their conversion
Hexadecimal Number System (or Base-16 System)
This number system has sixteen possible values i.e. 0, 1, 2, 3, 4 ,5 ,6, 7, 8, 9, A, B, C, D, E
and F. The decimal equivalent of a hexadecimal number can be found by multiplying
coefficients of integral part by increasing powers of 16 (from right to left) and
multiplying coefficients of fractional part by decreasing powers of 16 (from left to right)
and then adding all these terms as
……..+163 x a3 + 162x a2+161 x a1 +160 x a0 +16-1 x a-1 +16-2x a-2 + 16-3 x a-3 + 16-4 x a-4 +…
where ……a4 a3 a2 a1 a0 . a-1 a-2 a-3 a-4……….. are coefficients of hexadecimal number.
Example
Example
Write decimal equivalent of hexadecimal number (2C5.FB)16
CPCS-211 Digital Logic Design
Term
Spring 2012
Lab 2: Number Systems and their conversion
Binary Addition (cont.)
Carries
1
1 1 1 1
1 0 1 1 0 1
Q: How to verify?
Augund
A: Convert to decimal
1 0 0 1 1 1
Addend
45
+ 39
1 0 1 0 1 0 0
Sum
----------84
CPCS-211 Digital Logic Design
Term
Spring 2012
Lab 2: Number Systems and their conversion
ubtractionBinary S
Minuend
Subtrahend
CPCS-211 Digital Logic Design
Term
Spring 2012
Lab 2: Number Systems and their conversion
CPCS-211 Digital Logic Design
Term
Spring 2012
Lab 2: Number Systems and their conversion
CPCS-211 Digital Logic Design
Term
Spring 2012
Lab 2: Number Systems and their conversion
CPCS-211 Digital Logic Design
Term
Spring 2012
Lab 2: Number Systems and their conversion
Converting Octal to Binary
Since 8 can be written as 23, therefore each octal digit corresponds to three binary digits. The
conversion from octal to binary is easily accomplished by writing each octal digit into a group of
three bits.
Example:
(673.124)8 = (110 111 011 . 001 010 100)2
6
7
1
3
2
4
Converting Octal to Hexadecimal
First convert the octal number to binary number using the same procedure as described above. Then
make groups of 4 bits starting from right to left for integral part and from left to right for fractional
part. In the process, complete the unformed group by placing extra zeros (on the left of integral
part’s incomplete group and on the right of fractional part’s incomplete group). Finally write the
hexadecimal equivalent of each group of four bits.
Example:
(673.124)8 = (110 111 011 . 001 010 100)2
6
7
3
1
2
(First converting Octal number to Binary)
4
= (110 111 011 . 001 010 100)2
= (0001 1011 1011 . 0010 1010 0000)2
1
B
B
= (1 B B . 2 A )16
2
A
(Making groups of four bits)
0
(Writing hexadecimal equivalent of every group)
Converting Hexadecimal to Binary
Since 16 can be written as 24, therefore each hexadecimal digit corresponds to four binary digits.
The conversion from hexadecimal to binary is easily accomplished by writing each hexadecimal
digit into a group of four bits.
Example:
(30DF.A14)16 = (0011 0000 1101 1111 . 1010 0001 0100)2
3
0
D
F
A
1
4
CPCS-211 Digital Logic Design
Term
Spring 2012
Lab 2: Number Systems and their conversion
Converting Hexadecimal to Octal
First convert the hexadecimal number to binary number using the same procedure as described
above. Then make groups of 3 bits starting from right to left for integral part and from left to right
for fractional part. In the process, complete the unformed group by placing extra zeros (on the left of
integral part’s incomplete group and on the right of fractional part’s incomplete group). Finally
write the octal equivalent of each group of three bits.
Example:
(1 B B . 2 A 0 )16 = (0001 1011 1011 . 0010 1010 0000)2 (First converting hexadecimal
number to Binary)
B
B
1
2
A
0
= (000110111011 . 00101010 0000)2
= ( 110 111 011 . 001 010 100)2
6
7
3
=(6 7 3 . 1 2 4) 8
=
(673.124)8
(110 111 011 . 001 010 100)2
1
2
(Making groups of three bits)
4
(Writing octal equivalent of every three bit group)
(Writing hexadecimal equivalent of every group)
CPCS-211 Digital Logic Design
Term
Spring 2012
Lab 2: Number Systems and their conversion
Complements
Complements are used in digital computers for simplifying the subtraction
operation and for logical manipulation (i.e. finding complements of Boolean
functions).
For every base ‘r’ systems, we have two complements:
(iii) r’s Complement
(i)
(iv) (r – 1)’s Complement
(ii)
Hence for base 10, we have 10’s complement and 9’s complement.
Similarly for base 2 system, we have 2’s complement and 1’s complement.
r’s
Complement:
r’s Complement:
If we have a positive number N in any base ‘r’ with an integer part of ‘n’
digits, then
rn – N if N ≠ 0
r’s Complement of N =
0
if N = 0
Example:
Find 10’s complement of
(i)
(52520)10
(ii)
(ii)
(0.3267)10
(iii)
(25.639)10
Solution:
(i)
Subtract the first non-zero digit (from right) from 10 and subtract
(ii)
all remaining digits from 9. So, we get
–
9
9
9
10
5
2
5
2
0
4
7
4
8
0
(ii)
9
–
9
9
10
0
. 3
2
6
7
0
. 6
7
3
3
CPCS-211 Digital Logic Design
Term
Spring 2012
Lab 2: Number Systems and their conversion
)iii(
–
9
9
2
5
7
4
9
9
10
.
6
3
9
.
3
6
1
Note: In the above formula
Take
For part (i)
For part (ii)
For part (iii)
N
52520
0.3267
25.639
n
5
0
2
r
10
10
10
Example:
Find 2’s complement of
(i)
(10110)2
(ii)
(0.0110)2
(iii)
(1011.101)2
Solution:
(i)
Subtract the first non-zero bit (from right) from 2 and subtract all remaining
bits from 1. So, we get
–
1
1
1
2
1
0
1
1
0
0
0
1
0
1
0
0
(ii)
–
1
1
2
0
. 0
1
1
0
0
. 1
0
1
0
CPCS-211 Digital Logic Design
Term
Spring 2012
Lab 2: Number Systems and their conversion
)iii(
–
1
1
1
1
1
0
1
1
0
1
0
0
1
1
2
.
1
0
1
.
0
1
1
Note: In the above formula
Take
N
For part (i) 101100
For part (ii) 0.0110
For part (iii) 1011.101
n
6
0
4
r
2
2
2
CPCS-211 Digital Logic Design
Term
Spring 2012
Lab 2: Number Systems and their conversion
(r-1)’s Complement:
If we have a positive number N in any base ‘r’ with an integer part of ‘n’ digits and a
fraction part of ‘m’ digits, then
rn – N – r-m if N ≠ 0
(r-1)’s Complement of N =
0
if N = 0
Example:
Find 9’s complement of
(i)
(52520)10
(ii)
(0.3267)10
(iii)
Solution:
(i)
Subtract each and every significant digit from 9. So, we get
–
9
9
9
9
9
5
2
5
2
0
4
7
4
7
9
(ii)
–
(iii)
–
9
9
9
9
0
. 3
2
6
7
0
. 6
7
3
2
9
9
2
5
7
4
9
9
9
.
6
3
9
.
3
6
0
CPCS-211 Digital Logic Design
(25.639)10
Term
Spring 2012
Lab 2: Number Systems and their conversion
Note: In the above formula
Take
For part (i)
For part (ii)
For part (iii)
N
52520
0.3267
25.639
n
5
0
2
m
0
4
3
r
10
10
10
(ii)
(0.0110)2
Example:
Find 1’s complement of
(i)
(10110)2
(iii)
(1011.101)2
Solution:
(i)
Subtract each and every significant bit from 1. So, we get
–
1
1
1
1
1
1
1
0
1
1
0
0
0
1
0
0
1
1
1
1
1
1
(ii)
–
0
.
0
1
1
0
0
.
1
0
0
1
1
1
1
1
1
0
1
1
0
1
0
0
)iii(
–
1
1
1
.
1
0
1
.
0
1
0
CPCS-211 Digital Logic Design
Term
Spring 2012
Lab 2: Number Systems and their conversion
Note: In the above formula
Take
N
For part (i)
101100
For part (ii)
0.0110
For part (iii) 1011.101
n
6
0
4
r
2
2
2
m
0
4
3
Subtraction using r’s Complement:
The subtraction of two positive numbers M and N (i.e. M – N), both of base ‘r’ may be done as follows:
1. Add the minuend M to the r’s complement of the subtrahend N
i.e. M – N = M + (r’s complement of N)
2. Inspect the result obtained in step (1) for an end carry.
(i)
If an end carry occurs, discard it.
(ii)
If an end carry does not occur, again take the r’s complement of the result obtained in step
(1) and place a negative sign in front of the answer.
Note:
Make number of digits same for both the numbers by adding additional zeros to the left of integral part and
to the right of fractional part.
Example:
Use 2’s complement to perform M – N with the given binary numbers:
(i)
M = 1010100
N = 1000100
Verification:
Solution:
M – N = M + (2’s Complement of N)
= 1010100 + (2’s Complement of 1000100)
-
84
68
16
= 1010100 + 0111100
= 1 0 1 0 1 0 0
+ 0 1 1 1 1 0 0
End Carry
1 0 0 1 0 0 0 0
Since end carry exists, therefore we discard it. Hence the answer is 0010000 or 10000.
CPCS-211 Digital Logic Design
Term
Spring 2012
Lab 2: Number Systems and their conversion
(ii)
M = 1000100
N = 1010100
Solution:
Verification:
M – N = M + (2’s Complement of N)
= 1000100 + (2’s Complement of 1010100)
= 1000100 + 0101100
-
68
84
- 16
= 1 0 0 0 1 0 0
+ 0 1 0 1 1 0 0
1 1 1 0 0 0 0
Since no end carry exists, so final answer = - (2’s Complement of 1 1 1 0 0 0 0) = - 0 0 1 0 0 0 0
=-10000
CPCS-211 Digital Logic Design
Term
Spring 2012
Lab 2: Number Systems and their conversion
Subtraction using (r-1)’s Complement:
The subtraction of two positive numbers M and N (i.e. M – N), both of base ‘r’ may be done as follows:
1. Add the minuend M to the (r-1)’s complement of the subtrahend N
i.e. M – N = M + ((r-1)’s complement of N)
2. Inspect the result obtained in step (1) for an end carry.
(iii) If an end carry occurs, add 1 to the least significant digit in order to get the final answer.
(iv)
If an end carry does not occur, again take the (r-1)’s complement of the result obtained in
step (1) and place a negative sign in front of the answer.
Note:
Make number of digits same for both the numbers by adding additional zeros to the left of integral part
and to the right of fractional part.
Example:
Use 1’s complement to perform M – N with the given binary numbers:
(i)
M = 1010100
N = 1000100
Solution:
M – N = M + (1’s Complement of N)
= 1010100 + (1’s Complement of 1000100)
= 1010100 + 0111011
= 1 0 1 0 1 0 0
+ 0 1 1 1 0 1 1
1 0 0 0 1 1 1 1
+ 1
0 0 1 0 0 0 0
Since end carry exists, therefore we have added it to the above result in order to get the final answer
which is 0010000 or 10000.
CPCS-211 Digital Logic Design
Term
Spring 2012
Lab 2: Number Systems and their conversion
(ii)
M = 1000100
N = 1010100
Solution:
M – N = M + (1’s Complement of N)
= 1000100 + (1’s Complement of 1010100)
= 1000100 + 0101011
= 1 0 0 0 1 0 0
+ 0 1 0 1 0 1 1
1 1 0 1 1 1 1
Since no end carry exists, so final answer = - (1’s Complement of 1 1 0 1 1 1 1) = - 0 0 1 0 0 0 0
=-10000
Comparison between 1’s Complement and 2’s Complement:
The 1’s complement has advantage over 2’s complement because digital components has to change
only 0’s to 1’s and 1’s to 0’s in case of 1’s complement. But in case of 2’s complement, we have to take
1’s complement and then have to add 1 to get the 2’s complement or we have to leave all the least
significant 0’s and the first 1 unchanged and then change all 0’s into 1’s and 1’s into 0’s.
The 2’s complement has advantage over 1’s complement while subtracting two binary numbers using
complements. In 2’s complement, we have to perform addition for just one time, whereas in 1’s
complement, we have to perform addition twice, when an end carry exists.
The digital components prefer using 2’s complement because when we subtract two same numbers
using 1’s complement and 2’s complement separately, we have to remember two representations of 0 in
case of 1’s complement; one with positive zero and the other with negative zero.
CPCS-211 Digital Logic Design
Term
Spring 2012
Lab 2: Number Systems and their conversion
Binary Codes:
Electronic digital systems (computers) use signals that have two distinct values and circuit elements
that have two stable states. A binary number of ‘n’ digits may be represented by ‘n’ binary circuit
elements, each having an output signal equivalent to a 0 or 1. A bit is a binary digit To represent a
group of 2n distinct elements in a binary code requires a minimum of ‘n’ bits. For example, a group of
four (22) distinct quantities can be represented by a 2 bit code, with each quantity assigned one of the
bit combinations: 00, 01, 10, 11. Similarly a group of 8 (23) elements requires a 3-bit code.
Decimal Codes:
Binary codes for decimal digits require a minimum of four bits (because to represent digits from 0 to
9 we need at least four bit code). Numerous different codes can be obtained by arranging four or more
bits in the distinct possible combinations. A few possibilities are shown in the following table:
Decimal Digit
BCD ( 8 4 2 1)
Excess-3
8 4 -2 -1
2421
2 of 5
0
0000
0011
0000
0000
11000
1
0001
0100
0111
0001
10100
2
0010
0101
0110
0010
10010
3
0011
0110
0101
0011
10001
4
0100
0111
0100
0100
01100
5
0101
1000
1011
1011
01010
6
0110
1001
1010
1100
01001
7
0111
1010
1001
1101
00110
8
1000
1011
1000
1110
00101
9
1001
1100
1111
1111
00011
In the above table, Excess-3 and 2 of 5 are the only un-weighted codes. Excess-3 was used in some
old computers. Its code assignment is obtained from the corresponding value of BCD after adding 3
in it.
The Excess-3 , the 2 4 2 1 and the 8 4 -2 -1 are self-complementary codes, i.e. the 9’s complement of
the decimal number is easily obtained by changing 1’s into 0’s and vice versa. For example, the
decimal 395 is represented in 2 4 2 1 code by 0011 1111 1011. Its 9’s complement 604 is represented
by 1100 0000 0100 which is easily obtained by replacing 0’s into 1’s and 1’s into 0’s of the original
number.
CPCS-211 Digital Logic Design
Term
Spring 2012
Lab 2: Number Systems and their conversion
Q. No. 4. Convert following Binary Numbers into Decimal
(11010)2 = (
(110101.11)2 = (
)10
—
)10
—
Q. No. 5. Convert the following Octal Numbers into the base indicated
127.4)8=
(
)10
(673.12)8= (
)2
(407.06)8= (
)16
—
—
—
—
—
—
—
CPCS-211 Digital Logic Design
Term
Spring 2012
Lab 2: Number Systems and their conversion
Q. No. 6. Convert the following Hexadecimal Numbers into the base
indicated
(B65F)16 = (
)10
(3A6.C)16 = (
)2
— (40A.EC)16 = (
)8
Q. No. 7. Convert the following Decimal numbers into the base indicated:
(153.513)10 = (
)8
(41.6875)10 = (
)2
—
—
(106.16)10 = (
)16
—
Q. No. 8. Express the following numbers in decimal:
a. (10110.0101)2
b. (26.24)8
c. (1010.1010)2
Q. No. 9.Obtain the 1's and 2's complements of the following binary
numbers:
— (100000)2
(11011010)2
(1000101)2
Q. No. 10. Perform the addition (59F)16 + (E46)16:
CPCS-211 Digital Logic Design
Term
Spring 2012
Lab 2: Number Systems and their conversion
Q. No. 11.Convert the following numbers from the given base to the other
three bases listed in the table:
Decimal
Binary
Octal
Hexadecimal
110011001.101
472.3
ADF9.C
5324.25
Q. No. 12 Using 1’s and 2’s complement, perform M – N with the following binary
numbers. Also check the result by performing subtraction directly.
(ii)
(iii)
(iv)
M = 11010
M = 11010
M = 10010
N = 1101
N = 10000
N = 10011
Q. No. 13 For the weighted codes (a) 3 3 2 1 (b) 4 4 3 -2 for the decimal digits,
determine all possible tables so that the 9’s complement of each decimal digit is
obtained by changing 1’s to 0’s and 0’s to 1’s.
Q. No. 14 Represent the decimal number 8620 (a) in BCD (b) in Excess-3
(c) in 2 4 2 1 code
Q. No. 15 Obtain a binary code to represent all base-6 digits so that the 5’s
complement is obtained by replacing 1’s by 0’s and 0’s by 1’s in the bits of the
code.
Hint: In 5’s complement, 0 and 5 are complements of each other, 1 and 4 are
complements of each other, 2 and 3 are complements of each other.
Assignment No. 1
Solve above mentioned questions 12, 13, and 14 and submit these on or before
Tuesday, October 02, 2012.
CPCS-211 Digital Logic Design