Upload
sieu-nhan-bung-mo
View
220
Download
0
Embed Size (px)
Citation preview
8/18/2019 Digital Electronics- An Introduction to Theory and Practice_W. H. Gothmann
1/104
Scilab Textbook Companion for
Digital Electronics: An Introduction To
Theory And Practice
by W. H. Gothmann1
Created byAritra Ray
B.TechElectronics Engineering
NIT-DURGAPUR
College TeacherProf. Sabyasachi Sengupta,iit-kharagpur
Cross-Checked byLavitha M. Pereira
November 11, 2013
1Funded by a grant from the National Mission on Education through ICT,http://spoken-tutorial.org/NMEICT-Intro. This Textbook Companion and Scilabcodes written in it can be downloaded from the ”Textbook Companion Project”section at the website http://scilab.in
8/18/2019 Digital Electronics- An Introduction to Theory and Practice_W. H. Gothmann
2/104
Book Description
Title: Digital Electronics: An Introduction To Theory And Practice
Author: W. H. Gothmann
Publisher: Prentice Hall Of India Pvt. Ltd., New Delhi
Edition: 2
Year: 1994
ISBN: 81-203-0348-2
1
8/18/2019 Digital Electronics- An Introduction to Theory and Practice_W. H. Gothmann
3/104
Scilab numbering policy used in this document and the relation to theabove book.
Exa Example (Solved example)
Eqn Equation (Particular equation of the above book)
AP Appendix to Example(Scilab Code that is an Appednix to a particularExample of the above book)
For example, Exa 3.51 means solved example 3.51 of this book. Sec 2.3 meansa scilab code whose theory is explained in Section 2.3 of the book.
2
8/18/2019 Digital Electronics- An Introduction to Theory and Practice_W. H. Gothmann
4/104
Contents
List of Scilab Codes 4
2 Number Systems 7
3 Binary Codes 42
4 Boolean Algebra 58
6 Combinational Logic 62
11 Analog Digital Conversion 97
3
8/18/2019 Digital Electronics- An Introduction to Theory and Practice_W. H. Gothmann
5/104
List of Scilab Codes
Exa 2.1 Convert 10111 to decimal . . . . . . . . . . . . . . . . 7Exa 2.2 Convert 1011101001 to decimal . . . . . . . . . . . . . 7Exa 2.3 Convert 110111 to decimal . . . . . . . . . . . . . . . 8Exa 2.4 Convert 43 to binary . . . . . . . . . . . . . . . . . . . 8Exa 2.5 Convert 200 to binary . . . . . . . . . . . . . . . . . . 8Exa 2.6 Convert 43 to binary . . . . . . . . . . . . . . . . . . . 8Exa 2.7 Convert 200 to binary . . . . . . . . . . . . . . . . . . 9Exa 2.8 Add 1011 and 110 . . . . . . . . . . . . . . . . . . . . 9Exa 2.9 Add 11110 and 11 . . . . . . . . . . . . . . . . . . . . 10Exa 2.10 Subtract one from 100 . . . . . . . . . . . . . . . . . . 10Exa 2.11 Multiply 1011 by 101 . . . . . . . . . . . . . . . . . . 11Exa 2.12 Multiply 11010 by 11011 . . . . . . . . . . . . . . . . . 11Exa 2.13 Divide 110110 by 101 . . . . . . . . . . . . . . . . . . 12
Exa 2.14 Convert 1010111010 to hexadecimal . . . . . . . . . . 12Exa 2.15 Convert 11011110101110 to hexadecimal . . . . . . . . 13Exa 2.16 Convert 4A8C to binary . . . . . . . . . . . . . . . . . 13Exa 2.17 Convert FACE to binary . . . . . . . . . . . . . . . . . 14Exa 2.18 Convert 2C9 to decimal . . . . . . . . . . . . . . . . . 14Exa 2.19 Convert EB4A to decimal . . . . . . . . . . . . . . . . 14Exa 2.20 Convert 423 to hexadecimal . . . . . . . . . . . . . . . 14Exa 2.21 Convert 72905 to hexadecimal . . . . . . . . . . . . . 15Exa 2.22 Add 1A8 and 67B . . . . . . . . . . . . . . . . . . . . 15Exa 2.23 Add ACEF1 and 16B7D . . . . . . . . . . . . . . . . . 16Exa 2.24 Subtract 3A8 from 1273 . . . . . . . . . . . . . . . . . 16
Exa 2.25 Multiply 1A3 by 89 . . . . . . . . . . . . . . . . . . . 17Exa 2.26 Divide 1EC87 by A5 . . . . . . . . . . . . . . . . . . . 17Exa 2.27 Convert 11111011110101 to octal . . . . . . . . . . . . 18Exa 2.28 Convert 1011110100011000111 to octal . . . . . . . . . 18
4
8/18/2019 Digital Electronics- An Introduction to Theory and Practice_W. H. Gothmann
6/104
Exa 2.29 Convert octal number 3674 to binary . . . . . . . . . . 19
Exa 2.30 Minus 5 in twos complement form . . . . . . . . . . . 19Exa 2.31 12 bit twos complement . . . . . . . . . . . . . . . . . 20Exa 2.32 16 bit twos complement . . . . . . . . . . . . . . . . . 20Exa 2.33 Twos complement of minus4 second method . . . . . . 21Exa 2.34 Twos complement of minus17 second method . . . . . 22Exa 2.35 Twos complement of minus4 third method . . . . . . . 22Exa 2.36 Twos complement of minus17 third method . . . . . . 23Exa 2.37 Negative decimal number represented by 10011011 . . 24Exa 2.38 Add minus 17 to minus 30 . . . . . . . . . . . . . . . . 25Exa 2.39 Add minus 20 to 26 . . . . . . . . . . . . . . . . . . . 26Exa 2.40 Add minus 29 to 14 . . . . . . . . . . . . . . . . . . . 26
Exa 2.41 Ones complement of minus 13 . . . . . . . . . . . . . . 27Exa 2.42 Ones complement of minus 13 second method . . . . . 28Exa 2.43 Ones complement addition using 4 bits . . . . . . . . . 28Exa 2.44 Ones complement addition using 4 bits . . . . . . . . . 29Exa 2.45 Ones complement addition using 8 bits . . . . . . . . . 29Exa 2.46 Convert 1101 point 000101 to decimal . . . . . . . . . 30Exa 2.47 Convert point 375 to binary . . . . . . . . . . . . . . . 32Exa 2.48 Convert point 54545 to binary . . . . . . . . . . . . . 33Exa 2.49 Convert 38 point 21 to binary . . . . . . . . . . . . . . 34Exa 2.50 Binary addition . . . . . . . . . . . . . . . . . . . . . . 36
Exa 2.51 Binary mutiplication . . . . . . . . . . . . . . . . . . . 38Exa 3.1 Add 647 to 492 in BCD . . . . . . . . . . . . . . . . . 42Exa 3.2 Add 4318 and 7678 in BCD . . . . . . . . . . . . . . . 44Exa 3.3 Add 3 and 2 in XS3 . . . . . . . . . . . . . . . . . . . 46Exa 3.4 Add 6 and 8 in XS3 . . . . . . . . . . . . . . . . . . . 47Exa 3.5 Convert binary 1011 to Gray code . . . . . . . . . . . 48Exa 3.6 Convert binary 1001011 to Gray code . . . . . . . . . 49Exa 3.7 Convert Gray code 1011 to binary . . . . . . . . . . . 50Exa 3.8 Convert Gray code 1001011 to binary . . . . . . . . . 51Exa 3.9 Hamming code for 1011 . . . . . . . . . . . . . . . . . 52Exa 3.10 Hamming code for 0101 . . . . . . . . . . . . . . . . . 54
Exa 3.11 Correction of Hamming code 1111101 . . . . . . . . . 55Exa 4.1 Demorganize a function . . . . . . . . . . . . . . . . . 58Exa 4.2 Reduce an expression . . . . . . . . . . . . . . . . . . 58Exa 4.3 Reduce an expression . . . . . . . . . . . . . . . . . . 59Exa 4.4 Reduce a boolean expression . . . . . . . . . . . . . . 60
5
8/18/2019 Digital Electronics- An Introduction to Theory and Practice_W. H. Gothmann
7/104
Exa 4.5 Reduce a Boolean expression . . . . . . . . . . . . . . 61
Exa 6.1 Convert A plus B to minterms . . . . . . . . . . . . . 62Exa 6.2 Find minterms for A plus BC . . . . . . . . . . . . . . 62Exa 6.3 Find minterms for AB plus ACD . . . . . . . . . . . . 63Exa 6.4 Minterm designation . . . . . . . . . . . . . . . . . . . 64Exa 6.5 Minterm designation . . . . . . . . . . . . . . . . . . . 64Exa 6.6 2 variable mapping . . . . . . . . . . . . . . . . . . . . 65Exa 6.7 3 variable mapping . . . . . . . . . . . . . . . . . . . . 66Exa 6.8 Mapping an expression . . . . . . . . . . . . . . . . . 67Exa 6.9 Mapping an expression . . . . . . . . . . . . . . . . . 68Exa 6.10 4 variable mapping . . . . . . . . . . . . . . . . . . . . 69Exa 6.11 Reduce an expression by Kmap . . . . . . . . . . . . . 70
Exa 6.12 Reduce an expression using Kmap . . . . . . . . . . . 72Exa 6.13 Reduce expression by kmap . . . . . . . . . . . . . . . 74Exa 6.14.a Inputs Required . . . . . . . . . . . . . . . . . . . . . 75Exa 6.14.b Inputs required . . . . . . . . . . . . . . . . . . . . . . 75Exa 6.14.c Inputs required . . . . . . . . . . . . . . . . . . . . . . 76Exa 6.14.d Inputs required . . . . . . . . . . . . . . . . . . . . . . 76Exa 6.15 Minimise expression using mapping . . . . . . . . . . . 77Exa 6.16 Reduce using mapping . . . . . . . . . . . . . . . . . . 79Exa 6.17 Kmap POS SOP . . . . . . . . . . . . . . . . . . . . . 83Exa 6.18 Minimise expression . . . . . . . . . . . . . . . . . . . 86
Exa 6.19 Reduce kmap by POS . . . . . . . . . . . . . . . . . . 87Exa 6.20 Find minimum of expression . . . . . . . . . . . . . . 88Exa 6.24 Multiple output equation using mapping . . . . . . . . 89Exa 6.25 Multiple output equation using mapping . . . . . . . . 91Exa 6.26 Reduce by variable mapping . . . . . . . . . . . . . . 93Exa 6.27 Reduce by variable mapping . . . . . . . . . . . . . . 93Exa 6.28 Solve using 3 variable mapping . . . . . . . . . . . . . 94Exa 6.29 Reduce by mapping . . . . . . . . . . . . . . . . . . . 94Exa 6.30 Reducing expression by Kmap . . . . . . . . . . . . . 95Exa 11.1 Percentage resolution of 5bit DA converter . . . . . . . 97Exa 11.2 6 bit analog to digital converter . . . . . . . . . . . . . 97
Exa 11.3 Compute the gain of an Opamp . . . . . . . . . . . . . 98Exa 11.4 Compute the output voltage . . . . . . . . . . . . . . 99Exa 11.5 Compute output voltage . . . . . . . . . . . . . . . . . 100Exa 11.6 Resolution and Percent resolution 12bit DAconverter 101Exa 11.7 Resolution and Percent resolution 10bit ADconverter 102
6
8/18/2019 Digital Electronics- An Introduction to Theory and Practice_W. H. Gothmann
8/104
Chapter 2
Number Systems
Scilab code Exa 2.1 Convert 10111 to decimal
1 // E xampl e 2−1//2 / / B i n ar y t o D ec im a l C o n v er s i o n / /3 a = b i n 2 d e c ( ’ 1011 1 ’ )4 / / D ec im al e q u i v a l e n t o f t h e b i n a r y n umber //5 disp ( a )
6 / / a ns we r i n d e c im a l f or m //
Scilab code Exa 2.2 Convert 1011101001 to decimal
1 // E xampl e 2−2//2 / / B i n ar y t o D ec im a l C o n v er s i o n / /3 a = b i n 2 d e c ( ’ 1011 10100 1 ’ )4 / / D ec im al e q u i v a l e n t o f t h e b i n a r y n umber //5 disp ( a )
6 / / a ns we r i n d e c im a l f or m //
7
8/18/2019 Digital Electronics- An Introduction to Theory and Practice_W. H. Gothmann
9/104
Scilab code Exa 2.3 Convert 110111 to decimal
1 // E xampl e 2−3//2 / / B i n ar y t o D ec im a l C o n v er s i o n / /3 a = b i n 2 d e c ( ’ 1101 11 ’ )4 / / D ec im al e q u i v a l e n t o f t h e b i n a r y n umber //5 disp ( a )
6 / / a ns we r i n d e c im a l f or m //
Scilab code Exa 2.4 Convert 43 to binary
1 // E xampl e 2−4//2 / / D ec im al t o b i n a ry c o n v e r s i o n / /3 a = d e c 2 b i n ( 4 3 )
4 // B i n ar y e q u i v a l e n t o f t h e d e c im a l number //5 disp ( a )
6 / / a ns we r i n b i n a r y f or m //
Scilab code Exa 2.5 Convert 200 to binary
1 // E xampl e 2−5//2 / / D ec im al t o b i n a ry c o n v e r s i o n / /3 a = d e c 2 b i n ( 2 0 0 )
4 // B i n ar y e q u i v a l e n t o f t h e d e c im a l number //5 disp ( a )
6 / / a ns we r i n b i n a r y f or m //
Scilab code Exa 2.6 Convert 43 to binary
1 // E xampl e 2−6//
8
8/18/2019 Digital Electronics- An Introduction to Theory and Practice_W. H. Gothmann
10/104
2 / / D ec im al t o b i n a ry c o n v e r s i o n / /
3 a = d e c 2 b i n ( 4 3 )4 // B i n ar y e q u i v a l e n t o f t h e d e c im a l number //5 disp ( a )
6 / / a ns we r i n b i n a r y f or m //
Scilab code Exa 2.7 Convert 200 to binary
1 // E xampl e 2−7//
2 / / D ec im al t o b i n a ry c o n v e r s i o n / /3 a = d e c 2 b i n ( 2 0 0 )4 // B i n ar y e q u i v a l e n t o f t h e d e c im a l number //5 disp ( a )
6 / / a ns we r i n b i n a r y f or m //
Scilab code Exa 2.8 Add 1011 and 110
1 // E xampl e 2
−
8//2 / / B i n ar y a d d i t i o n / /3 clc
4 // c l e a r s t he c o n s o l e //5 clear
6 // c l e a r s t h e a l r e a d y e x i s t i n g v a r i a b l e s //7 x = b i n 2 d e c ( ’ 101 1 ’ )8 y = b i n 2 d e c ( ’ 110 ’ )9 / / b i n a r y t o d e ci m a l c o n v e r s i o n / /
10 z = x + y
11 / / a d d i t i o n / /
12 a = d e c 2 b i n ( z )13 / / d e ci m a l t o b i n a r y c o n v e r s i o n / /14 disp ( ’ a d di t i o n o f t h e 2 b in ar y numbers i s : ’ )15 disp ( a )
16 / / a ns we r i n b i n a r y f or m //
9
8/18/2019 Digital Electronics- An Introduction to Theory and Practice_W. H. Gothmann
11/104
Scilab code Exa 2.9 Add 11110 and 11
1 // E xampl e 2−9//2 / / B i n ar y a d d i t i o n / /3 clc
4 // c l e a r s t he c o n s o l e //5 clear
6 // c l e a r s t h e a l r e a d y e x i s t i n g v a r i a b l e s //
7 x = b i n 2 d e c ( ’ 1111 0 ’ )8 y = b i n 2 d e c ( ’ 1 1 ’ )9 / / b i n a r y t o d e ci m a l c o n v e r s i o n / /
10 z = x + y
11 / / a d d i t i o n / /12 a = d e c 2 b i n ( z )
13 / / d e ci m a l t o b i n a r y c o n v e r s i o n / /14 disp ( ’ a d di t i o n o f t h e 2 b in ar y numbers i s : ’ )15 disp ( a )
16 / / a ns we r i n b i n a r y f or m //
Scilab code Exa 2.10 Subtract one from 100
1 // E xampl e 2−10//2 / / B i n ar y S u b t r a c t i o n / /3 clc
4 // c l e a r s t he c o n s o l e //5 clear
6 // c l e a r s a l l e x i s t i n g v a r i a b l e s //7 x = b i n 2 d e c ( ’ 100 ’ )
8 y = b i n 2 d e c ( ’ 1 ’ )9 / / b i n a r y t o d e ci m a l c o n v e r s i o n / /
10 z = x - y
10
8/18/2019 Digital Electronics- An Introduction to Theory and Practice_W. H. Gothmann
12/104
11 / / s u b t r a c t i o n / /
12 a = d e c 2 b i n ( z )13 / / d e ci m a l t o b i n a r y c o n v e r s i o n / /14 disp ( ’ s u b t r a c t i o n o f two b i na r y numbers i s : ’ )15 disp ( a )
16 / / a ns we r i n b i n a r y f or m //
Scilab code Exa 2.11 Multiply 1011 by 101
1 // E xampl e 2−
11//2 // B i na ry m u l t i p l i c a t i o n //3 clc
4 // c l e a r s t he c o n s o l e //5 clear
6 // c l e a r s a l l e x i s t i n g v a r i a b l e s //7 x = b i n 2 d e c ( ’ 101 1 ’ )8 y = b i n 2 d e c ( ’ 101 ’ )9 / / b i n a r y t o d e ci m a l c o n v e r s i o n / /
10 z = x * y ;
11 / / m u l t i p l i c a t i o n / /
12 a = d e c 2 b i n ( z )13 / / d e ci m a l t o b i n a r y c o n v e r s i o n / /14 disp ( ’ m u l t i p l i c a t i o n o f two b in ar y numbers i s : ’ )15 disp ( a )
16 / / a ns we r i n b i n a r y f or m //
Scilab code Exa 2.12 Multiply 11010 by 11011
1 // E xampl e 2−
12//2 // B i na ry m u l t i p l i c a t i o n //3 clc
4 // c l e a r s t he c o n s o l e //5 clear
11
8/18/2019 Digital Electronics- An Introduction to Theory and Practice_W. H. Gothmann
13/104
6 // c l e a r s a l l e x i s t i n g v a r i a b l e s //
7 x = b i n 2 d e c ( ’ 1101 0 ’ )8 y = b i n 2 d e c ( ’ 1101 1 ’ )9 / / b i n a r y t o d e ci m a l c o n v e r s i o n / /
10 z = x * y ;
11 / / m u l t i p l i c a t i o n / /12 a = d e c 2 b i n ( z )
13 / / d e ci m a l t o b i n a r y c o n v e r s i o n / /14 disp ( ’ m u l t i p l i c a t i o n o f two b in ar y numbers i s : ’ )15 disp ( a )
16 / / a ns we r i n b i n a r y f or m //
Scilab code Exa 2.13 Divide 110110 by 101
1 // E xampl e 2−13//2 / / B in ar y D i v i s i o n //3 x = b i n 2 d e c ( ’ 1101 10 ’ )4 y = b i n 2 d e c ( ’ 101 ’ )5 r = modul o ( x , y )
6 / / f i n d i n g t h e r e ma i n de r / /
7 z = x / y8 q = floor ( z )
9 / / f i n d i n g t h e q u o t i e n t //10 q u o = d e c 2 b i n ( q )
11 r e m = d e c 2 b i n ( r )
12 / / d e ci m a l t o b i n a r y c o n v e r s i o n s / /13 disp ( ’ t h e q u o t ie n t i s : ’ )14 disp ( q u o )
15 disp ( ’ t h e r e ma in de r i s : ’ )16 disp ( r e m )
17 / / a n sw e rs i n b i n a ry f or m //
Scilab code Exa 2.14 Convert 1010111010 to hexadecimal
12
8/18/2019 Digital Electronics- An Introduction to Theory and Practice_W. H. Gothmann
14/104
1 // E xampl e 2−14//
2 / / b i n a r y t o h e xa d e ci m a l c o n v e r s i o n //3 x = b i n 2 d e c ( ’ 1010 11101 0 ’ )4 / / d e ci m a l e q u i v a l e n t o f t h e b i n a ry number //5 a = dec2hex ( x )
6 / / Hex e q u i v a l e n t o f t h e d e ci m a l number //7 disp ( a )
8 / / a n sw er i n h e xa d e ci m a l f or m //
Scilab code Exa 2.15 Convert 11011110101110 to hexadecimal
1 // E xampl e 2−15//2 / / b i n a r y t o h e xa d e ci m a l c o n v e r s i o n //3 x = b i n 2 d e c ( ’ 1 1 0 1 1 1 1 0 1 0 1 1 1 0 ’ )4 / / d e ci m a l e q u i v a l e n t o f t h e b i n a ry number //5 a = dec2hex ( x )
6 / / Hex e q u i v a l e n t o f t h e d e ci m a l number //7 disp ( a )
8 / / a n sw er i n h e xa d e ci m a l f or m //
Scilab code Exa 2.16 Convert 4A8C to binary
1 // E xampl e 2−16//2 / / H ex ad ec im al t o b i n a r y c o n v e r s i o n //3 x = hex2dec ( ’ 4A8C ’ )4 / / d e ci m a l c o n v e r s i o n o f t h e h e xa d ec i ma l number //5 a = d e c 2 b i n ( x )
6 // B i n ar y e q u i v a l e n t o f t h e d e c im a l number //
7 disp ( a )8 / / a ns we r i n b i n a r y f or m //
13
8/18/2019 Digital Electronics- An Introduction to Theory and Practice_W. H. Gothmann
15/104
Scilab code Exa 2.17 Convert FACE to binary
1 // E xampl e 2−17//2 / / H ex ad ec im al t o b i n a r y c o n v e r s i o n //3 x = hex2dec ( ’FACE ’ )4 / / d e ci m a l c o n v e r s i o n o f t h e h e xa d ec i ma l number //5 a = d e c 2 b i n ( x )
6 // B i n ar y e q u i v a l e n t o f t h e d e c im a l number //7 disp ( a )
8 / / a ns we r i n b i n a r y f or m //
Scilab code Exa 2.18 Convert 2C9 to decimal
1 // E xampl e 2−18//2 / / H ex ad ec im al t o d e ci m a l c o n v e r s i o n / /3 a = hex2dec ( ’ 2C9 ’ )4 / / d e ci m a l e q u i v a l e n t o f t h e h e xa d ec i ma l number //5 disp ( a )
6 / / a ns we r i n d e c im a l f or m //
Scilab code Exa 2.19 Convert EB4A to decimal
1 // E xampl e 2−19//2 / / H ex ad ec im al t o d e ci m a l c o n v e r s i o n / /3 a = hex2dec ( ’EB4A ’ )4 / / d e ci m a l e q u i v a l e n t o f t h e h e xa d ec i ma l number //5 disp ( a )
6 / / a ns we r i n d e c im a l f or m //
Scilab code Exa 2.20 Convert 423 to hexadecimal
14
8/18/2019 Digital Electronics- An Introduction to Theory and Practice_W. H. Gothmann
16/104
1 // E xampl e 2−20//
2 / / D ec im al t o h e xa d ec i ma l c o n v e r s i o n / /3 a = dec2hex (423)4 / / h e xa d e ci m a l e q u i v a l e n t o f t h e d e c im a l number //5 disp ( a )
6 / / a n sw er i n h e xa d e ci m a l f or m //
Scilab code Exa 2.21 Convert 72905 to hexadecimal
1 // E xampl e 2−
21//2 / / D ec im al t o h e xa d ec i ma l c o n v e r s i o n / /3 a = dec2hex (72905)
4 / / h e xa d e ci m a l e q u i v a l e n t o f t h e d e c im a l number //5 disp ( a )
6 / / a n sw er i n h e xa d e ci m a l f or m //
Scilab code Exa 2.22 Add 1A8 and 67B
1 // E xampl e 2−22//2 / / H e xa d ec i ma l a d d i t i o n / /3 clc
4 // c l e a r s t he c o n s o l e //5 clear
6 // c l e a r s a l l e x i s t i n g v a r i a b l e s //7 x = hex2dec ( ’ 1A8 ’ )8 y = hex2dec ( ’ 6 7B ’ )9 / / D ec im al c o n v e r s i o n o f t h e h e xa d ec i ma l nu mbers / /
10 z = x + y
11 / / a d d i t i o n / /12 a = dec2hex ( z )
13 / / d e ci m a l t o h e xa d ec i ma l c o n v e r s i o n / /14 disp ( ’ a d d i ti o n o f t he 2 h ex ad ec im al numbers i s ’ )15 disp ( a )
15
8/18/2019 Digital Electronics- An Introduction to Theory and Practice_W. H. Gothmann
17/104
16 / / a n sw er i n h e xa d e ci m a l f or m //
Scilab code Exa 2.23 Add ACEF1 and 16B7D
1 // E xampl e 2−23//2 / / H e xa d ec i ma l a d d i t i o n / /3 clc
4 // c l e a r s t he c o n s o l e //5 clear
6 // c l e a r s a l l e x i s t i n g v a r i a b l e s //7 x = hex2dec ( ’ ACEF1 ’ )8 y = hex2dec ( ’ 16B7D ’ )9 / / D ec im al c o n v e r s i o n o f t h e h e xa d ec i ma l nu mbers / /
10 z = x + y
11 / / a d d i t i o n / /12 a = dec2hex ( z )
13 / / d e ci m a l t o h e xa d ec i ma l c o n v e r s i o n / /14 disp ( ’ a d d i ti o n o f t he 2 h ex ad ec im al numbers i s ’ )15 disp ( a )
16 / / a n sw er i n h e xa d e ci m a l f or m //
Scilab code Exa 2.24 Subtract 3A8 from 1273
1 // E xampl e 2−24//2 / / H e xa d ec i ma l S u b t r a c t i o n / /3 clc
4 // c l e a r s t he c o n s o l e //5 clear
6 // c l e a r s a l l e x i s t i n g v a r i a b l e s //7 x = hex2dec ( ’ 127 3 ’ )8 y = hex2dec ( ’ 3A8 ’ )9 / / D ec im al c o n v e r s i o n o f t h e h e xa d ec i ma l nu mbers / /
10 z = x - y
16
8/18/2019 Digital Electronics- An Introduction to Theory and Practice_W. H. Gothmann
18/104
11 / / a d d i t i o n / /
12 a = dec2hex ( z )13 / / d e ci m a l t o h e xa d ec i ma l c o n v e r s i o n / /14 disp ( ’ a d d i ti o n o f t he 2 h ex ad ec im al numbers i s ’ )15 disp ( a )
16 / / a n sw er i n h e xa d e ci m a l f or m //
Scilab code Exa 2.25 Multiply 1A3 by 89
1 // E xampl e 6−
25//2 / / S o l v e m u l t i p l e o ut p ut e q u a t io n u s i n g mapping / /3 clc
4 / / c l e a r s t h e window / /5 clear
6 // c l e a r s a l l e x i s t i n g v a r i a b l e s //7 x = hex2dec ( ’ 1A3 ’ )8 y = hex2dec ( ’ 8 9 ’ )9 / / D ec im al c o n v e r s i o n o f t h e h e xa d ec i ma l nu mbers / /
10 z = x * y
11 / / m u l t i p l i c a t i o n / /
12 a = dec2hex ( z )13 / / d e ci m a l t o h e xa d ec i ma l c o n v e r s i o n / /14 disp ( ’ m u l t i p l i c a t i o n o f t he 2 h ex ad ec im al numbers
i s ’ )15 disp ( a )
16 / / a n sw er i n h e xa d e ci m a l f or m //
Scilab code Exa 2.26 Divide 1EC87 by A5
1 // E xampl e 2−26//2 / / H e xa d ec i ma l D i v i s i o n / /3 x = hex2dec ( ’ 1EC87 ’ )4 y = hex2dec ( ’ A5 ’ )
17
8/18/2019 Digital Electronics- An Introduction to Theory and Practice_W. H. Gothmann
19/104
5 r = modul o ( x , y )
6 / / f i n d i n g t h e r e ma i n de r / /7 z = x / y8 q = floor ( z )
9 / / f i n d i n g t h e q u o t i e n t //10 q u o = dec2hex ( q )
11 r e m = dec2hex ( r )
12 / / d e ci m a l t o b i n a r y c o n v e r s i o n s / /13 disp ( ’ t h e q u o t ie n t i s : ’ )14 disp ( q u o )
15 disp ( ’ t h e r e ma in de r i s : ’ )16 disp ( r e m )
17 / / a n sw e rs i n b i n a ry f or m //
Scilab code Exa 2.27 Convert 11111011110101 to octal
1 // E xampl e 2−27//2 // B i na ry t o o c t a l c o n v e r si o n //3 x = b i n 2 d e c ( ’ 1 1 1 1 1 0 1 1 1 1 0 1 0 1 ’ )4 / / d e ci m a l e q u i v a l e n t o f t h e b i n a ry number //
5 a = d e c 2 o c t ( x )6 // o c t a l e q u i v a l e n t o f t he d ec i ma l number //7 disp ( a )
8 / / an sw er i n o c t a l fo rm / /
Scilab code Exa 2.28 Convert 1011110100011000111 to octal
1 // E xampl e 2−28//
2 // B i na ry t o o c t a l c o n v e r si o n //3 x = b i n 2 d e c ( ’ 1 0 1 1 1 1 0 1 0 0 0 1 1 0 0 0 1 1 1 ’ )4 / / d e ci m a l e q u i v a l e n t o f t h e b i n a ry number //5 a = d e c 2 o c t ( x )
6 // o c t a l e q u i v a l e n t o f t he d ec i ma l number //
18
8/18/2019 Digital Electronics- An Introduction to Theory and Practice_W. H. Gothmann
20/104
7 disp ( a )
8 / / an sw er i n o c t a l fo rm / /
Scilab code Exa 2.29 Convert octal number 3674 to binary
1 // E xampl e 2−29//2 // o c t a l t o b i na r y c o n v e r si o n //3 x = o c t 2 d e c ( ’ 367 4 ’ )4 // d ec i ma l e q u i v a l e n t o f t he o c t a l number //
5 a = d e c 2 b i n ( x )6 / / b i n a r y e q u i v a l e n t o f t h e d e c im a l number //7 disp ( a )
8 / / a ns we r i n b i n a r y f or m //
Scilab code Exa 2.30 Minus 5 in twos complement form
1 // E xampl e 2−30//
2 // −
5 i n t w o s complement fo rm / /3 clc
4 // c l e a r s t he c o n s o l e //5 clear
6 // c l e a r s a l l e x i s t i n g v a r i a b l e s //7 x = 2 ^ 8
8 / / S m a l l e s t n i n e b i t number //9 y =5
10 z = x - y
11 / / s u b t r a c t i o n / /12 a = d e c 2 b i n ( z )
13 / / b i n a r y c o n v e r s i o n o f t h e d e c im a l number //14 disp ( ’ −5 i n t w o s complement form i s ’ )15 disp ( a )
16 / / a ns we r i n b i n a r y f or m //
19
8/18/2019 Digital Electronics- An Introduction to Theory and Practice_W. H. Gothmann
21/104
Scilab code Exa 2.31 12 bit twos complement
1 // E xampl e 2−31//2 // −4 ,−15 ,−17 i n t w o s complement fo rm / /3 clc
4 // c l e a r s t he c o n s o l e //5 clear
6 // c l e a r s a l l e x i s t i n g v a r i a b l e s //7 x = 2 ^ 1 2
8 / / S m a l l e s t n i n e b i t number //9 p =4
10 q = 1 5
11 r = 1 7
12 u = x - p
13 v = x - q
14 w = x - r
15 / / s u b t r a c t i o n / /16 a = d e c 2 b i n ( u )
17 b = d e c 2 b i n ( v )
18 c = d e c 2 b i n ( w )19 / / b i n a r y c o n v e r s i o n o f t h e d e c im a l number //20 disp ( ’ −4 i n t w o s complement form i s ’ )21 disp ( a )
22 disp ( ’ −15 i n t w o s complement form i s ’ )23 disp ( b )
24 disp ( ’ −17 i n t w o s complement form i s ’ )25 disp ( c )
26 / / a n sw e rs i n b i n a ry f or m //
Scilab code Exa 2.32 16 bit twos complement
1 // E xampl e 2−32//
20
8/18/2019 Digital Electronics- An Introduction to Theory and Practice_W. H. Gothmann
22/104
2 // −16000 i n t w o s co mpl em en t f or m //
3 clc4 // c l e a r s t he c o n s o l e //5 clear
6 // c l e a r s a l l e x i s t i n g v a r i a b l e s //7 x = 2 ^ 1 6
8 / / S m a l l e s t n i n e b i t number //9 y = 1 6 0 0 0
10 z = x - y
11 / / s u b t r a c t i o n / /12 a = d e c 2 b i n ( z )
13 / / b i n a r y c o n v e r s i o n o f t h e d e c im a l number //
14 disp ( ’ −
16000 i n t w o s complement fo rm i s ’ )15 disp ( a )
16 / / a ns we r i n b i n a r y f or m //
Scilab code Exa 2.33 Twos complement of minus4 second method
1 // E xampl e 2−33//2 // −4 i n t w o s c om pl em en t f or m by s e co n d method / /
3 clc4 / / c l e a r s t h e window / /5 clear
6 // c l e a r s a l l e x i s t i n g v a r i a b l e s //7 x = b i t c m p ( 4 , 8 )
8 / / c om pl em en t o f t h e d e c im a l number 4 ( 8 b i tr e p r e s e n t a t i o n ) //
9 y =1
10 z = x + y
11 / / 1 i s a dded t o t h e co mp lem en t //
12 a = d e c 2 b i n ( z )13 / / b i n a r y c o n v e r s i o n o f t h e d e c im a l number //14 disp ( ’ −4 i n t w o s complement form i s : ’ )15 disp ( a )
16 // r e s u l t i s d i s p la y e d //
21
8/18/2019 Digital Electronics- An Introduction to Theory and Practice_W. H. Gothmann
23/104
Scilab code Exa 2.34 Twos complement of minus17 second method
1 // E xampl e 2−34//2 // −17 i n t w o s co mp le men t f or m by s e co n d method / /3 clc
4 / / c l e a r s t h e window / /5 clear
6 // c l e a r s a l l e x i s t i n g v a r i a b l e s //
7 x = b i t c m p ( 1 7 , 8 )8 / / c om pl em en t o f t h e d e c im a l number 1 7 (8 b i t
r e p r e s e n t a t i o n ) //9 y =1
10 z = x + y
11 / / 1 i s a dde d t o t h e co mp lem en t //12 a = d e c 2 b i n ( z )
13 / / b i n a r y c o n v e r s i o n o f t h e d e c im a l number //14 disp ( ’ −17 i n t w o s complement form i s : ’ )15 disp ( a )
16 // r e s u l t i s d i s p la y e d //
Scilab code Exa 2.35 Twos complement of minus4 third method
1 // E xampl e 2−35//2 //−4 i n two ’ s c om pl em en t by t h i r d method / /3 clc
4 // c l e a r s t he c o n s o l e //5 clear
6 // c l e a r s a l l e x i s t i n g v a r i a b l e s //7 a = 0 0 0 0 0 1 0 0
8 c =0
9 z =0
22
8/18/2019 Digital Electronics- An Introduction to Theory and Practice_W. H. Gothmann
24/104
10 for i =1: 8
11 x ( i ) = modulo ( a , 1 0 )12 a = a / 1 0
13 a = floor ( a )
14 end
15 for i =1: 8
16 if c >1 then
17 break
18 end
19 if x ( i ) = = 1 then
20 for k = i +1 : 8
21 x ( k ) = b i t c m p ( x ( k ) , 1 )
22 c = c + 123 end
24 end
25 end
26 for i =1: 8
27 z = z + x ( i ) * 1 0 ^ ( i - 1 )
28 end
29 disp ( ’−4 i n t wo s complement i s : ’ )30 disp ( z )
31 // a ns we r i s d i s p l a y e d //
Scilab code Exa 2.36 Twos complement of minus17 third method
1 // E xampl e 2−36//2 //−17 i n two ’ s c om pl em en t by t h i r d m et ho d / /3 clc
4 // c l e a r s t he c o n s o l e //5 clear
6 // c l e a r s a l l e x i s t i n g v a r i a b l e s //7 a = 0 0 0 1 0 0 0 18 c =0
9 z =0
10 for i =1: 8
23
8/18/2019 Digital Electronics- An Introduction to Theory and Practice_W. H. Gothmann
25/104
11 x ( i ) = modulo ( a , 1 0 )
12 a = a / 1 013 a = floor ( a )
14 end
15 for i =1: 8
16 if c >1 then
17 break
18 end
19 if x ( i ) = = 1 then
20 for k = i +1 : 8
21 x ( k ) = b i t c m p ( x ( k ) , 1 )
22 c = c + 1
23 end24 end
25 end
26 for i =1: 8
27 z = z + x ( i ) * 1 0 ^ ( i - 1 )
28 end
29 disp ( ’ −17 i n t wo s co mp le men t i s : ’ )30 disp ( z )
31 // a ns we r i s d i s p l a y e d //
Scilab code Exa 2.37 Negative decimal number represented by 10011011
1 // E xampl e 2−37//2 / / n e g at i v e d ec im al r e p r e s e n t a t i o n o f 1 00 11 01 1/ /3 clc
4 / / c l e a r s t h e window / /5 clear
6 // c l e a r s a l l e x i s t i n g v a r i a b l e s //
7 b = b i n 2 d e c ( ’ 10011 011 ’ )8 x = b i t c m p ( b , 8 )9 / / c om pl em en t o f t h e d e c im a l number 1 7 (8 b i t
r e p r e s e n t a t i o n ) //10 y =1
24
8/18/2019 Digital Electronics- An Introduction to Theory and Practice_W. H. Gothmann
26/104
11 z = x + y
12 / / 1 i s a dde d t o t h e co mp lem en t //13 a = d e c 2 b i n ( z )14 / / b i n a r y c o n v e r s i o n o f t h e d e c im a l number //15 z = z * ( - 1 )
16 disp ( ’ t he n e g at i v e v al ue t ha t 1 00 11 01 1 r e p r e s e n t si s : ’ )
17 disp ( z )
18 // r e s u l t i s d i s p la y e d //
Scilab code Exa 2.38 Add minus 17 to minus 30
1 // E xampl e 2−38//2 //add −17 t o −30//3 clc
4 // c l e a r s t he c o n s o l e //5 clear
6 // c l e a r s a l l e x i s i t i n g v a r i a b l e s //7 x = b i t c m p ( 1 7 , 8 )
8 y = b i t c m p ( 3 0 , 8 )
9 / / c om pl em en t o f t h e d e c i m a l n um be rs 1 7 a nd 3 0/ /10 z =1
11 u = x + z
12 v = y + z
13 / / 1 i s a dde d t o t h e c om pl em en ts / /14 w = u + v
15 a = d e c 2 b i n ( w )
16 / / b i n a r y c o n v e r s i o n o f t h e d e c im a l number //17 disp ( ’ b i n a r y f or m o f number o b t a i ne d by a d di ng −17
t o −30 ’ )
18 disp ( a )19 // r e s u l t i s d i s p la y e d //20 disp ( ’ t he msb i s d i s c a r d e d , s o e i g h t b i t
r e p r e s e n t a t i o n i s t he an s we r i n b in ar y form ’ )21 a = d e c 2 b i n ( w - ( 2 ^ 8 ) )
25
8/18/2019 Digital Electronics- An Introduction to Theory and Practice_W. H. Gothmann
27/104
22 disp ( a )
23 // f i n a l r e s u l t i s d i sp l ay e d //
Scilab code Exa 2.39 Add minus 20 to 26
1 // E xampl e 2−39//2 //add −20 t o 2 6/ /3 clc
4 // c l e a r s t he c o n s o l e //
5 clear6 // c l e a r s a l l e x i s t i n g v a r i a b l e s //7 x = b i t c m p ( 2 0 , 8 )
8 / / f i n d s c om pl em en t o f 2 9 //9 y =1
10 u = x + y
11 / / 1 i s a dde d t o t h e co mp lem en t //12 v = 2 6
13 w = u + v
14 a = d e c 2 b i n ( w )
15 / / b i n a r y c o n v e r s i o n o f t h e d e c im a l number //
16 disp ( ’ b i na r y form o f t he number o b ta i ne d by a dd in g−20 t o 26 ’ )
17 disp ( a )
18 // r e s u l t i s d i s p la y e d //19 disp ( ’ t he msb i s d i s c a r d e d , s o e i g h t b i t
r e p r e s e n t a t i o n i s t he an s we r i n b in ar y form ’ )20 a = d e c 2 b i n ( w - ( 2 ^ 8 ) )
21 disp ( a )
22 // f i n a l r e s u l t i s d i sp l ay e d //
Scilab code Exa 2.40 Add minus 29 to 14
1 // E xampl e 2−40//
26
8/18/2019 Digital Electronics- An Introduction to Theory and Practice_W. H. Gothmann
28/104
2 //add −29 t o 1 4/ /
3 clc4 // c l e a r s t he c o n s o l e //5 clear
6 // c l e a r s a l l e x i s t i n g v a r i a b l e s //7 x = b i t c m p ( 2 9 , 8 )
8 / / f i n d s c om pl em en t o f 2 9 //9 y =1
10 u = x + y
11 / / 1 i s a dde d t o t h e co mp lem en t //12 v = 1 4
13 w = u + v
14 a = d e c 2 b i n ( w )15 / / b i n a r y c o n v e r s i o n o f t h e d e c im a l number //16 disp ( ’ b i na r y form o f t he number o b ta i ne d by a dd in g
−29 t o 14 ’ )17 disp ( a )
18 // r e s u l t i s d i s p la y e d //
Scilab code Exa 2.41 Ones complement of minus 13
1 // E xampl e 2−41//2 / / o ne ’ s c om pl em en t f or m o f −13//3 clc
4 / / c l e a r s t h e window / /5 clear
6 // c l e a r s a l l e x i s t i n g v a r i a b l e s //7 x = b i t c m p ( 1 3 , 8 )
8 / / c o mp le me nt o f 1 3 //9 a = d e c 2 b i n ( x )
10 / / b i n a r y c o n v e r s i o n o f t h e d e c im a l number //11 disp ( ’ o n es c om pl em en t f or m o f −13 ’ )12 disp ( a )
13 // r e s u l t i s d i s p la y e d //
27
8/18/2019 Digital Electronics- An Introduction to Theory and Practice_W. H. Gothmann
29/104
Scilab code Exa 2.42 Ones complement of minus 13 second method
1 // E xampl e 2−42//2 / / o n e s c om pl em en t o f −13 b y 2 n d m et ho d / /3 clc
4 / / c l e a r s t h e window / /5 clear
6 // c l e a r s a l l e x i s t i n g v a r i a b l e s //7 a = b i t c m p ( 0 , 8 )
8 / / d e ci m a l e q u i v a l e n t o f 1 1 11 11 1 1/ /9 b = 1 3
10 c = a - b
11 / / s u b t r a c t i n g 13 from d e ci m al e q u i v a l e n t o f 1 1 1 1 1 1 1 1 / /
12 z = d e c 2 b i n ( c )
13 disp ( ’ o n e s c om pl em en t o f −13 i s : ’ )14 disp ( z )
15 // r e s u l t i s d i s p la y e d //
Scilab code Exa 2.43 Ones complement addition using 4 bits
1 // E xampl e 2−43//2 //add −3 t o −2 i n one ’ s c om pl em ent u s i n g 4 b i t s / /3 clc
4 / / c l e a r s t h e window / /5 clear
6 // c l e a r s a l l t h e e x i s t i n g v a r i a b l e s //
7 x = b i t c m p ( 3 , 4 )8 y = b i t c m p ( 2 , 4 )
9 / / c o mp le me nt o f t h e d e c i m a l number 2 //10 z = x + y + 1
11 / / c a r r y i s a dd ed / /
28
8/18/2019 Digital Electronics- An Introduction to Theory and Practice_W. H. Gothmann
30/104
12 a = d e c 2 b i n ( z )
13 / / b i n a r y c o n v e r s i o n / /14 disp ( ’ b i n a r y f or m o f t h e number o b t a i n ed by a d di n g−3 t o −2 ’ )
15 disp ( a )
16 // r e s u l t i s d i s p la y e d //17 disp ( ’ msb i s d is ca rd ed , 4 b i t r e p r e s e n t a t i o n i s t he
a ns we r i n b i n a r y f or m ’ )18 a = d e c 2 b i n ( z - ( 2 ^ 4 ) )
19 disp ( a )
20 // F i na l r e s u l t i s d i s p l a y e d //
Scilab code Exa 2.44 Ones complement addition using 4 bits
1 // E xampl e 2−44//2 //add −3 t o 2 i n one ’ s complement u s i ng 4 b i t s //3 clc
4 / / c l e a r s t h e window / /5 clear
6 // c l e a r s a l l t h e e x i s t i n g v a r i a b l e s //
7 x =28 y = b i t c m p ( 3 , 4 )
9 / / c o mp le me nt o f t h e d e c i m a l number 2 //10 z = x + y
11 a = d e c 2 b i n ( z )
12 / / b i n a r y c o n v e r s i o n / /13 disp ( ’ b i n a r y f or m o f t h e number o b t a i n ed by a d di n g
−3 t o 2 ’ )14 disp ( a )
15 // r e s u l t i s d i s p la y e d //
Scilab code Exa 2.45 Ones complement addition using 8 bits
29
8/18/2019 Digital Electronics- An Introduction to Theory and Practice_W. H. Gothmann
31/104
1 // E xampl e 2−45//
2 / / add 3 t o −
2 i n one ’ s co mp le men t u s i n g 8 b i t s / /3 clc4 / / c l e a r s t h e window / /5 clear
6 // c l e a r s a l l t h e e x i s t i n g v a r i a b l e s //7 x =3
8 y = b i t c m p ( 2 , 8 )
9 / / c o mp le me nt o f t h e d e c i m a l number 2 //10 z = x + y + 1
11 / / c a r r y i s a dd ed / /12 a = d e c 2 b i n ( z )
13 / / b i n a r y c o n v e r s i o n / /14 disp ( ’ b i n a r y f or m o f t h e number o b t a i n ed by a d di n g 3
t o −2 ’ )15 disp ( a )
16 // r e s u l t i s d i s p la y e d //17 disp ( ’ msb i s d is ca rd ed , 8 b i t r e p r e s e n t a t i o n i s t he
a ns we r i n b i n a r y f or m ’ )18 a = d e c 2 b i n ( z - ( 2 ^ 8 ) )
19 disp ( a )
20 // F i na l r e s u l t i s d i s p l a y e d //
Scilab code Exa 2.46 Convert 1101 point 000101 to decimal
1 // E xampl e 2−46//2 / / C o n v er s i o n t o d e c i m a l / /3 clc
4 // c l e a r s t he c o n s o l e //5 clear
6 // c l e a r s a l l e x i s t i n g v a r i a b l e s //7 p =18 q =1
9 z =0
10 b =0
30
8/18/2019 Digital Electronics- An Introduction to Theory and Practice_W. H. Gothmann
32/104
11 w =0
12 f =013 // i n i t i a l i s i n g / /14 / / b i n=i n p u t ( e n t e r t h e b i n a r y number t o be c o n v e rt e d
t o i t s d e ci m al fo rm ) / /15 // a c c e p ti n g t he i n pu t from t he u s e r //16 b i n = 1 1 0 1 . 0 0 0 1 0 1
17 d = modul o ( b i n , 1 )
18 / / s e p a r at i n g t he d ec im al p ar t from t he i n t e g e r p ar t//
19 d = d * 1 0 ^ 1 0
20 a = floor ( b i n )
21 / / r em o vi n g t h e d e c i m a l p a r t / /22 while ( a > 0 )
23 / / l oo p t o e n t e r t h e b in ar y b i t s o f t he i n t e g e r p ar ti n t o a m a tr i x / /
24 r = modul o ( a , 1 0 )
25 b ( 1 , q ) = r
26 a = a / 1 0
27 a = floor ( a )
28 q = q + 1
29 end
30 for m = 1: q - 1
31 / / m u l t i pl y i n g e a c h b i t o f t h e i n t e g e r p ar t w i t h i t sc o r r e sp o n di n g p o s i t i o n a l v al ue and a dd in g //
32 c = m - 1
33 f = f + b ( 1 , m ) * ( 2 ^ c )
34 end
35 while ( d > 0 )
36 / / l oo p t o t ak e t he b i t s o f t he d ec im al p ar t i n t o amat r i x //
37 e = modul o ( d , 2 )
38 w ( 1 , p ) = e
39 d = d / 1 040 d = floor ( d )
41 p = p + 1
42 end
43 for n = 1: p - 1
31
8/18/2019 Digital Electronics- An Introduction to Theory and Practice_W. H. Gothmann
33/104
44 / / m u l t i p l y i n g e a ch b i t w i th i t s c o r r e sp o n di n g
p o s i t i o n a l v al ue and a dd in g //45 z = z + w ( 1 , n ) * ( 0 . 5 ) ^ ( 1 1 - n )46 end
47 z = z * 1 0 0 0 0
48 z = round ( z )
49 // r ou nd in g o f f t o 4 d e ci m al p l a c e s //50 z = z / 1 0 0 0 0
51 x = f + z
52 disp ( ’ t he d e ci m al e q u i v a l e n t o f t he b i na r y number i s: ’ )
53 disp ( x )
54 // r e s u l t i s d i s p la y e d //
Scilab code Exa 2.47 Convert point 375 to binary
1 // E xampl e 2−47//2 / / C o nv e rs i o n o f d e ci m a l t o b i n a r y / /3 clc
4 // c l e a r s t he c o n s o l e //
5 clear6 // c l e a r s a l l e x i s t i n g v a r i a b l e s //7 q =0
8 b =0
9 s =0
10 // i n i t i a l i s i n g / /11 / / a=i n p u t ( e n t e r t h e d e c i m a l number t o b e c o n v e r t e d
t o i t s b i n ar y form )12 / / t a k i n g i n p u t f ro m t h e u s e r / /13 a = 0 . 3 7 5
14 d = modul o ( a , 1 )15 // s e p a r a t i n g t he d ec i ma l p a rt fro m t he i n t e g e r //16 a = floor ( a )
17 / / r em o vi n g t h e d e c i m a l p a r t / /18 while ( a > 0 )
32
8/18/2019 Digital Electronics- An Introduction to Theory and Practice_W. H. Gothmann
34/104
19 // i n t e g e r p ar t c on v e rt e d t o e q u i v a l e n t b i n ar y form //
20 x = modul o ( a , 2 )21 b = b + ( 1 0 ^ q ) * x
22 a = a / 2
23 a = floor ( a )
24 q = q + 1
25 end
26 for i = 1: 1 0
27 / / t a ki n g v a l ue s a f t e r t he d ec im al p ar t andc o n ve r t i n g t o e q u i v a l e n t b i n a ry form //
28 d = d * 2
29 q = floor ( d )
30 s = s + q / ( 1 0 ^ i )31 if d > = 1 then
32 d = d - 1
33 end
34 end
35 k = b + s
36 disp ( ’ t he d e ci m al number i n b i na r y fo rm i s : ’ )37 disp ( k )
38 // r e s u l t i s d i s p la y e d //
Scilab code Exa 2.48 Convert point 54545 to binary
1 // E xampl e 2−48//2 / / C o nv e rs i o n o f d e ci m a l t o b i n a r y / /3 clc
4 // c l e a r s t he c o n s o l e //5 clear
6 // c l e a r s a l l e x i s t i n g v a r i a b l e s //
7 q =08 b =0
9 s =0
10 // i n i t i a l i s i n g / /11 / / a=i n p u t ( e n t e r t h e d e c i m a l number t o b e c o n v e r t e d
33
8/18/2019 Digital Electronics- An Introduction to Theory and Practice_W. H. Gothmann
35/104
t o i t s b i n ar y form )
12 / / t a k i n g i n p u t f ro m t h e u s e r / /13 a = 0 . 5 4 5 4 514 d = modul o ( a , 1 )
15 // s e p a r a t i n g t he d ec i ma l p a rt fro m t he i n t e g e r //16 a = floor ( a )
17 / / r em o vi n g t h e d e c i m a l p a r t / /18 while ( a > 0 )
19 // i n t e g e r p ar t c on v e rt e d t o e q u i v a l e n t b i n ar y form //20 x = modul o ( a , 2 )
21 b = b + ( 1 0 ^ q ) * x
22 a = a / 2
23 a = floor ( a )24 q = q + 1
25 end
26 for i = 1: 1 0
27 / / t a ki n g v a l ue s a f t e r t he d ec im al p ar t andc o n ve r t i n g t o e q u i v a l e n t b i n a ry form //
28 d = d * 2
29 q = floor ( d )
30 s = s + q / ( 1 0 ^ i )
31 if d > = 1 then
32 d = d - 1
33 end
34 end
35 k = b + s
36 disp ( ’ t he d e ci m al number i n b i na r y fo rm i s : ’ )37 disp ( k )
38 // r e s u l t i s d i s p la y e d //
Scilab code Exa 2.49 Convert 38 point 21 to binary
1 // E xampl e 2−49//2 / / C o nv e rs i o n o f d e ci m a l t o b i n a r y / /3 clc
34
8/18/2019 Digital Electronics- An Introduction to Theory and Practice_W. H. Gothmann
36/104
4 // c l e a r s t he c o n s o l e //
5 clear6 // c l e a r s a l l e x i s t i n g v a r i a b l e s //7 q =0
8 b =0
9 s =0
10 // i n i t i a l i s i n g / /11 / / a=i n p u t ( e n t e r t h e d e c i m a l number t o b e c o n v e r t e d
t o i t s b i n ar y form )12 / / t a k i n g i n p u t f ro m t h e u s e r / /13 a = 3 8 . 2 1
14 d = modul o ( a , 1 )
15 // s e p a r a t i n g t he d ec i ma l p a rt fro m t he i n t e g e r //16 a = floor ( a )
17 / / r em o vi n g t h e d e c i m a l p a r t / /18 while ( a > 0 )
19 // i n t e g e r p ar t c on v e rt e d t o e q u i v a l e n t b i n ar y form //20 x = modul o ( a , 2 )
21 b = b + ( 1 0 ^ q ) * x
22 a = a / 2
23 a = floor ( a )
24 q = q + 1
25 end
26 for i = 1: 1 0
27 / / t a ki n g v a l ue s a f t e r t he d ec im al p ar t andc o n ve r t i n g t o e q u i v a l e n t b i n a ry form //
28 d = d * 2
29 q = floor ( d )
30 s = s + q / ( 1 0 ^ i )
31 if d > = 1 then
32 d = d - 1
33 end
34 end
35 k = b + s36 disp ( ’ t h e i n t e g e r p ar t o f t h e b in ar y for m i s : ’ )37 disp ( b )
38 disp ( ’ t h e f r a c t i o n a l p ar t o f t h e b in ar y form i s : ’ )39 disp ( s )
35
8/18/2019 Digital Electronics- An Introduction to Theory and Practice_W. H. Gothmann
37/104
40 // r e s u l t i s d i s p la y e d //
Scilab code Exa 2.50 Binary addition
1 // E xampl e 2−50//2 / / a d d i t i o n o f b i n a r y n umb ers / /3 // t h i s program r e q u i r e s f u n c t i o n s b i n ar y 2 de c im a l . s c i
and d e c i m a l 2 b i n a r y . s c i / /4 clc
5 / / c l e a r s t h e window / /6 clear
7 // c l e a r s a l l e x i s t i n g v a r i a b l e s //8 function x = b i n a r y 2 d e c i m a l ( b i n )
9 p =1
10 q =1
11 z =0
12 b =0
13 w =0
14 f =0
15 // i n i t i a l i s i n g / /
16 d = modul o ( b i n , 1 )17 / / s e p a r at i n g t he d ec im al p ar t from t he i n t e g e r p ar t
//18 d = d * 1 0 ^ 1 0
19 a = floor ( b i n )
20 / / r em o vi n g t h e d e c i m a l p a r t / /21 while ( a > 0 )
22 / / l oo p t o e n t e r t h e b in ar y b i t s o f t he i n t e g e r p ar ti n t o a m at ri x //
23 r = modul o ( a , 1 0 )
24 b ( 1 , q ) = r25 a = a / 1 0
26 a = floor ( a )
27 q = q + 1
28 end
36
8/18/2019 Digital Electronics- An Introduction to Theory and Practice_W. H. Gothmann
38/104
29 for m = 1: q - 1
30 / / m u l t i pl y i n g e a c h b i t o f t h e i n t e g e r p ar t w i t h i t sc o r r e sp o n di n g p o s i t i o n a l v al ue and a dd in g //31 c = m - 1
32 f = f + b ( 1 , m ) * ( 2 ^ c )
33 end
34 while ( d > 0 )
35 / / l oo p t o t ak e t he b i t s o f t he d ec im al p ar t i n t o amat r i x //
36 e = modul o ( d , 2 )
37 w ( 1 , p ) = e
38 d = d / 1 0
39 d = floor ( d )40 p = p + 1
41 end
42 for n = 1: p - 1
43 / / m u l t i p l y i n g e a ch b i t w i th i t s c o r r e sp o n di n gp o s i t i o n a l v al ue and a dd in g //
44 z = z + w ( 1 , n ) * ( 0 . 5 ) ^ ( 1 1 - n )
45 end
46 z = z * 1 0 0 0 0
47 z = round ( z )
48 // r ou nd in g o f f t o 4 d e ci m al p l a c e s //49 z = z / 1 0 0 0 0
50 x = f + z
51 e n d f u n c t i o n
52 function y = d e c i m a l 2 b i n a r y ( a )
53 q =0
54 b =0
55 s =0
56 // i n i t i a l i s i n g / /57 d = modul o ( a , 1 )
58 // s e p a r a t i n g t he d ec i ma l p a rt fro m t he i n t e g e r //
59 a = floor ( a )60 / / r em o vi n g t h e d e c i m a l p a r t / /61 while ( a > 0 )
62 // i n t e g e r p ar t c on v e rt e d t o e q u i v a l e n t b i n ar y form //63 x = modul o ( a , 2 )
37
8/18/2019 Digital Electronics- An Introduction to Theory and Practice_W. H. Gothmann
39/104
64 b = b + ( 1 0 ^ q ) * x
65 a = a / 266 a = floor ( a )
67 q = q + 1
68 end
69 for i = 1: 1 0
70 / / t a ki n g v a l ue s a f t e r t he d ec im al p ar t andc o n ve r t i n g t o e q u i v a l e n t b i n a ry form //
71 d = d * 2
72 q = floor ( d )
73 s = s + q / ( 1 0 ^ i )
74 if d > = 1 then
75 d = d - 176 end
77 end
78 y = b + s
79 e n d f u n c t i o n
80 x = b i n a r y 2 d e c i m a l ( 1 1 . 0 1 1 )
81 y = b i n a r y 2 d e c i m a l ( 1 0 . 0 0 1 )
82 z = x + y
83 a = d e c i m a l 2 b i n a r y ( z )
84 disp ( ’ t he a d d i ti o n o f t he b i n ar y numbers i s : ’ )85 disp ( a )
86 // r e s u l t i s d i s p la y e d //
Scilab code Exa 2.51 Binary mutiplication
1 // E xampl e 2−51//2 // m u l t i p l i c a t i o n o f b i na r y numbers //3 // t h i s program r e q u i r e s f u n c t i o n s b i n ar y 2 de c im a l . s c i
and d e c i m a l 2 b i n a r y . s c i / /4 clc5 / / c l e a r s t h e window / /6 clear
7 // c l e a r s a l l e x i s t i n g v a r i a b l e s //
38
8/18/2019 Digital Electronics- An Introduction to Theory and Practice_W. H. Gothmann
40/104
8 function x = b i n a r y 2 d e c i m a l ( b i n )
9 p =110 q =1
11 z =0
12 b =0
13 w =0
14 f =0
15 // i n i t i a l i s i n g / /16 d = modul o ( b i n , 1 )
17 / / s e p a r at i n g t he d ec im al p ar t from t he i n t e g e r p ar t//
18 d = d * 1 0 ^ 1 0
19 a = floor ( b i n )20 / / r em o vi n g t h e d e c i m a l p a r t / /21 while ( a > 0 )
22 / / l oo p t o e n t e r t h e b in ar y b i t s o f t he i n t e g e r p ar ti n t o a m at ri x //
23 r = modul o ( a , 1 0 )
24 b ( 1 , q ) = r
25 a = a / 1 0
26 a = floor ( a )
27 q = q + 1
28 end
29 for m = 1: q - 1
30 / / m u l t i pl y i n g e a c h b i t o f t h e i n t e g e r p ar t w i t h i t sc o r r e sp o n di n g p o s i t i o n a l v al ue and a dd in g //
31 c = m - 1
32 f = f + b ( 1 , m ) * ( 2 ^ c )
33 end
34 while ( d > 0 )
35 / / l oo p t o t ak e t he b i t s o f t he d ec im al p ar t i n t o amat r i x //
36 e = modul o ( d , 2 )
37 w ( 1 , p ) = e38 d = d / 1 0
39 d = floor ( d )
40 p = p + 1
41 end
39
8/18/2019 Digital Electronics- An Introduction to Theory and Practice_W. H. Gothmann
41/104
42 for n = 1: p - 1
43 / / m u l t i p l y i n g e a ch b i t w i th i t s c o r r e sp o n di n gp o s i t i o n a l v al ue and a dd in g //44 z = z + w ( 1 , n ) * ( 0 . 5 ) ^ ( 1 1 - n )
45 end
46 z = z * 1 0 0 0 0
47 z = round ( z )
48 // r ou nd in g o f f t o 4 d e ci m al p l a c e s //49 z = z / 1 0 0 0 0
50 x = f + z
51 e n d f u n c t i o n
52 function y = d e c i m a l 2 b i n a r y ( a )
53 q =054 b =0
55 s =0
56 // i n i t i a l i s i n g / /57 d = modul o ( a , 1 )
58 // s e p a r a t i n g t he d ec i ma l p a rt fro m t he i n t e g e r //59 a = floor ( a )
60 / / r em o vi n g t h e d e c i m a l p a r t / /61 while ( a > 0 )
62 // i n t e g e r p ar t c on v e rt e d t o e q u i v a l e n t b i n ar y form //63 x = modul o ( a , 2 )
64 b = b + ( 1 0 ^ q ) * x
65 a = a / 2
66 a = floor ( a )
67 q = q + 1
68 end
69 for i = 1: 1 0
70 / / t a ki n g v a l ue s a f t e r t he d ec im al p ar t andc o n ve r t i n g t o e q u i v a l e n t b i n a ry form //
71 d = d * 2
72 q = floor ( d )
73 s = s + q / ( 1 0 ^ i )74 if d > = 1 then
75 d = d - 1
76 end
77 end
40
8/18/2019 Digital Electronics- An Introduction to Theory and Practice_W. H. Gothmann
42/104
78 y = b + s
79 e n d f u n c t i o n80 x = b i n a r y 2 d e c i m a l ( 1 0 . 0 0 1 )
81 y = b i n a r y 2 d e c i m a l ( 0 . 1 1 )
82 z = x * y
83 a = d e c i m a l 2 b i n a r y ( z )
84 disp ( ’ t he m u l t i p l i c a t i o n o f t he b i n a ry numbers i s : ’)
85 disp ( a )
86 // r e s u l t i s d i s p la y e d //
41
8/18/2019 Digital Electronics- An Introduction to Theory and Practice_W. H. Gothmann
43/104
Chapter 3
Binary Codes
Scilab code Exa 3.1 Add 647 to 492 in BCD
1 // E xampl e 3−1//2 //BCD ad di t i on //3 clc
4 // c l e a r s t he c o n s o l e //5 clear
6 // c l e a r s a l l e x i s t i n g v a r i a b l e s //
7 a = 6 4 78 b = 4 9 2
9 for i =1: 3
10 x ( i ) = modulo ( a , 1 0 )
11 a = a / 1 0
12 a = floor ( a )
13 y ( i ) = modulo ( b , 1 0 )
14 b = b / 1 0
15 b = floor ( b )
16 end
17 d = x ( 1 ) + y ( 1 )18 d b = d e c 2 b i n ( d )
19 if d >9 then
20 d b = d e c 2 b i n ( d + 6 )
21 d b = d e c 2 b i n ( b i n 2 d e c ( d b ) - b i n 2 d e c ( ’ 1000 0 ’ ) )
42
8/18/2019 Digital Electronics- An Introduction to Theory and Practice_W. H. Gothmann
44/104
22 end
23 e = x ( 2 ) + y ( 2 )24 e b = d e c 2 b i n ( e )
25 if d >9 then
26 e b = d e c 2 b i n ( e + 1 )
27 e = e + 1
28 end
29 if e >9
30 e b = d e c 2 b i n ( e + 6 )
31 e b = d e c 2 b i n ( b i n 2 d e c ( e b ) - b i n 2 d e c ( ’ 1000 0 ’ ) )32 end
33 f = x ( 3 ) + y ( 3 )
34 f b = d e c 2 b i n ( f )35 if e >9 then
36 f b = d e c 2 b i n ( f + 1 )
37 f = f + 1
38 end
39 if f >9
40 f b = d e c 2 b i n ( f + 6 )
41 f b = d e c 2 b i n ( b i n 2 d e c ( f b ) - b i n 2 d e c ( ’ 1000 0 ’ ) )42 d c ( 4 ) = 1
43 end
44 d c ( 1 ) = b i n 2 d e c ( d b )
45 d c ( 2 ) = b i n 2 d e c ( e b )
46 d c ( 3 ) = b i n 2 d e c ( f b )
47 z =0
48 for i =1 : 4
49 z = z + d c ( i ) * ( 1 0 ^ ( i - 1 ) )
50 end
51 disp ( z )
52 disp ( ’ e q u i v a l e n t b i n a r y fo rm ’ )53 p = strcat ( d e c 2 b i n ( d c ( 4 ) , 1 ) + d e c 2 b i n ( d c ( 3 ) , 4 ) + d e c 2 b i n (
d c ( 2 ) , 4 ) + d e c 2 b i n ( d c ( 1 ) , 4 ) )
54 disp ( p )55 // a ns we r i s d i s p l a y e d //
43
8/18/2019 Digital Electronics- An Introduction to Theory and Practice_W. H. Gothmann
45/104
Scilab code Exa 3.2 Add 4318 and 7678 in BCD
1 // E xampl e 3−2//2 //BCD ad di t i on //3 clc
4 // c l e a r s t he c o n s o l e //5 clear
6 // c l e a r s a l l e x i s t i n g v a r i a b l e s //7 a = 4 3 1 8
8 b = 7 6 7 8
9 for i =1: 4
10 x ( i ) = modulo ( a , 1 0 )
11 a = a / 1 0
12 a = floor ( a )
13 y ( i ) = modulo ( b , 1 0 )
14 b = b / 1 0
15 b = floor ( b )
16 end
17 d = x ( 1 ) + y ( 1 )
18 d b = d e c 2 b i n ( d )19 if d >9 then
20 d b = d e c 2 b i n ( d + 6 )
21 d b = d e c 2 b i n ( b i n 2 d e c ( d b ) - b i n 2 d e c ( ’ 1000 0 ’ ) )22 end
23 e = x ( 2 ) + y ( 2 )
24 e b = d e c 2 b i n ( e )
25 if d >9 then
26 e b = d e c 2 b i n ( e + 1 )
27 e = e + 1
28 end
29 if e >9
30 e b = d e c 2 b i n ( e + 6 )
31 e b = d e c 2 b i n ( b i n 2 d e c ( e b ) - b i n 2 d e c ( ’ 1000 0 ’ ) )32 end
44
8/18/2019 Digital Electronics- An Introduction to Theory and Practice_W. H. Gothmann
46/104
33 f = x ( 3 ) + y ( 3 )
34 f b = d e c 2 b i n ( f )35 if e >9 then
36 f b = d e c 2 b i n ( f + 1 )
37 f = f + 1
38 end
39 if f >9
40 f b = d e c 2 b i n ( f + 6 )
41 f b = d e c 2 b i n ( b i n 2 d e c ( f b ) - b i n 2 d e c ( ’ 1000 0 ’ ) )42 end
43 g = x ( 4 ) + y ( 4 )
44 g b = d e c 2 b i n ( g )
45 if f >9 then46 g b = d e c 2 b i n ( g + 1 )
47 g = g + 1
48 end
49 if g >9
50 g b = d e c 2 b i n ( g + 6 )
51 g b = d e c 2 b i n ( b i n 2 d e c ( g b ) - b i n 2 d e c ( ’ 1000 0 ’ ) )52 d c ( 5 ) = 1
53 end
54 d c ( 1 ) = b i n 2 d e c ( d b )
55 d c ( 2 ) = b i n 2 d e c ( e b )
56 d c ( 3 ) = b i n 2 d e c ( f b )
57 d c ( 4 ) = b i n 2 d e c ( g b )
58 z =0
59 for i =1 : 5
60 z = z + d c ( i ) * ( 1 0 ^ ( i - 1 ) )
61 end
62 disp ( z )
63 disp ( ’ e q u i v a l e n t b i n a r y fo rm ’ )64 p = strcat ( d e c 2 b i n ( d c ( 5 ) , 1 ) + d e c 2 b i n ( d c ( 4 ) , 4 ) + d e c 2 b i n (
d c ( 3 ) , 4 ) + d e c 2 b i n ( d c ( 2 ) , 4 ) + d e c 2 b i n ( d c ( 1 ) , 4 ) )
65 disp ( p )66 // a ns we r i s d i s p l a y e d //
45
8/18/2019 Digital Electronics- An Introduction to Theory and Practice_W. H. Gothmann
47/104
Scilab code Exa 3.3 Add 3 and 2 in XS3
1 // E xampl e 3−3//2 // add 3 and 2 i n E xc es s 3 c o de / /3 clc
4 // c l e a r s t he c o n s o l e //5 clear
6 // c l e a r s a l l e x i s t i n g v a r i a b l e s //7 n =3
8 m =2
9 z =0
10 a = 0 1 1 0
11 b = 0 1 0 1
12 e a = d e c 2 b i n ( n + 3 )
13 e b = d e c 2 b i n ( m + 3 )
14 for i =1: 4
15 x ( i ) = modulo ( a , 1 0 )
16 a = a / 1 0
17 a = floor ( a )
18 y ( i ) = modulo ( b , 1 0 )19 b = b / 1 0
20 b = floor ( b )
21 end
22 for i =1: 4
23 g ( i ) = b i t a n d ( x ( i ) , y ( i ) )
24 p ( i ) = b i t o r ( x ( i ) , y ( i ) )
25 end
26 c ( 1 ) = 0
27 for i =1: 4
28 c ( i + 1 ) = b i t o r ( g ( i ) , b i t a n d ( p ( i ) , c ( i ) ) )
29 end
30 if c ( 5 ) = = 1 then
31 z = d e c 2 b i n ( b i n 2 d e c ( e a ) + b i n 2 d e c ( e b ) + 3 )
32 end
46
8/18/2019 Digital Electronics- An Introduction to Theory and Practice_W. H. Gothmann
48/104
33 if c ( 5 ) = = 0 then
34 z = d e c 2 b i n ( b i n 2 d e c ( e a ) + b i n 2 d e c ( e b ) - 3 )35 end
36 disp ( ’ e q u i v a l e n t b i n ar y number a f t e r e x c e s s 3a d d i t i o n ’ )
37 disp ( z )
38 disp ( ’ e q u i v a l e n t d e c i m a l number ’ )39 disp ( m + n )
40 // r e s u l t i s d i s p la y e d //
Scilab code Exa 3.4 Add 6 and 8 in XS3
1 // E xampl e 3−4//2 // add 6 and 8 i n E xc es s 3 c o de / /3 clc
4 // c l e a r s t he c o n s o l e //5 clear
6 // c l e a r s a l l e x i s t i n g v a r i a b l e s //7 n =6
8 m =8
9 a = 1 0 0 110 b = 1 0 1 1
11 e a = d e c 2 b i n ( n + 3 )
12 e b = d e c 2 b i n ( m + 3 )
13 for i =1: 4
14 x ( i ) = modulo ( a , 1 0 )
15 a = a / 1 0
16 a = floor ( a )
17 y ( i ) = modulo ( b , 1 0 )
18 b = b / 1 0
19 b = floor ( b )20 end
21 for i =1: 4
22 g ( i ) = b i t a n d ( x ( i ) , y ( i ) )
23 p ( i ) = b i t o r ( x ( i ) , y ( i ) )
47
8/18/2019 Digital Electronics- An Introduction to Theory and Practice_W. H. Gothmann
49/104
24 end
25 c ( 1 ) = 026 for i =1: 4
27 c ( i + 1 ) = b i t o r ( g ( i ) , b i t a n d ( p ( i ) , c ( i ) ) )
28 end
29 if c ( 5 ) = = 1 then
30 z = d e c 2 b i n ( b i n 2 d e c ( e a ) + b i n 2 d e c ( e b ) + 3 )
31 end
32 if c ( 5 ) = = 0 then
33 z = d e c 2 b i n ( b i n 2 d e c ( e a ) + b i n 2 d e c ( e b ) - 3 )
34 end
35 disp ( ’ e q u i v a l e n t b i n ar y number a f t e r e x c e s s 3
a d d i t i o n ’ )36 disp ( z )
37 disp ( ’ e q u i v a l e n t d e c i m a l number ’ )38 disp ( m + n )
39 // r e s u l t i s d i s p la y e d //
Scilab code Exa 3.5 Convert binary 1011 to Gray code
1 // E xampl e 3−
5//2 / / b i n a r y t o g ra y c od e / /3 clc
4 // c l e a r s t he c o n s o l e //5 clear
6 // c l e a r s a l l e x i s t i n g v a r i a b l e s //7 a = 1 0 1 1
8 for i =1: 4
9 x ( i ) = modulo ( a , 1 0 )
10 a = a / 1 0
11 a = floor ( a )12 end
13 y ( 4 ) = x ( 4 )
14 k =3
15 while ( k > 0 )
48
8/18/2019 Digital Electronics- An Introduction to Theory and Practice_W. H. Gothmann
50/104
16 if b i t a n d ( x ( k + 1 ) , x ( k ) ) = = 1 then
17 y ( k ) = b i t c m p ( 1 , 1 )18 else
19 y ( k ) = b i t o r ( x ( k + 1 ) , x ( k ) )
20 end
21 k = k - 1
22 end
23 / / d i s p l a y / /24 z =0
25 for i =1: 4
26 z = z + y ( i ) * ( 1 0 ^ ( i - 1 ) )
27 end
28 disp ( ’ e q u i v a l e n t g ra y c od e ’ )29 disp ( z )
30 // e q u i v a l e n t g ra y c od e i s d i s p l a y e d //
Scilab code Exa 3.6 Convert binary 1001011 to Gray code
1 // E xampl e 3−6//2 / / b i n a r y t o g ra y c od e / /
3 clc4 // c l e a r s t he c o n s o l e //5 clear
6 // c l e a r s a l l e x i s t i n g v a r i a b l e s //7 a = 1 0 0 1 0 1 1
8 for i =1: 7
9 x ( i ) = modulo ( a , 1 0 )
10 a = a / 1 0
11 a = floor ( a )
12 end
13 y ( 7 ) = x ( 7 )14 k =6
15 while ( k > 0 )
16 if b i t a n d ( x ( k + 1 ) , x ( k ) ) = = 1 then
17 y ( k ) = b i t c m p ( 1 , 1 )
49
8/18/2019 Digital Electronics- An Introduction to Theory and Practice_W. H. Gothmann
51/104
18 else
19 y ( k ) = b i t o r ( x ( k + 1 ) , x ( k ) )20 end
21 k = k - 1
22 end
23 / / d i s p l a y / /24 z =0
25 for i =1: 7
26 z = z + y ( i ) * ( 1 0 ^ ( i - 1 ) )
27 end
28 disp ( ’ e q u i v a l e n t g ra y c od e ’ )29 disp ( z )
30 // e q u i v a l e n t g ra y c od e i s d i s p l a y e d //
Scilab code Exa 3.7 Convert Gray code 1011 to binary
1 // E xampl e 3−7//2 / / g ra y c od e t o b i n a ry / /3 clc
4 // c l e a r s t he c o n s o l e //
5 clear6 // c l e a r s a l l e x i s t i n g v a r i a b l e s //7 a = 1 0 1 1
8 for i =1: 4
9 x ( i ) = modulo ( a , 1 0 )
10 a = a / 1 0
11 a = floor ( a )
12 end
13 y ( 4 ) = x ( 4 )
14 k =3
15 while ( k > 0 )16 if b i t a n d ( y ( k + 1 ) , x ( k ) ) = = 1 then
17 y ( k ) = b i t c m p ( 1 , 1 )
18 else
19 y ( k ) = b i t o r ( y ( k + 1 ) , x ( k ) )
50
8/18/2019 Digital Electronics- An Introduction to Theory and Practice_W. H. Gothmann
52/104
20 end
21 k = k - 122 end
23 z =0
24 for i =1: 4
25 z = z + y ( i ) * ( 1 0 ^ ( i - 1 ) )
26 end
27 disp ( ’ e q u i v a l e n t b i n a r y c od e ’ )28 disp ( z )
29 // e q u i v a l e n t b i na r y c od e i s d i s p l a y e d //
Scilab code Exa 3.8 Convert Gray code 1001011 to binary
1 // E xampl e 3−8//2 / / g ra y c od e t o b i n a ry / /3 clc
4 // c l e a r s t he c o n s o l e //5 clear
6 // c l e a r s a l l e x i s t i n g v a r i a b l e s //7 a = 1 0 0 1 0 1 1
8 for i =1: 79 x ( i ) = modulo ( a , 1 0 )
10 a = a / 1 0
11 a = floor ( a )
12 end
13 y ( 7 ) = x ( 7 )
14 k =6
15 while ( k > 0 )
16 if b i t a n d ( y ( k + 1 ) , x ( k ) ) = = 1 then
17 y ( k ) = b i t c m p ( 1 , 1 )
18 else19 y ( k ) = b i t o r ( y ( k + 1 ) , x ( k ) )
20 end
21 k = k - 1
22 end
51
8/18/2019 Digital Electronics- An Introduction to Theory and Practice_W. H. Gothmann
53/104
23 z =0
24 for i =1: 725 z = z + y ( i ) * ( 1 0 ^ ( i - 1 ) )
26 end
27 disp ( ’ e q u i v a l e n t b i n a r y c od e : ’ )28 disp ( z )
29 // e q u i v a l e n t b i na r y c od e i s d i s p l a y e d //
Scilab code Exa 3.9 Hamming code for 1011
1 // E xampl e 3−9//2 //Hamming cod e //3 clc
4 / / c l e a r s t h e command w in do w / /5 clear
6 // c l e a r s a l l e x i s t i n g v a r i a b l e s //7 z = 1 0 1 1
8 // in pu t //9 a = 0 ; b = 0 ; c = 0 ; d = 0 ;
10 / / t a k i n g t h e i n p u t / /
11 for i = 1 : 712 x ( i ) = 0
13 if i = = 3 then
14 x ( i ) = 1
15 end
16 if i = = 5 then
17 x ( i ) = 1
18 end
19 if i = = 7 then
20 x ( i ) = 1
21 end22 end
23 // e s t a b l i s h i n g e v e n p a r i t y a t p o s i t i o n s 1 , 3 , 5 , 7/ /24 for i =1: 7
25 if x ( i ) = = 1 then
52
8/18/2019 Digital Electronics- An Introduction to Theory and Practice_W. H. Gothmann
54/104
26 a = a + 1
27 end28 end
29 d = modul o ( a , 2 )
30 if ( d = = 1 ) then
31 x ( 1 ) = 1
32 end
33 // e s t a b l i s h i n g e v e n p a r i t y a t p o s i t i o n s 2 , 3 , 6 , 7/ /34 for i =2: 7
35 if ( i = = 5 ) then
36 continue
37 end
38 if ( x ( i ) = = 1 ) then39 b = b + 1
40 end
41 end
42 d = modul o ( b , 2 )
43 if ( d = = 1 ) then
44 x ( 2 ) = 1
45 end
46 // e s t a b l i s h i n g e v e n p a r i t y a t p o s i t i o n s 4 , 5 , 6 , 7/ /47 for i = 5 : 7
48 if ( x ( i ) = = 1 ) then
49 c = c + 1
50 end
51 end
52 d = modul o ( c , 2 )
53 if ( d = = 1 ) then
54 x ( 4 ) = 1
55 end
56 // d i s p l a y i n g t he r e s u l t //57 disp ( ’ t he hamming c od e f o r t h i s d at a i s g i ve n a s ’ )58 for i = 1 : 7
59 disp ( x ( i ) )60 end
53
8/18/2019 Digital Electronics- An Introduction to Theory and Practice_W. H. Gothmann
55/104
Scilab code Exa 3.10 Hamming code for 0101
1 // E xampl e 3−10//2 //Hamming cod e //3 clc
4 / / c l e a r s t h e command w in do w / /5 clear
6 // c l e a r s a l l e x i s t i n g v a r i a b l e s //7 z = 0 1 0 1
8 // in pu t //9 a = 0 ; b = 0 ; c = 0 ; d = 0 ;
10 / / t a k i n g t h e i n p u t / /11 for i = 1 : 7
12 x ( i ) = 0
13 if i = = 3 then
14 x ( i ) = 1
15 end
16 if i = = 6 then
17 x ( i ) = 1
18 end19 end
20 // e s t a b l i s h i n g e v e n p a r i t y a t p o s i t i o n s 1 , 3 , 5 , 7/ /21 for i =1: 7
22 if i = = 6 then
23 continue
24 end
25 if x ( i ) = = 1 then
26 a = a + 1
27 end
28 end
29 d = modul o ( a , 2 )
30 if ( d = = 1 ) then
31 x ( 1 ) = 1
32 end
54
8/18/2019 Digital Electronics- An Introduction to Theory and Practice_W. H. Gothmann
56/104
33 // e s t a b l i s h i n g e v e n p a r i t y a t p o s i t i o n s 2 , 3 , 6 , 7/ /
34 for i =2: 735 if x ( i ) = = 1 then
36 b = b + 1
37 end
38 end
39 d = modul o ( b , 2 )
40 if ( d = = 1 ) then
41 x ( 2 ) = 1
42 end
43 // e s t a b l i s h i n g e v e n p a r i t y a t p o s i t i o n s 4 , 5 , 6 , 7/ /44 for i = 5 : 7
45 if ( x ( i ) = = 1 ) then46 c = c + 1
47 end
48 end
49 d = modul o ( c , 2 )
50 if ( d = = 1 ) then
51 x ( 4 ) = 1
52 end
53 // d i s p l a y i n g t he r e s u l t //54 disp ( ’ t he hamming c od e f o r t h i s d at a i s g i ve n a s ’ )55 for i = 1 : 7
56 disp ( x ( i ) )
57 end
Scilab code Exa 3.11 Correction of Hamming code 1111101
1 // E xampl e 3−11//2 / / C o r r e c t i o n o f r e c e i v e d Hamming c o de / /
3 clc4 // c l e a r s t he c o n s o l e //5 clear
6 // c l e a r s a l l e x i s t i n g v a r i a b l e s //7 z = 1 1 1 1 1 0 1
55
8/18/2019 Digital Electronics- An Introduction to Theory and Practice_W. H. Gothmann
57/104
8 // i n c o r r e c t c od e t ak en a s i n pu t //
9 a = 0 ; b = 0 ; c = 0 ; d = 0 ;10 // t a k in g t he i n pu t i n an a r ra y //11 for i =1: 7
12 x ( i ) = 1
13 if i = = 2 then
14 x ( i ) = 0
15 end
16 end
17 / / c h e c ki n g f o r 4 , 5 , 6 , 7 e ve n p a r i t y //18 for i =4: 7
19 if x ( i ) = = 1 then
20 a = a + 121 end
22 end
23 d = modul o ( a , 2 )
24 if d = = 1 then
25 disp ( ’ w rong e n t r y f o r 4 , 5 , 6 , 7 ’ )26 x ( 4 ) = 1
27 else
28 disp ( ’ c o r r e c t e n tr y f o r 4 , 5 , 6 , 7 ’ )29 end
30 / / c h e c ki n g f o r 2 , 3 , 6 , 7 e ve n p a r i t y //31 for i =2: 7
32 if i = = 4 then
33 continue
34 end
35 if i = = 5 then
36 continue
37 end
38 if x ( i ) = = 1 then
39 a = a + 1
40 end
41 end42 d = modul o ( a , 2 )
43 if d = = 1 then
44 disp ( ’ w rong e n t r y f o r 2 , 3 , 6 , 7 ’ )45 x ( 2 ) = 1
56
8/18/2019 Digital Electronics- An Introduction to Theory and Practice_W. H. Gothmann
58/104
46 else
47 disp ( ’ c o r r e c t e n tr y f o r 2 , 3 , 6 , 7 ’ )48 end49 / / c h e c ki n g f o r 1 , 3 , 5 , 7 e ve n p a r i t y //50 for i =1: 7
51 if i = = 2 then
52 continue
53 end
54 if i = = 4 then
55 continue
56 end
57 if i = = 6 then
58 continue59 end
60 if x ( i ) = = 1 then
61 a = a + 1
62 end
63 end
64 d = modul o ( a , 2 )
65 if d = = 1 then
66 disp ( ’ c o r r e c t e n tr y f o r 1 , 3 , 5 , 7 ’ )67 x ( 1 ) = 1
68 else
69 disp ( ’ w rong e n t r y f o r 1 , 3 , 5 , 7 ’ )70 end
71 disp ( ’ T he re fo re , b i t 2 i s i n e r r o r and t he c o r r e c t e dc o d e i s : ’ )
72 for i =1: 7
73 disp ( x ( i ) )
74 end
75 // c o r r e c t c od e i s d i s p l a y e d //
57
8/18/2019 Digital Electronics- An Introduction to Theory and Practice_W. H. Gothmann
59/104
Chapter 4
Boolean Algebra
Scilab code Exa 4.1 Demorganize a function
1 // E xampl e 4−1//2 / / d e m o rg a n i ze f u n c t i o n / /3 clc
4 // c l e a r s t he c o n s o l e //5 clear
6 // c l e a r s a l l e x i s t i n g v a r i a b l e s //
7 // t he g i ve n f u n c ti o n i s a s f o l l o w s //8 disp ( ’ G iv en f u n c t i o n − (AB ’ ’+C) ’ ’ ’ )9 disp ( ’ now d e m or g a ni z i ng t h e f u n c t i o n ’ )
10 disp ( ’ c om pl em en t f u n c t i o n ’ )11 disp ( ’ AB ’ ’+C ’ )12 disp ( ’ c h an g in g o p e r a t o r s ’ )13 disp ( ’ (A+B ’ ’ ) (C) ’ )14 disp ( ’ c om pl em en t v a r i a b l e s ’ )15 disp ( ’ ( A ’ ’ +B) ( C ’ ’ ) ’ )16 // f i n a l a ns w e r d i sp l ay e d a f t e r s i m p l i f i c a t i o n //
Scilab code Exa 4.2 Reduce an expression
58
8/18/2019 Digital Electronics- An Introduction to Theory and Practice_W. H. Gothmann
60/104
1 // E xampl e 4−2//
2 / / R ed u ci n g an e x p r e s s i o n / /3 clc4 // c l e a r s t he c o n s o l e //5 clear
6 // c l e a r s a l l e x i s t i n g v a r i a b l e s //7 // t he g i ve n e x p r e s s i o n i s a s f o l l o w s //8 disp ( ’ G iv en E x p r e s si o n− A+B( C+(DE) ’ ’ ) ’ ’ ’ )9 disp ( ’ a p p l y i n g DeMo rga n t h e o r em ’ )
10 disp ( ’ r ed uc ed e x p r e s s i o n : ’ )11 disp ( ’ A+B(C+D ’ ’+E ’ ’ ) ’ ’ ’ )12 disp ( ’ A p p l y i n g DeMo rga n t h e o r em a g a i n ’ )
13 disp ( ’ A+B(C ’ ’DE) ’ )14 disp ( ’ T he re fo re f i n a l r e d u c e d e x p r e s s i o n i s : ’ )15 disp ( ’ A+BC ’ ’DE ’ )16 // f i n a l e x p r e s s i o n d i s pl a y e d //
Scilab code Exa 4.3 Reduce an expression
1 // E xampl e 4−3//
2 / / Re du ci ng a g i v e n e x p r e s s i o n //3 clc
4 // c l e a r s t he c o n s o l e //5 clear
6 // c l e a r s a l l e x i s t i n g v a r i a b l e s //7 // t he g i ve n e x p r e s s i o n i s a s f o l l o w s //8 disp ( ’ G iv en E x p r e s si o n− ( (AB) ’ ’+A ’ ’+AB) ’ ’ ’ )9 disp ( ’ ap pl y i ng DeM or gan T he or em ’ )
10 disp ( ’ r ed uc ed e x pr e s s i o n ’ )11 disp ( ’ (A ’ ’+B ’ ’+A ’ ’+AB) ’ ’ ’ )
12 disp ( ’ a s A+A=A ’ )13 disp ( ’ (A’ ’+B ’ ’+AB) ’ ’ ’ )14 / / By l a w 2 0 //15 disp ( ’ (A’ ’+B ’ ’+A) ’ ’ ’ )16 disp ( ’ r e a r r a n g i n g ’ )
59
8/18/2019 Digital Electronics- An Introduction to Theory and Practice_W. H. Gothmann
61/104
17 disp ( ’ (A+A ’ ’+B ’ ’ ) ’ ’ ’ )
18 / / by l aw 1 3/ /19 disp ( ’ (1+B ’ ’ ) ’ ’ ’ )20 / / by l aw 1 1/ /21 disp ( ’ 1 ’ ’ ’ )22 disp ( ’ 0 ’ )23 // f i n a l r ed uc ed e x p r e s s i o n i s d i s p la y e d /
Scilab code Exa 4.4 Reduce a boolean expression
1 // E xampl e 4−4//2 / / Re du ci ng a g i v e n e x p r e s s i o n //3 clc
4 / / c l e a r s t h e window / /5 clear
6 // c l e a r s a l l e x i s t i n g v a r i a b l e s //7 // t he g i ve n e x p r e s s i o n i s a s f o l l o w s //8 disp ( ’ G iv en E x p r e s si o n− AB+(AC) ’ ’+AB ’ ’C(AB+C) ’ )9 disp ( ’ M u l t i p l y i n g ’ )
10 disp ( ’ AB+(AC) ’ ’+AABB ’ ’C+AB ’ ’CC ’ )
11 / / u s i n g l a w s 1 8 , 6 , 8 , 9 / /12 disp ( ’ AB+(AC) ’ ’+AB ’ ’C ’ )13 disp ( ’ a p p l y i n g DeMo rga n t h e o r em ’ )14 disp ( ’ AB+A ’ ’+C ’ ’+AB ’ ’C ’ )15 disp ( ’ r e a r r a n g e ’ )16 disp ( ’ AB+C ’ ’+A ’ ’+AB ’ ’C ’ )17 / / r e d uc e u s i n g l aw 2 0/ /18 disp ( ’ AB+C ’ ’+A ’ ’+B ’ ’C ’ )19 disp ( ’ r e a r r a n g i n g a g a i n ’ )20 disp ( ’ A ’ ’+AB+C ’ ’+B ’ ’C ’ )
21 / / r e d uc e u s i n g l aw 2 0/ /22 disp ( ’ A ’ ’+B+C ’ ’+B ’ ’ ’ )23 / / u s i n g l a ws 1 1 and 1 3/ /24 disp ( ’ 1 ’ )25 // f i n a l r ed uc ed e x p r e s s i o n i s d i s p la y e d //
60
8/18/2019 Digital Electronics- An Introduction to Theory and Practice_W. H. Gothmann
62/104
Scilab code Exa 4.5 Reduce a Boolean expression
1 // E xampl e 4−5//2 / / Red uce a g i v e n e x p r e s s i o n //3 clc
4 / / c l e a r s t h e window / /5 clear
6 // c l e a r s a l l e x i s t i n g v a r i a b l e s //
7 // t he g i ve n e x p r e s s i o n i s a s f o l l o w s //8 disp ( ’ G iv en E x p r e s si o n− ( (AB ’ ’+ABC) ’ ’+A( B+AB ’ ’ ) ) ’ ’
’ )9 disp ( ’ f a c t o r i s e ’ )
10 disp ( ’ ( (A(B ’ ’+BC) ) ’ ’+A(B+AB ’ ’ ) ) ’ ’ ’ )11 / / r e d uc e u s i n g l a ws 1 8 and 2 0/ /12 disp ( ’ ( (A(B ’ ’+C) ) ’ ’+A(B+A) ) ’ ’ ’ )13 disp ( ’ m u l t i p l y ’ )14 disp ( ’ ( (AB ’ ’+AC) ’ ’+AA+AB) ’ ’ ’ )15 / / r e d u c e u s i n g l a w s 7 , 8 , 1 1 , 1 8 / /16 disp ( ’ ( (AB ’ ’+AC) ’ ’+A) ’ ’ ’ )17 disp ( ’ de mo r gan i z e ’ )18 disp ( ’ ( (A ’ ’+B) (A ’ ’+C ’ ’ )+A) ’ ’ ’ )19 disp ( ’ m u l t i p l y ’ )20 disp ( ’ (A ’ ’A’ ’+A ’ ’C ’ ’+A ’ ’B+BC ’ ’+A) ’ ’ ’ )21 / / Red uc e u s i n g l a w s 1 8 , 8 / /22 disp ( ’ (A ’ ’(1+C’ ’+B)+BC ’ ’+A) ’ ’ ’ )23 / / r e d u c e u s i n g l aw 1 8 , 7 , 1 1 / /24 disp ( ’ 1 ’ ’ ’ )25 disp ( ’ 0 ’ )26 // f i n a l r ed uc ed e x p r e s s i o n i s d i s p la y e d //
61
8/18/2019 Digital Electronics- An Introduction to Theory and Practice_W. H. Gothmann
63/104
Chapter 6
Combinational Logic
Scilab code Exa 6.1 Convert A plus B to minterms
1 // E xampl e 6−1//2 / / c o n v e r t A+B t o m i n t er m s / /3 clc
4 // c l e a r s t he c o n s o l e //5 clear
6 // c l e a r s a l l e x i s t i n g v a r i a b l e s //
7 / / c o n v e r s i o n t o m in te rm s / /8 disp ( ’ Given e x p r es s i o n − A+B ’ )9 disp ( ’ on s o l v i n g ’ )
10 disp ( ’ A(B+B ’ ’ )+B(A+A ’ ’ ) ’ )11 disp ( ’ m u l t i p l y i n g ’ )12 disp ( ’ AB+AB ’ ’+AB+A ’ ’B ’ )13 disp ( ’ we know A+A=A ’ )14 disp ( ’ AB+AB ’ ’+A ’ ’B ’ )15 // f i n a l r e s u l t i s d i sp l ay e d //
Scilab code Exa 6.2 Find minterms for A plus BC
62
8/18/2019 Digital Electronics- An Introduction to Theory and Practice_W. H. Gothmann
64/104
1 // E xampl e 6−2//
2 // f i n d mi nt e r ms f o r A+BC//3 clc4 // c l e a r s t he c o n s o l e //5 clear
6 // c l e a r s a l l e x i s t i n g v a r i a b l e s //7 / / c o n v e r s i o n t o m in te rm s / /8 disp ( ’ G iv en e x p r e s s i o n − A+BC ’ )9 disp ( ’ on s o l v i n g ’ )
10 disp ( ’ A(B+B ’ ’ ) (C+C ’ ’ )+BC(A+A ’ ’ ) ’ )11 disp ( ’ (AB+AB ’ ’ ) (C+C ’ ’ )+BCA+BCA ’ ’ ’ )12 disp ( ’ m u l t i p l y i n g ’ )
13 disp ( ’ C ’ ’AB+AB ’ ’C+AB ’ ’C+AB ’ ’C ’ ’+BCA+BCA ’ ’ ’ )14 // f i n a l r e s u l t i s d i sp l ay e d //
Scilab code Exa 6.3 Find minterms for AB plus ACD
1 // E xampl e 6−3//2 // f i n d minterms f o r AB+ACD//3 clc
4 / / c l e a r s t h e window / /5 clear
6 // c l e a r s a l l e x i s t i n g v a r i a b l e s //7 / / c o n v e r s i o n t o m in te rm s / /8 disp ( ’ Given e x p r es s i o n − AB+ACD ’ )9 disp ( ’ on s o l v i n g ’ )
10 disp ( ’ AB(C+C ’ ’ ) (D+D ’ ’ )+ACD(B+B ’ ’ ) ’ )11 disp ( ’ m u l t i p l y i n g ’ )12 disp ( ’ ABCD+ABC ’ ’D ’ ’+ABC’ ’D+ABCD ’ ’+AB ’ ’CD+ABCD ’ )13 disp ( ’ we know A+A=A ’ )
14 disp ( ’ ABC ’ ’D ’ ’+ABC ’ ’D+ABCD ’ ’+AB ’ ’CD+ABCD ’ )15