Digital Electronics - An Introduction To Theory and Practice - W. H. Gothmann

March 16, 2023 | Author: Anonymous | Category: N/A
Share Embed Donate


Short Description

Download Digital Electronics - An Introduction To Theory and Practice - W. H. Gothmann...

Description

 

Scilab Textbook Companion for Digital Electronics: An Introduction To Theory And Practice by W. H. Gothmann1 Created by Aritra Ray B.Tech Electronics Engineering NIT-DURGAPUR College Teacher Prof. Sabyasachi Sengupta,iit-kharagpur Cross-Checked by Lavitha M. Pereira November 11, 2013

1

Funded by a grant from the National Mission on Education through ICT, http://spoken-tutorial.org/NMEICT-Intro. This Textbook Companion and Scilab codes written in it can be downloaded from the ”Textbook Companion Project” section at the website http://scilab.in

 

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

 

Scilab numbering policy used in this document and the relation to the above 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 particular

Example of the above book) For example, Exa 3.51 means solved example 3.51 of this book. Sec 2.3 means a scilab code whose theory is explained in Section 2.3 of the book.

2

 

Contents Listt of Scilab Codes Lis 2

Num Nu mber Sy Syst stem emss

3

Bina Bi nary ry Co Code dess

4

Boole Boo lean an Al Alge gebra bra

6

Combin Com binati ationa onall Logi Logic c

 

4  

7

 

42  

58  

11 Analog Digital Conversion

62  

3

97

 

List of Scilab Codes Exaa 2. 2.11 Ex Ex Exaa 2.2 2.2 Exaa 2. 2.33 Ex Exa 2. Exa 2.44 Ex Exaa 2. 2.55 Exa 2.66 Exa 2. Ex Exaa 2. 2.77 Exa 2. Exa 2.88 Exa 2.99 Exa 2. Exa 2.10 Exa 2.10 Ex Exaa 2.11 2.11 2.122 Exa 2.1 Ex Exaa 2.13 2.13 .14 Exa 22.14 Exa 2.15 Exa 2.1 2.166 2.177 Exa 2.1 Exa 2.1 2.188 2.199 Exa 2.1 Exa 2.2 2.200 Exa 2.2 2.211 Ex Exaa 2.2 2.222 Exaa 2.2 2.233 Ex Exa 2.2 2.244 Exaa 2.2 2.255 Ex Exa 2.2 Exa 2.266 Exa 2.2 2.277 Exa 2.28 2.28

Conver Con ertt 101 10111 11 to de deci cima mall   . . . . . . . . . Con Co nver ertt 101 10111 1101 01001 001 to de deci cimal mal   . . . . . . Con Co nver ertt 110 11011 1111 to de deci cima mall   . . . . . . . . Con Co nver ertt 43 to bi bina nary ry   . . . . . . . . . . . . Con Co nver ertt 200 to bi bina nary ry   . . . . . . . . . . . Con Co nver ertt 43 to bi bina nary ry   . . . . . . . . . . . . Con Co nver ertt 200 to bi bina nary ry   . . . . . . . . . . . Add Ad d 10 1011 11 an and d 11 1100   . . . . . . . . . . . . . Add Ad d 11 1111 1100 an and d 11   . . . . . . . . . . . . . Subt Su btra ract ct on onee fro from m 100 100   . . . . . . . . . . . Mult Mu ltip iply ly 101 10111 by by 101 101   . . . . . . . . . . . Multip Mul tiply ly 110 11010 10 by 110 11011 11   . . . . . . . . . . Divi Di vide de 110 11011 1100 by by 101 101   . . . . . . . . . . . Conve Con vert rt 10101 10101110 11010 10 to hexa hexadec decima imall   . . . Conver Con vertt 1101111 11011110101110 0101110 to hexad hexadecima ecimall   . Conve Con vert rt 4A8 4A8C C to to bina binary ry   . . . . . . . . . . Conve Con vert rt FAC FACE E to bina binary ry   . . . . . . . . . . Conve Con vert rt 2C9 to dec decima imall   . . . . . . . . . . Conve Con vert rt EB4 EB4A A to to deci decimal mal   . . . . . . . . . Conve Con vert rt 423 to to hexad hexadeci ecimal mal   . . . . . . . . Conve Con vert rt 7290 729055 to hex hexade adecim cimal al   . . . . . . Add Ad d 1A8 1A8 an and d 67B 67B   . . . . . . . . . . . . . Add Ad d ACE ACEF1 F1 an and d 16B 16B7D 7D   . . . . . . . . . . Subtra Sub tract ct 3A8 fro from m 1273 1273   . . . . . . . . . . Mult Mu ltip iply ly 1A 1A33 by by 89 89   . . . . . . . . . . . . Divi Di vide de 1E 1EC8 C877 by by A5 A5   . . . . . . . . . . . . Conve Con vert rt 1111 1111101 1011110 1110101 101 to to octal octal   . . . . . Conve Con vert rt 10111 10111101 1010001 0001100 1000111 0111 to to octal octal   . .

4

. . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . .

7 7 8 8 8 8 9 9 10 10 11 11 12 12 13 13 14 14 14 14 15 15 16 16 17 17 18 18

 

Exa 2.29 2.29 .30 Exa 22.30 Exa 2.3 2.311 2.322 Exa 2.3 Exa 22.33 .33 2.34 Exa 2.34 .35 Exa 22.35 2.36 Exa 2.36 2.37 Exa 2.37 Ex Exaa 2.38 2.38 Exa 2.399 Exa 2.3 Exa 2.400 Exa 2.4 Exa 2.4 2.411 2.42 Exa 2.42 Exa 22.43 .43 Exa 2.44 2.44 2.45 Exa 2.45 Exa 2.46 2.46 Exa 2.4 2.477 2.488 Exa 2.4 Exa 2.4 2.499 Ex Exaa 2. 2.50 50 Exa 2.5 2.511 Exaa 3. 3.11 Ex Ex Exaa 3. 3.22 Exa 3. Exa 3.33 Exaa 3. 3.44 Ex

Convert Conve rt octal octal num number ber 3674 3674 to bina binary ry   . . . . . . . . . Minus Min us 5 in two twoss comple complemen mentt form form   . . . . . . . . . . 12 bit tw twos os com comple plemen mentt   . . . . . . . . . . . . . . . . 16 bit tw twos os com comple plemen mentt   . . . . . . . . . . . . . . . . Twos Tw os comple complemen mentt of minu minus4 s4 second second method method   . . . . . Twos Tw os comple complemen mentt of minus1 minus177 second second method method   . . . . Twos Tw os comple complemen mentt of minu minus4 s4 third third method method   . . . . . . Twos Tw os comple complemen mentt of minu minus17 s17 third third meth method od   . . . . . Negative Negat ive decim decimal al number number repre represen sented ted by by 10011011 10011011   . Add Ad d minu minuss 17 to min minus us 30 30   . . . . . . . . . . . . . . . Add Ad d min minus us 20 to 26   . . . . . . . . . . . . . . . . . . Add Ad d min minus us 29 to 14   . . . . . . . . . . . . . . . . . . Oness compl One compleme ement nt of min minus us 13 13   . . . . . . . . . . . . . Oness complem One complemen entt of minu minuss 13 second second method method   . . . . Oness comple One complemen mentt additi addition on using using 4 bits bits   . . . . . . . . Oness comple One complemen mentt additi addition on using using 4 bits bits   . . . . . . . . Oness comple One complemen mentt additi addition on using using 8 bits bits   . . . . . . . . Conve Con vert rt 1101 1101 point point 00010 0001011 to decim decimal al   . . . . . . . . Conve Con vert rt poin pointt 375 375 to bin binary ary   . . . . . . . . . . . . . . Conve Con vert rt poin pointt 54545 54545 to bina binary ry   . . . . . . . . . . . . Conve Con vert rt 38 point point 21 to to binar binary y . . . . . . . . . . . . . Bina Bi nary ry ad addi diti tion on  . . . . . . . . . . . . . . . . . . . . . Binary Bin ary mu mutip tiplic licati ation on   . . . . . . . . . . . . . . . . . . Add Ad d 64 6477 to 49 4922 in BC BCD D . . . . . . . . . . . . . . . . Add Ad d 43 4318 18 an and d 76 7678 78 in BC BCD D . . . . . . . . . . . . . . Add Ad d 3 an and d 2 in XS3   . . . . . . . . . . . . . . . . . . Add Ad d 6 an and d 8 in XS3   . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . .

19 19 20 20 21 22 22 23 24 25 26 26 27 28 28 29 29 30 32 33 34 36 38 42 44 46 47

Exaa 3.5 3.5 Ex Exa 3.6 Ex Exaa 3.7 3.7 Exa 3.8 Ex Exaa 3. 3.99 Ex Exaa 3.1 3.100 Exa 3.1 3.111 Exaa 4. 4.11 Ex Ex Exaa 4. 4.22 Exa 4. Exa 4.33 Exa 4.44 Exa 4.

Conver Con ertt bin binar ary y 1011 1011 to Gr Gray ay cod codee   . . Conve Con vert rt bin binary ary 1001 1001011 011 to Gra Gray y code   Con Co nver ertt Gra Gray y code code 10 1011 11 to bi bina nary ry   . . Conve Con vert rt Gra Gray y code 100 100101 10111 to bin binary ary   Hamm Ha mmin ingg co code de fo forr 10 1011 11   . . . . . . . . Hamm Ha mmin ingg code code fo forr 0101 0101   . . . . . . . . Correc Cor rectio tion n of Hamm Hamming ing code 1111 1111101 101   Demo De morg rgan aniz izee a fu func ncti tion on   . . . . . . . . Redu Re duce ce an ex expr pres essi sion on   . . . . . . . . . Redu Re duce ce an ex expr pres essi sion on   . . . . . . . . . Redu Re duce ce a bool boolea ean n ex expr pres essi sion on   . . . . .

. . . . . . . . . . .

48 49 50 51 52 54 55 58 58 59 60

5

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

 

Exaa 4. 4.55 Redu Re duce ce a Boo Boole lean an ex expr pres essi sion on   . . . . . . . Ex Exa 6.1 Con Co nver ertt A plu pluss B to to min minte term rmss   . . . . . . Exa 6.1 Ex Exaa 6. 6.22 Find Fi nd mi min nte term rmss fo forr A pl plus us BC   . . . . . . . Exaa 6.3 6.3 Find Fi nd mi min nte term rmss fo forr AB pl plus us AC ACD D . . . . . Ex Ex Exaa 6. 6.44 Mint Mi nter erm m de desi sign gnat atio ion n  . . . . . . . . . . . . Exa 6. Exa 6.55 Mint Mi nter erm m de desi sign gnat atio ion n  . . . . . . . . . . . . Exa 6.66 2 var ariab iable le ma mapp ppin ingg   . . . . . . . . . . . . . Exa 6. Exa 6. Exa 6.77 3 var ariab iable le ma mapp ppin ingg   . . . . . . . . . . . . . Exa 6. Exa 6.88 Mapp Ma ppin ingg an ex expr pres essi sion on   . . . . . . . . . . Ex Exaa 6. 6.99 Mapp Ma ppin ingg an ex expr pres essi sion on   . . . . . . . . . . 6.100 4 vari ariabl ablee map mappin pingg   . . . . . . . . . . . . . Exa 6.1 Exa 6.1 6.111 Red Reduce uce an expr express ession ion by Kma Kmap p  . . . . . . Exa 6.1 6.122 Red Reduce uce an expr express ession ion usi using ng Kmap Kmap   . . . . Exa 6.1 6.133 Red Reduce uce exp expres ressio sion n by by kmap kmap   . . . . . . . . Exa 6.14.a Inputs Required   . . . . . . . . . . . . . . Exa 6.14.b Inputs required   . . . . . . . . . . . . . . . Exa 6.14.c Inputs required   . . . . . . . . . . . . . . . Exa 6.14.d Inputs required   . . . . . . . . . . . . . . . Exa 6.1 6.155 Min Minimi imise se expr express ession ion usin usingg mappin mappingg   . . . . Exa 6.1 6.166 Red Reduce uce usi using ng map mappin pingg   . . . . . . . . . . . Ex Exaa 6.1 6.177 Km Kmap ap PO POS S SO SOP P . . . . . . . . . . . . . . Exa 6.1 6.188 Min Minimi imise se exp expres ressio sion n . . . . . . . . . . . . Ex Exaa 6.1 6.199 Re Redu duce ce km kmap ap by PO POS S . . . . . . . . . . . Exa 6.2 6.200 Fin Find d minim minimum um of of expr express ession ion   . . . . . . . 6.24 Mul Exa 6.24 Multip tiple le output output equa equatio tion n using using mappin mappingg   . 6.25 Mul Exa 6.25 Multip tiple le output output equa equatio tion n using using mappin mappingg   . Exa 6.2 6.266 Red Reduce uce by vari ariabl ablee mappi mapping ng   . . . . . . . Exa 6.2 6.277 6.288 Exa 6.2 Ex Exaa 6.2 6.299 6.300 Exa 6.3 Exa 11 11.1 .1 Exa 11. 11.22 Exa 11. 11.33 11.44 Exa 11. Exa 11. 11.55 Exa 11. 11.66 Exa 11.7 11.7

. . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . .

61 62 62 63 64 64 65 66 67 68 69 70 72 74 75 75 76 76 77 79 83 86 87 88 89 91 93

Reduce by vari Reduce ariabl ablee mappi mapping ng   . . . . . . . . . . . . . Solve Sol ve usi using ng 3 vari variabl ablee mappin mappingg   . . . . . . . . . . . . Redu Re duce ce by ma mapp ppin ingg   . . . . . . . . . . . . . . . . . . Reduci Red ucing ng expr express ession ion by Kma Kmap p . . . . . . . . . . . . Percen Per centage tage resol resolution ution of of 5bit DA conv converte erterr . . . . . . 6 bit ana analog log to to digita digitall conv convert erter er   . . . . . . . . . . . . Comput Com putee the the gai gain n of an Opa Opamp mp   . . . . . . . . . . . . Comput Com putee the the out output put vo volta ltage ge   . . . . . . . . . . . . . Comput Com putee out output put vo volta ltage ge   . . . . . . . . . . . . . . . . Resolution Resol ution and and Percen Percentt resolution resolution 12bit 12bit DAcon DAconve verter rter Resolution Resol ution and and Percen Percentt resolution resolution 10bit 10bit ADcon ADconver verter ter

. . . . . . . . .

93 94 94 95 97 97 98 99 100   101   102

6

. . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . .

 

Chapter 2 Number Systems

Scilab code Exa 2.1   Convert 10111 to decimal

1 2 3 4 5 6

  // E xamp x ample le 2 − 1/ 1///   / / B i n ar a r y t o D ec e c im i m a l C o n v er er s i o n / /   a = b i n 2 d e c (  ’ 1011 1 ’ )   / / D eecc im i m al a l e q u i v a l e n t o f t h e b i n a r y n um um b e r // //   disp ( a )

n s we   / / a ns w e r i n d e c im i m a l f or o r m // //

Scilab code Exa 2.2   Convert 1011101001 to decimal

1 2 3 4 5 6

  // E xamp x ample le 2 − 2/ 2///   / / B i n ar a r y t o D ec e c im i m a l C o n v er er s i o n / /   a = b i n 2 d e c (  ’ 1011 10100 1 ’ )   / / D ec i m al a l e q u i v a l e n t o f t h e b i n a r y n um um b e r // // e c im   disp ( a )

n s we   / / a ns w e r i n d e c im i m a l f or o r m // //

7

 

Scilab code Exa 2.3   Convert 110111 to decimal

1 2 3 4 5 6

  // E xamp x ample le 2 − 3/ 3///   / / B i n ar a r y t o D ec e c im i m a l C o n v er er s i o n / /   a = b i n 2 d e c (  ’ 1101 11 ’ ) i m al a l e q u i v a l e n t o f t h e b i n a r y n um um b e r // //   / / D eecc im   disp ( a )

n s we   / / a ns w e r i n d e c im i m a l f or o r m // //

Scilab code Exa 2.4  Convert 43 to binary

1   // E xamp x ample le 2 − 4/ 4/// 2 3 4 5 6

e c im i m al a l t o b i n a ry ry c o n v e r s i o n / /   / / D ec

  a=dec2bin(43)

/ / B i n ar   // a r y e q u i v a l e n t o f t h e d e c im i m a l n u m b e r // //   disp ( a )

n s we   / / a ns w e r i n b i n a r y f or or m // //

Scilab code Exa 2.5  Convert 200 to binary

1 2 3 4 5 6

  // E xamp x ample le 2 − 5/ 5///   / / D ec e c im i m al a l t o b i n a ry ry c o n v e r s i o n / /   a=dec2bin(200)

/ / B i n ar   // a r y e q u i v a l e n t o f t h e d e c im i m a l n u m b e r // //   disp ( a )

n s we   / / a ns w e r i n b i n a r y f or or m // //

Scilab code Exa 2.6  Convert 43 to binary

x ample le 2 − 6/ 6/// 1   // E xamp 8

 

2 3 4 5 6

  / / D ec e c im i m al a l t o b i n a ry ry c o n v e r s i o n / /   a=dec2bin(43)

/ / B i n ar   // a r y e q u i v a l e n t o f t h e d e c im i m a l n u m b e r // //   disp ( a )

  / / a ns w e r i n b i n a r y f or or m // // n s we

Scilab code Exa 2.7  Convert 200 to binary

1 2 3 4

  // E xamp x ample le 2 − 7/ 7///   / / D ec e c im i m al a l t o b i n a ry ry c o n v e r s i o n / /   a=dec2bin(200)

/ / B i n ar   // a r y e q u i v a l e n t o f t h e d e c im i m a l n u m b e r // //

56    / disp a )e r i n b i n a r y f or n(s we / a ns w or m // //

Scilab code Exa 2.8  Add 1011 and 110

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

  // E xamp x ample le 2 − 8/ 8///   / / B i n ar ar y a d d i t i o n / /   clc

  / / c l e a r s t he he c o n s o l e //   clear

  // 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 //

  x = b i n 2 d e c (  ’ 101 1 ’ )   y = b i n 2 d e c (  ’ 110 ’ )

  / / b i n a r y t o d e ci ci m a l c o n v e r s i o n / /   z=x+y

  // ad dit io n //   a=dec2bin(z)

  / / d e ci ci m a l t o b i n a r y c o n v e r s i o n / /   disp (   ’ a d d dii t iioo n o f t h e 2 b in i n aarr y n u m b e r s i s : ’ )   disp ( a )

n s we   / / a ns w e r i n b i n a r y f or or m // // 9

 

Scilab code Exa 2.9  Add 11110 and 11

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

  // E xamp x ample le 2 − 9/ 9///   / / B i n ar ar y a d d i t i o n / /   clc

  / / c l e a r s t he he c o n s o l e //   clear

  // 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 //   x = b i n 2 d e c (  ’ 1111 0 ’ )   y = b i n 2 d e c (  ’ 1 1 ’ )   / / b i n a r y t o d e ci ci m a l c o n v e r s i o n / /   z=x+y

  // ad dit io n //

  a=dec2bin(z)

  / / d e ci ci m a l t o b i n a r y c o n v e r s i o n / /   disp (   ’ a d d dii t iioo n o f t h e 2 b in i n aarr y n u m b e r s i s : ’ )   disp ( a )

n s we   / / a ns w e r i n b i n a r y f or or m // //

Scilab code Exa 2.10  Subtract one from 100

1 2 3 4 5 6 7 8 9 10

  // E xamp x ample le 2 − 10//   / / B i n ar ar y S u b t r a c t i o n / /   clc

  / / c l e a r s t he he c o n s o l e //   clear

  // 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 //   x = b i n 2 d e c (  ’ 100 ’ )   y = b i n 2 d e c (  ’ 1 ’ )   / / b i n a r y t o d e ci ci m a l c o n v e r s i o n / /   z=x-y

10

 

11 12 13 14 15 16

  // su bt ra ct io n //   a=dec2bin(z)

  / / d e ci ci m a l t o b i n a r y c o n v e r s i o n / /   disp (   ’ s u b t r a c t i o n o f t w o b i n naa r y n u m b e r s i s : ’ )   disp ( a )

n s we   / / a ns w e r i n b i n a r y f or or m // //

Scilab code Exa 2.11  Multiply 1011 by 101

x ample le 2 − 11// 1   // E xamp 2   / / B i na n a ry ry m u l t i p l i c a t i o n // 3   clc 4 5 6 7 8 9 10 11 12 13 14 15 16

/ / c l e a r s t he he c o n s o l e //    clear   // 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 //   x = b i n 2 d e c (  ’ 101 1 ’ )   y = b i n 2 d e c (  ’ 101 ’ )   / / b i n a r y t o d e ci ci m a l c o n v e r s i o n / /   z=x*y;

  // mul tip lic ati on //   a=dec2bin(z)

  / / d e ci ci m a l t o b i n a r y c o n v e r s i o n / /   disp (   ’ m u l t i p l i c a t i o n o f t w o b in i n aarr y n u m b e r s i s :   disp ( a )

n s we   / / a ns w e r i n b i n a r y f or or m // //

Scilab code Exa 2.12  Multiply 11010 by 11011

1 2 3 4 5

  // E xamp x ample le 2 − 12//   / / B i na n a ry ry m u l t i p l i c a t i o n //   clc

  / / c l e a r s t he he c o n s o l e //   clear

11

’)

 

6 7 8 9 10 11 12 13 14 15 16

  // 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 //   x = b i n 2 d e c (  ’ 1101 0 ’ )   y = b i n 2 d e c (  ’ 1101 1 ’ )   / / b i n a r y t o d e ci ci m a l c o n v e r s i o n / /   z=x*y;

  // mul tip lic ati on //   a=dec2bin(z)

  / / d e ci ci m a l t o b i n a r y c o n v e r s i o n / /   disp (   ’ m u l t i p l i c a t i o n o f t w o b in i n aarr y n u m b e r s i s :   disp ( a )

n s we   / / a ns w e r i n b i n a r y f or or m // //

Scilab code Exa 2.13  Divide 110110 by 101

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

  // E xamp x ample le 2 − 13//   / / B in i n ar ar y D i v i s i o n //   x = b i n 2 d e c (  ’ 1101 10 ’ )   y = b i n 2 d e c (  ’ 101 ’ )   r = m  mo odulo (x,y)

  / / f i n d i n g t h e r e ma m a i n de de r / /   z=x/y   q = floor ( z )

  / / f i n d i n g t h e q u o t i e n t //   quo=dec2bin(q)   rem=dec2bin(r)

  / / d e ci ci m a l t o b i n a r y c o n v e r s i o n s / /   disp (   ’ t h e q u o t iiee n t i s : ’ )   disp ( q u o )   disp (   ’ t h e r e m maa iin nd dee r   disp ( r e m )

is :

’)

  / / a n sw s w e rs r s i n b i n a ry r y f or o r m // //

Scilab code Exa 2.14   Convert 1010111010 to hexadecimal

12

’)

 

1 2 3 4 5 6 7 8

  // E xamp x ample le 2 − 14//   / / b i n a r y t o h e xa x a d e ci ci m a l c o n v e r s i o n //   x = b i n 2 d e c (  ’ 1010 11101 0 ’ )   / / d e ci c i m a l e q u i v a l e n t o f t h e b i n a ry r y n u m b e r // //   a = dec2hex ( x )

He x e q u i v a l e n t o f t h e d e ci   / / He c i m a l n u m b e r // //   disp ( a )

  / / a n sw e r i n h e xa x a d e ci c i m a l f or or m // // s w er

Scilab code Exa 2.15   Convert 11011110101110 to hexadecimal

1   // E xamp x ample le 2 − 15// 2 3 4 5 6 7 8

/ bbii nn 2a dr ye c (t  ’o 1 1h0e 1xa x1a1d1e0ci c1i m    / x= 0 1a1l 1 0c o’ )n v e r s i o n / /   / / d e ci c i m a l e q u i v a l e n t o f t h e b i n a ry r y n u m b e r // //   a = dec2hex ( x )

  / / He c i m a l n u m b e r // // He x e q u i v a l e n t o f t h e d e ci   disp ( a )

s w er   / / a n sw e r i n h e xa x a d e ci c i m a l f or or m // //

Scilab code Exa 2.16  Convert 4A8C to binary

1 2 3 4 5 6 7 8

  // E xamp x ample le 2 − 16//   / / H ex e x ad a d ec e c im i m al al t o b i n a r y c o n v e r s i o n //   x = hex2dec (  ’ 4A8 4A8C C ’) c i m a l c o n v e r s i o n o f t h e h e xa x a d ec e c i ma m a l n u m b e r // //   / / d e ci   a=dec2bin(x)

/ / B i n ar   // a r y e q u i v a l e n t o f t h e d e c im i m a l n u m b e r // //   disp ( a )

  / / a ns w e r i n b i n a r y f or or m // // n s we

13

 

Scilab code Exa 2.17  Convert FACE to binary

1 2 3 4 5 6 7 8

  // E xamp x ample le 2 − 17// e x ad a d ec e c im i m al al t o b i n a r y c o n v e r s i o n //   / / H ex   x = hex2dec (  ’FA  ’FAC CE ’ )   / / d e ci c i m a l c o n v e r s i o n o f t h e h e xa x a d ec e c i ma m a l n u m b e r // //   a=dec2bin(x)

  // a r y e q u i v a l e n t o f t h e d e c im i m a l n u m b e r // // / / B i n ar   disp ( a )

  / / a ns w e r i n b i n a r y f or or m // // n s we

Scilab code Exa 2.18  Convert 2C9 to decimal

1 2 3 4 5 6

  // E xamp x ample le 2 − 18//   / / H ex e x ad a d ec e c im i m al a l t o d e ci ci m a l c o n v e r s i o n / /   a = hex2dec (  ’ 2C9 ’ )   / / d e ci c i m a l e q u i v a l e n t o f t h e h e xa x a d ec e c i ma m a l n u m b e r // //   disp ( a )

n s we   / / a ns w e r i n d e c im i m a l f or o r m // //

Scilab code Exa 2.19  Convert EB4A to decimal

1 2 3 4 5 6

  // E xamp x ample le 2 − 19//   / / H ex e x ad a d ec e c im i m al a l t o d e ci ci m a l c o n v e r s i o n / /   a = hex2dec (  ’EB4A ’ )   / / d e ci c i m a l e q u i v a l e n t o f t h e h e xa x a d ec e c i ma m a l n u m b e r // //   disp ( a )

n s we   / / a ns w e r i n d e c im i m a l f or o r m // //

Scilab code Exa 2.20  Convert 423 to hexadecimal

14

 

1 2 3 4 5 6

  // E xamp x ample le 2 − 20//   / / D ec e c im i m al a l t o h e xa x a d ec e c i ma ma l c o n v e r s i o n / /   a = dec2hex (423)

  / / h e xa x a d e ci c i m a l e q u i v a l e n t o f t h e d e c im i m a l n u m b e r // //   disp ( a )

s w er   / / a n sw e r i n h e xa x a d e ci c i m a l f or or m // //

Scilab code Exa 2.21  Convert 72905 to hexadecimal

1   // E xamp x ample le 2 − 21// 2   / / D ec e c im i m al a l t o h e xa x a d ec e c i ma ma l c o n v e r s i o n / / 3   a = dec2hex (72905) 4   / / h e xa c i m a l e q u i v a l e n t o f t h e d e c im i m a l n u m b e r // // 5   disp (xaa)d e ci e r i n h e xa x a d e ci c i m a l f or or m // // 6   / / a n sw s w er

Scilab code Exa 2.22  Add 1A8 and 67B

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

  // E xamp x ample le 2 − 22//   / / H e xa x a d ec e c i ma ma l a d d i t i o n / /   clc

  / / c l e a r s t he he c o n s o l e //   clear

  // 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 //   x = hex2dec (  ’ 1A8 ’ )   y = hex2dec (  ’ 6 7 7B B’ )   / / D ec i m al a l c o n v e r s i o n o f t h e h e xa x a d ec e c i ma m a l n u mb mb e r s / / e c im   z=x+y

  // ad dit io n //   a = dec2hex ( z )

  / / d e ci c i m a l t o h e xa x a d ec e c i ma ma l c o n v e r s i o n / /   disp (   ’ a d d i ttii o n o f t h hee 2 h eex x aad d eecc im i m aall n u m b e r s i s   disp ( a )

15

’)

 

e r i n h e xa x a d e ci c i m a l f or or m // // 16   / / a n sw s w er

Scilab code Exa 2.23  Add ACEF1 and 16B7D

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

  // E xamp x ample le 2 − 23//   / / H e xa x a d ec e c i ma ma l a d d i t i o n / /   clc

  / / c l e a r s t he he c o n s o l e //   clear

  // 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 //   x = hex2dec (  ’ ACEF EF11 ’ )   y = hex2dec (  ’ 16B7 16B7D D’) eyc im / =/ xD+ec i m al al c o n v e r s i o n    z

o f t h e h e xa x a d ec e c i ma m a l n u mb mb e r s / /

  // ad dit io n //   a = dec2hex ( z )

  / / d e ci c i m a l t o h e xa x a d ec e c i ma ma l c o n v e r s i o n / /   disp (   ’ a d d i ttii o n o f t h hee 2 h eex x aad d eecc im i m aall n u m b e r s i s

’)

  disp ( a )

  / / a n sw e r i n h e xa x a d e ci c i m a l f or or m // // s w er

Scilab code Exa 2.24  Subtract 3A8 from 1273

1 2 3 4 5 6 7 8 9 10

  // E xamp x ample le 2 − 24//   / / H e xa x a d ec e c i ma ma l S u b t r a c t i o n / /   clc

he c o n s o l e //   / / c l e a r s t he   clear

  // 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 //   x = hex2dec (  ’ 127 3 ’ )   y = hex2dec (  ’ 3A8 ’ )   / / D ec i m al a l c o n v e r s i o n o f t h e h e xa x a d ec e c i ma m a l n u mb mb e r s / / e c im   z=x-y

16

 

11 12 13 14 15 16

  // ad dit io n //   a = dec2hex ( z )

  / / d e ci c i m a l t o h e xa x a d ec e c i ma ma l c o n v e r s i o n / /   disp (   ’ a d d i ttii o n o f t h hee 2 h eex x aad d eecc im i m aall n u m b e r s i s

’)

  disp ( a )

s w er   / / a n sw e r i n h e xa x a d e ci c i m a l f or or m // //

Scilab code Exa 2.25  Multiply 1A3 by 89

x ample le 6 − 25// 1   // E xamp 2   / / S o l v e m u l t i p l e o ut u t p ut u t e q u a t io io n u s i n g mapping / / 3   clc 4 5 6 7 8 9 10 11 12 13 14

   clear / / c l e a r s t h e window / /   // 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 //   x = hex2dec (  ’ 1A3 ’ )   y = hex2dec (  ’ 8 9 ’ ) e c im   / / D ec i m al a l c o n v e r s i o n o f t h e h e xa x a d ec e c i ma m a l n u mb mb e r s / /   z=x*y

  // mul tip lic ati on //   a = dec2hex ( z )

c i m a l t o h e xa x a d ec e c i ma ma l c o n v e r s i o n / /   / / d e ci   disp (   ’ m u l t i p l i c a t i o n o f t h hee 2 h eex x aad d eecc im i m aall n u m b e r s is ’)

15    /disp a )er / a n (sw e r i n h e xa x a d e ci c i m a l f or or m // // 16 sw

Scilab code Exa 2.26  Divide 1EC87 by A5

1 2 3 4

  // E xamp x ample le 2 − 26//   / / H e xa x a d ec e c i ma ma l D i v i s i o n / /   x = hex2dec (  ’ 1EC8 1EC877 ’ )   y = hex2dec (  ’ A5 ’ ) 17

 

5 6 7 8 9 10 11 12 13 14 15 16 17

 mo odulo (x,y)   r = m

m a i n de de r / /   / / f i n d i n g t h e r e ma   z=x/y   q = floor ( z )

  / / f i n d i n g t h e q u o t i e n t //   q u o = dec2hex ( q )   r e m = dec2hex ( r )

  / / d e ci ci m a l t o b i n a r y c o n v e r s i o n s / /   disp (   ’ t h e q u o t iiee n t i s : ’ )   disp ( q u o )   disp (   ’ t h e r e m maa iin nd dee r   disp ( r e m )

is :

’)

  / / a n sw s w e rs r s i n b i n a ry r y f or o r m // //

Scilab code Exa 2.27   Convert 11111011110101 to octal

1 2 3 4 5 6 7 8

  // E xamp x ample le 2 − 27//   / / B i na n a ry r y t o o c t a l c o n v e r si si o n //   x = b i n 2 d e c (  ’ 1 1 1 1 1 0 1 1 1 1 0 1 0 1 ’ )   / / d e ci c i m a l e q u i v a l e n t o f t h e b i n a ry r y n u m b e r // //   a=dec2oct(x)

  / / o c t a l e q u i v a l e n t o f t he h e d ec e c i ma m a l n u m b e r // //   disp ( a )

a n sw   / / an s w er e r i n o c t a l f o rm rm / /

Scilab code Exa 2.28   Convert 1011110100011000111 to octal

1 2 3 4 5 6

  // E xamp x ample le 2 − 28//   / / B i na n a ry r y t o o c t a l c o n v e r si si o n //   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 ’ )   / / d e ci c i m a l e q u i v a l e n t o f t h e b i n a ry r y n u m b e r // //   a=dec2oct(x)

h e d ec e c i ma m a l n u m b e r // //   / / o c t a l e q u i v a l e n t o f t he 18

 

7   disp ( a ) s w er e r i n o c t a l f o rm rm / / 8   / / an a n sw

Scilab code Exa 2.29  Convert octal number 3674 to binary

1 2 3 4 5 6 7

  // E xamp x ample le 2 − 29// n a r y c o n v e r si si o n //   / / o c t a l t o b i na   x = o c t 2 d e c (  ’ 367 4 ’ )   / / d ec e c i ma m a l e q u i v a l e n t o f t he h e o c t a l n u m b e r // //   a=dec2bin(x)

  / / 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 i m a l n u m b e r // //   disp ( a )

8   / / a ns n s we w e r i n b i n a r y f or or m // //

Scilab code Exa 2.30  Minus 5 in twos complement form

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

  // E xamp x ample le 2 − 30//   / /   −5 i n t w o s c o m p l e m e n t f o rm rm / /   clc

  / / c l e a r s t he he c o n s o l e //   clear

  // 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 //

  x=2^8

//   / / S m a l l e s t n i n e b i t n u m b e r //   y =5   z=x-y

  // su bt ra ct io n //   a=dec2bin(z)

  / / 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 i m a l n u m b e r // //   disp (  ’   −5 i n t w o s c o m p l e m e n t f o r m i s ’ )   disp ( a )

n s we   / / a ns w e r i n b i n a r y f or or m // //

19

 

Scilab code Exa 2.31  12 bit twos complement

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

  // E xamp x ample le 2 − 31//   / /   − 4 , − 15 , − 1 7 i n t w o

s c o m p l e m e n t f o rm rm / /

  clc

  / / c l e a r s t he he c o n s o l e //   clear

  // 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 //

  x=2^12

  / / S m a l l e s t n i n e b i t n u m b e r // //   p =4         

q r= =1 15 7 u=x-p v=x-q  w=x-r

  // su bt ra ct io n //   a=dec2bin(u)   b=dec2bin(v)   c=dec2bin(w)

  / / 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 i m a l n u m b e r // //   disp (  ’   −4 i n t w o s c o m p l e m e n t f o r m i s ’ )          

disp ( a ) disp (  ’   −15 i n disp ( b ) disp (  ’   −17 i n disp ( c )

two

s complement form i s

’)

two

s complement form i s

’)

  / / a n sw s w e rs r s i n b i n a ry r y f or o r m // //

Scilab code Exa 2.32  16 bit twos complement

1   // E xamp x ample le 2 − 32//

20

 

2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

  / /   −16000 i n

two

s c o mp m p l em e m en en t f or or m // //

  clc

  / / c l e a r s t he he c o n s o l e //   clear

  // 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 //

  x=2^16

//   / / S m a l l e s t n i n e b i t n u m b e r //   y=16000   z=x-y

  // su bt ra ct io n //   a=dec2bin(z)

  / / 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 i m a l n u m b e r // //   disp (  ’   − 1 6 0 0 0 i n t w o s c o m p l e m e n t f o rm rm i s ’ )   disp ( a )

n s we   / / a ns w e r i n b i n a r y f or or m // //

Scilab code Exa 2.33   Twos complement of minus4 second method

1 2 3 4 5 6

  // E xamp x ample le 2 − 33//   / /   −4 i n t w o s c om om pl p l em em en e n t f or o r m b y s e co co n d method / /   clc

  / / c l e a r s t h e window / /   clear

  // 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=bitcmp(4,8) 8   / / c om o m pl p l em em en e n t o f t h e d e c im im a l number 4 ( 8 b i t

r e p r e s e n t a t i o n ) // // 9 10 11 12 13 14 15 16

  y =1   z=x+y

  / / 1 i s a dd d d e d t o t h e c o mp mp le l e m en e n t // //   a=dec2bin(z)

  / / 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 i m a l n u m b e r // //   disp (  ’   −4 i n t w o s c o m p l e m e n t f o r m i s : ’ )   disp ( a )

  / / r e s u l t i s d i s p la la y e d // 21

 

Scilab code Exa 2.34  Twos complement of minus17 second method

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

  // E xamp x ample le 2 − 34//   / /   −17 i n t w o s c o mp mp le l e me me n t f or or m b y s e co co n d method / /   clc

  / / c l e a r s t h e window / /   clear

  // 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 //

  x=bitcmp(17,8)

  / / c om p l em em en e n t o f t h e d e c im i m a l n u m b e r 1 7 (8 (8 b i t o m pl r e p r e s e n t a t i o n ) // //   y =1   z=x+y

  / / 1 i s a dd d d e d t o t h e c o mp mp le l e m en e n t // //   a=dec2bin(z)

  / / 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 i m a l n u m b e r // //   disp (  ’   −17 i n t w o s c o m p l e m e n t f o r m i s : ’ )   disp ( a )

  / / r e s u l t i s d i s p la la y e d //

Scilab code Exa 2.35  Twos complement of minus4 third method

1 2 3 4 5 6 7 8 9

  // E xamp x ample le 2 − 35//   / / −4 i n t w o ’ s c om o m pl p l em e m en en t b y t h i r d m e t h o d / /   clc

  / / c l e a r s t he he c o n s o l e //   clear

  // 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 //

  a=00000100   c =0   z =0

22

 

10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

  f o r   i =1 =1: 8 odulo (a,10)   x ( i ) = m = mo   a=a/10   a = floor ( a )   end   f o r   i =1 =1: 8   i f   c > 1   then   break   end   i f   x ( i ) = = 1   then   f o r   k = i +1 +1 : 8   x(k)=bitcmp(x(k),1)   c=c+1   end end     end   f o r   i =1 =1: 8   z=z+x(i)*10^(i-1)   end   disp (  ’ −4 i n t w woo s c o m p l e m e n t i s   disp ( z )

: ’)

  / / a ns n s we 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 2 3 4 5 6 7 8 9 10

  // E xamp x ample le 2 − 36//   / / −17 i n t w o ’ s c om o m pl p l em e m en e n t b y t h i r d m et et ho ho d / /   clc

  / / c l e a r s t he he c o n s o l e //   clear

  // 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 //

a=00010001 c =0 z =0 f o r   i =1 =1: 8

23

 

11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

   

         

odulo (a,10)   x ( i ) = m = mo   a=a/10   a = floor ( a ) end f o r   i =1 =1: 8   i f   c > 1   then   break   end   i f   x ( i ) = = 1   then   f o r   k = i +1 +1 : 8   x(k)=bitcmp(x(k),1)   c=c+1   end   end end f o r   i =1 =1: 8   z=z+x(i)*10^(i-1) end disp (  ’ −17 i n t w woo s c o m mp p le le m mee n t i s disp ( z )

: ’)

  / / a ns n s we 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 2 3 4 5 6 7 8 9

  // E xamp x ample le 2 37//   / / n e g at a t i v e −d ec e c im i m al a l r e p r e s e n t a t i o n o f 1 00 0 0 11 1 1 01 0 1 1/ 1/ /   clc

  / / c l e a r s t h e window / /   clear

  // 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 //   b = b i n 2 d e c (  ’ 10011 011 ’ )   x=bitcmp(b,8)

  / / c om p l em em en e n t o f t h e d e c im i m a l n u m b e r 1 7 (8 (8 b i t o m pl r e p r e s e n t a t i o n ) // //

10   y = 1

24

 

11 12 13 14 15 16

  z=x+y

  / / 1 i s a dd d d e d t o t h e c o mp mp le l e m en e n t // //   a=dec2bin(z)

  / / 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 i m a l n u m b e r // //

  z=z*(-1)   disp (   ’ t he h e n e g aatt i v e v aall u uee t h haa t 1 0000 11 1 1 01 01 1 r e p r e s e n t s i s : ’) 17   disp ( z ) la y e d // 18   / / r e s u l t i s d i s p la

Scilab code Exa 2.38  Add minus 17 to minus 30

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

  // E xamp x ample le 2 38//   //add   −17 t o−  − 30//   clc

  / / c l e a r s t he he c o n s o l e //   clear

  // c l e a r s a l l

exisiting

v a r i a b l e s //

  x=bitcmp(17,8)   y=bitcmp(30,8)

  / / c om p l em e m en e n t o f t h e d e c i m a l n um u m be be rs rs 1 7 a nd n d 3 0/ 0/ / o m pl   z =1   u=x+z   v=y+z

  / / 1 i s a dd d d e d t o t h e c om o m pl p l em e m en e n ts ts / /

  w=u+v   a=dec2bin(w)

  / / 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 i m a l n u m b e r // //   disp (   ’ b i n a r y f o orr m o f n u m b e r o b t a i n nee d b y a d d dii ng n g   −17 t o   −30 ’ )

18   disp ( a ) 19   / / r e s u l t i s d i s p la la y e d // 20   disp (   ’ t he h e ms b i s d is is ca rd ed , s o e i g h t b i t

r e p re s e n ta t i o n i s t h hee a n s w e r i n b in i n aarr y f o r m ’ ) 21   a = d e c 2 b i n ( w - ( 2 ^ 8 ) )

25

 

22   disp ( a ) 23   / / f i n a l r e s u l t

i s d i sp s p l ay a y e d //

Scilab code Exa 2.39  Add minus 20 to 26

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

  // E xamp x ample le 2 − 39//   //add   −20 t o 2 6/ 6/ /   clc

  / / c l e a r s t he he c o n s o l e //   clear

  // 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 //

  x=bitcmp(20,8)

om pl p l em e m en e n t o f 2 9 // //   / / f i n d s c om

  y =1   u=x+y

  / / 1 i s a dd d d e d t o t h e c o mp mp le l e m en e n t // //   v=26   w=u+v   a=dec2bin(w)

  / / 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 i m a l n u m b e r // //   disp (   ’ b i n naa r y f o r m o f t he h e n u m b e r o b ttaa i n nee d b y a d dd d in in g −2 0 t o 2 6 ’ )

17   disp ( a ) 18   / / r e s u l t

i s d i s p la la y e d //

19   disp heen tmastbi oins idsis i s ct ah irty f o r m r e(p  ’ r et she heer d eadn s,ws eor eiing hbt in i nbaar 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 s p l ay a y e d //

Scilab code Exa 2.40  Add minus 29 to 14

1   // E xamp x ample le 2 − 40//

26

’)

 

2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

  //add   −29 t o 1 4/ 4/ /   clc

  / / c l e a r s t he he c o n s o l e //   clear

  // 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 //

  x=bitcmp(29,8)

om pl p l em e m en e n t o f 2 9 // //   / / f i n d s c om   y =1   u=x+y

  / / 1 i s a dd d d e d t o t h e c o mp mp le l e m en e n t // //   v=14   w=u+v   a=dec2bin(w)

  / / 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 i m a l n u m b e r // //   disp (   ’ b i n naa r y f o r m o f t he h e n u m b e r o b ttaa i n nee d b y a d dd d in in g −2 9 t o 1 4 ’ )

17   disp ( a ) 18   / / r e s u l t

i s d i s p la la y e d //

Scilab code Exa 2.41  Ones complement of minus 13

1   // E xamp x ample le 2 − 41// 2   / / o ne n e ’ s c om o m pl p l em e m en e n t f or o r m o f      − 13// 3   clc 4 5 6 7 8 9 10 11 12 13

  / / c l e a r s t h e window / /   clear

  // 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 //

  x=bitcmp(13,8)

  / / c o mp l e me m e nt n t o f 1 3 // // mp le   a=dec2bin(x)

  / / 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 i m a l n u m b e r // //   disp (   ’ o n eess c o om mp pll eem m een n t f oorr m o f      −13 ’ )   disp ( a )

  / / r e s u l t i s d i s p la la y e d //

27

 

Scilab code Exa 2.42  Ones complement of minus 13 second method

1 2 3 4 5 6 7 8 9

  // E xamp x ample le 2 − 42//   / / o n e s c om o m pl p l em e m en e n t o f      −13 b y 2 n d m et et ho ho d / /   clc

  / / c l e a r s t h e window / /   clear

  // 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 //

  a=bitcmp(0,8)

  / / d e ci c i m a l e q u i v a l e n t o f 1 1 11 1 1 11 1 1 1 1/ 1/ /   b=13

10   c = a - b c i m al al e q u i v a l e n t o f   11   / / s u b t r a c t i n g 1 3 f r o m d e ci

11111111// 12 13 14 15

  z=dec2bin(c)   disp (   ’ o n e s c om om p pll em e m een n t o f      −13 i s : ’ )   disp ( z )

  / / r e s u l t i s d i s p la la y e d //

Scilab code Exa 2.43  Ones complement addition using 4 bits

1 2 3 4 5 6 7 8 9 10 11

  // E xamp x ample le 2 − 43//   //add   −3 t o   −2 i n o n e ’ s c om om pl p l em em en en t u s i n g 4 b i t s / /   clc

  / / c l e a r s t h e window / /   clear

  // 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 //   x=bitcmp(3,4)   y=bitcmp(2,4)

  / / c o mp mp le l e me me nt n t o f t h e d e c i m a l n u m b e r 2 // //   z=x+y+1

  / / c a r r y i s a dd dd ed ed / / 28

 

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 o orr m o f t h e n u m b e r o b t a i n eed d b y a d di di n g −3 t o   −2 ’ ) 15   disp ( a ) 16   / / r e s u l t i s d i s p la la y e d // 17   disp (   ’ m mss b i s d is i s ca c a rd r d eed d , 4 b i t r e p re s e n ta t i o n i s t h hee a ns ns w wee r i n b i n a r y f oorr m ’ ) 18   a = d e c 2 b i n ( z - ( 2 ^ 4 ) ) 19   disp ( a ) 20   / / F i na n a l r e s u l t i s d i s p la l a y e d //

Scilab code Exa 2.44  Ones complement addition using 4 bits

1 2 3 4 5 6 7 8 9 10

  // E xamp x ample le 2 − 44//   //add   −3 t o 2 i n o n e ’ s c o m p l e m e n t u s i ng ng 4 b i t s //   clc

  / / c l e a r s t h e window / /   clear

  // 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 //   x =2   y=bitcmp(3,4)

  / / c o mp l e me me nt n t o f t h e d e c i m a l n u m b e r 2 // // mp le   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 o orr m o f t h e n u m b e r o b t a i n eed d b y a d di di n g −3 t o 2 ’ ) 14   disp ( a ) 15   / / r e s u l t i s d i s p la la y e d //

Scilab code Exa 2.45  Ones complement addition using 8 bits

29

 

1 2 3 4 5 6 7 8 9 10 11 12 13 14

  // E xamp x ample le 2 − 45//   / / ad a d d 3 t o   −2 i n o n e ’ s c o mp mp le l e me me n t u s i n g 8 b i t s / /   clc

  / / c l e a r s t h e window / /   clear

  // 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 //   x =3   y=bitcmp(2,8)

mp le   / / c o mp l e me me nt n t o f t h e d e c i m a l n u m b e r 2 // //   z=x+y+1

  / / c a r r y i s a dd dd ed ed / /   a=dec2bin(z)

  //binary conver sion //   disp (   ’ b i n a r y f o orr m o f t h e n u m b e r o b t a i n eed d b y a d di di n g 3 t o   −2 ’ )

15   disp ( a ) 16   / / r e s u l t i s d i s p la la y e d // 17   disp (   ’ m mss b i s d is i s ca c a rd r d eed d , 8 b i t r e p re s e n ta t i o n a ns ns w wee r i n b i n a r y f oorr m ’ ) 18   a = d e c 2 b i n ( z - ( 2 ^ 8 ) ) 19   disp ( a ) n a l r e s u l t i s d i s p la l a y e d // 20   / / F i na

Scilab code Exa 2.46  Convert 1101 point 000101 to decimal

1 2 3 4 5 6 7 8 9 10

  // E xamp x ample le 2 − 46//   / / C o n v er er s i o n t o d e c i m a l / /   clc

  / / c l e a r s t he he c o n s o l e //   clear

  // 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 //

p =1 q =1 z =0 b =0

30

is th hee

 

11 12 13 14

  w =0   f =0

  // i n i t i a l i s i n g / /   / / b i n= n= i n p u t ( e n t e r t h e b i n a r y n u m b e r t o b e c o n v e rt rt e d t o i t s d e ci c i m al a l f o rm rm ) / / 15   / / a c c e p ti t i n g t he h e i n pu p u t f r o m t he he u s e r // 16   b i n = 1 1 0 1 . 0 0 0 1 0 1  mo odulo (bin,1) 17   d = m a t i n g t he h e d ec e c im i m al a l p ar a r t f r o m t he he i n t e g e r 18   / / s e p a r at

p ar ar t

// 19 20 21 22 23

  d=d*10^10   a = floor ( b i n )

24 25 26 27 28 29 30 31

             

32 33 34 35 36

       

37 38 39 40 41 42 43

             

  / / r em e m o vi vi n g t h e d e c i m a l p a r t / /   while ( a > 0 )

  / / l oo o o p t o e n t e r t h e b in i n ar a r y b i t s o f t he h e i n t e g e r p ar ar t i n t o a m a tr tr i x / /  mo odulo (a,10) r = m b(1,q)=r a=a/10 a = floor ( a ) q=q+1 end f o r   m = 1: 1: q - 1

  / / m u lt l t i pl p l 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 ar t w i t h i t s c o r r e sp s p o n di d i n g p o s i t i o n a l v al a l ue u e a n d a dd d d in i n g // // c=m-1 f=f+b(1,m)*(2^c) end while ( d > 0 )

  / / l oo o o p t o t ak a k e t he h e b i t s o f t he h e d ec e c im i m al a l p ar ar t i n to a mat r ix // // e = m  mo odulo (d,2) w(1,p)=e d=d/10 d = floor ( d ) p=p+1 end f o r   n = 1: 1: p - 1

31

 

44   / / m u l t i p l y i n g e a ch c h b i t w i th th

i t s c o r r e sp s p o n di di n g p o s i t i o n a l v al al u uee a n d a dd d d in i n g // //

45 46 47 48 49 50 51 52

       

z=z+w(1,n)*(0.5)^(11-n) end z=z*10000 z = round ( z )

o u nd n d in i n g o f f t o 4 d e ci c i m al al p l a c e s //   / / r ou

  z=z/10000   x=f+z   disp (   ’ t h hee d e ccii m aall e q u i v a l e n t o f t h hee b i n naa r y n u m b e r i s : ’) 53   disp ( x ) 54   / / r e s u l t i s d i s p la la y e d //

Scilab code Exa 2.47  Convert point 375 to binary

1 2 3 4 5 6 7 8

  // E xamp x ample le 2 − 47//   / / C o nv n v e rs r s i o n o f d e ci ci m a l t o b i n a r y / /   clc

  / / c l e a r s t he he c o n s o l e //   clear

  // 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 //

  q =0   b =0

9   s =0 10   / / i n i t i a l i s i n g / / 11   / / a= a= i n p u t ( e n t e r t h e d e c i m a l n u m b e r t o b e c o n v e r t e d

t o i t s b in i n ar ar y form ) 12   / / t a k i n g i n p u t f ro ro m t h e u s e r / / 13 14 15 16 17 18

  a=0.375   d = m  mo odulo (a,1)

h e d ec e c i ma m a l p a rt r t f r o m t he he i n t e g e r //   / / s e p a r a t i n g t he   a = floor ( a )

  / / r em e m o vi vi n g t h e d e c i m a l p a r t / /   while ( a > 0 )

32

 

19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38

  / / i n t e g e r p ar a r t c on o n v e rt r t e d t o e q u i v a l e n t b in i n ar a r y f o r m // //              

x = m  mo odulo (a,2) b=b+(10^q)*x a=a/2 a = floor ( a ) q=q+1 end f o r   i = 1: 1: 1 0

k i n g v a l ue u e s a f t e r t he h e d ec e c im i m al a l p ar a r t and   / / t a ki c o n ve v e r t i n g t o e q u i v a l e n t b in i n a ry r y f o r m // //                  

d=d*2 q = floor ( d ) s=s+q/(10^i) i f   d > = 1   then   d=d-1 end end k=b+s disp (   ’ t h hee d e ccii m aall n u m b e r i n b i n naa r y f o rm rm i s disp ( k )

: ’)

  / / r e s u l t i s d i s p la la y e d //

Scilab code Exa 2.48  Convert point 54545 to binary

1 2 3 4 5 6 7 8 9 10 11

  // E xamp x ample le 2 − 48//   / / C o nv n v e rs r s i o n o f d e ci ci m a l t o b i n a r y / /   clc

  / / c l e a r s t he he c o n s o l e //   clear

  // 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 //

  q =0   b =0   s =0

  // i n i t i a l i s i n g / /   / / a= a= i n p u t ( e n t e r t h e d e c i m a l n u m b e r t o b e c o n v e r t e d 33

 

t o i t s b in i n ar ar y form ) 12   / / t a k i n g i n p u t f ro ro m t h e u s e r / / 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38

  a=0.54545  mo odulo (a,1)   d = m

h e d ec e c i ma m a l p a rt r t f r o m t he he i n t e g e r //   / / s e p a r a t i n g t he   a = floor ( a )

  / / r em e m o vi vi n g t h e d e c i m a l p a r t / /   while ( a > 0 )

  / / i n t e g e r p ar a r t c on o n v e rt r t e d t o e q u i v a l e n t b in i n ar a r y f o r m // //              

x = m  mo odulo (a,2) b=b+(10^q)*x a=a/2 a = floor ( a ) q=q+1 end f o r   i = 1: 1: 1 0

  / / t a ki k i n g v a l ue u e s a f t e r t he h e d ec e c im i m al a l p ar a r t and c o n ve v e r t i n g t o e q u i v a l e n t b in i n a ry r y f o r m // //                  

d=d*2 q = floor ( d ) s=s+q/(10^i) i f   d > = 1   then   d=d-1 end end k=b+s disp (   ’ t h hee d e ccii m aall n u m b e r i n b i n naa r y f o rm rm i s disp ( k )

  / / r e s u l t i s d i s p la la y e d //

Scilab code Exa 2.49  Convert 38 point 21 to binary

1   // E xamp x ample le 2 − 49// 2   / / C o nv n v e rs r s i o n o f d e ci ci m a l t o b i n a r y / / 3   clc

34

: ’)

 

4 5 6 7 8 9 10 11

  / / c l e a r s t he he c o n s o l e //   clear

/ /= 0c l e a r s    q   b =0   s =0

all

e x i s t i n g v a r i a b l e s //

  // i n i t i a l i s i n g / /   / / a= a= i n p u t ( e n t e r t h e d e c i m a l n u m b e r t o b e c o n v e r t e d t o i t s b in i n ar ar y form ) 12   / / t a k i n g i n p u t f ro ro m t h e u s e r / / 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

  a=38.21  mo odulo (a,1)   d = m

  / / s e p a r a t i n g t he h e d ec e c i ma m a l p a rt r t f r o m t he he i n t e g e r //   a = floor ( a )

  / / r em e m o vi vi n g t h e d e c i m a l p a r t / /   while ( a > 0 )

  / / i n t e g e r p ar a r t c on o n v e rt r t e d t o e q u i v a l e n t b in i n ar a r y f o r m // //              

x = m  mo odulo (a,2) b=b+(10^q)*x a=a/2 a = floor ( a ) q=q+1 end f o r   i = 1: 1: 1 0

  / / t a ki k i n g v a l ue u e s a f t e r t he h e d ec e c im i m al a l p ar a r t and c o n ve v e r t i n g t o e q u i v a l e n t b in i n a ry r y f o r m // //                      

d=d*2 q = floor ( d ) s=s+q/(10^i) i f   d > = 1   then   d=d-1 end end k=b+s disp (   ’ t h e i n t e g e r p a arr t o f t h e b in i n aarr y f o r m i s : ’ ) disp ( b ) disp (   ’ t h e f r a c t i o n a l p a arr t o f t h e b in i n aarr y f o r m i s : ’ ) disp ( s )

35

 

40   / / r e s u l t

i s d i s p la la y e d //

Scilab code Exa 2.50   Binary addition

x ample le 2 − 50// 1   // E xamp 2   / / a d d i t i o n o f b i n a r y n um um b er er s / / 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 a r y 2 de d e c im 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 5 6 7 8 9 10 11 12 13 14 15 16 17

  clc

  / / c l e a r s t h e window / /   clear

  // 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 //

function   x = b i n a r y 2 d e c i m a l ( b i n ) p =1 q =1 z =0 b =0 w =0 f =0

  // i n i t i a l i s i n g / /   d = m  mo odulo (bin,1)

  / / s e p a r at a t i n g t he h e d ec e c im i m al a l p ar a r t f r o m t he h e i n t e g e r p ar ar t //

18   d = d * 1 0 ^ 1 0 19 20 21 22

  a = floor ( b i n )

23 24 25 26 27 28

           

  / / r em e m o vi vi n g t h e d e c i m a l p a r t / /   while ( a > 0 )

  / / l oo o o p t o e n t e r t h e b in i n ar a r y b i t s o f t he h e i n t e g e r p ar ar t i n t o a m aatt ri r i x // // r = m  mo odulo (a,10) b(1,q)=r a=a/10 a = floor ( a ) q=q+1 end

36

 

29   f o r   m = 1: 1: q - 1 l t i pl pl yi n g each b i t o f th e i n t e g er 30   / / m u lt 31 32 33 34 35

       

36 37 38 39 40 41 42 43

             

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

       

s p o n di di n g p o s i t i o n a l c = mc-o1 r r e sp f=f+b(1,m)*(2^c) end while ( d > 0 )

p ar ar t w i t h i t s

v al a l ue u e a n d a dd d d in i n g // //

  / / l oo o o p t o t ak a k e t he h e b i t s o f t he h e d ec e c im i m al a l p ar ar t i n to a mat r ix // // e = m  mo odulo (d,2) w(1,p)=e d=d/10 d = floor ( d ) p=p+1 end f o r   n = 1: 1: p - 1

  / / m u l t i p l y i n g e a ch c h b i t w i th t h i t s c o r r e sp s p o n di di n g p o s i t i o n a l v aall ue u e and a d dd d in i n g // // z=z+w(1,n)*(0.5)^(11-n) end z=z*10000 z = round ( z )

  / / r ou o u nd n d in i n g o f f t o 4 d e ci c i m al al p l a c e s //              

z=z/10000 x=f+z endfunction function   y = d e c i m a l 2 b i n a r y ( a ) q =0 b =0 s =0

  // i n i t i a l i s i n g / /   d = m  mo odulo (a,1)

  / / s e p a r a t i n g t he h e d ec e c i ma m a l p a rt r t f r o m t he he i n t e g e r //   a = floor ( a )

  / / r em e m o vi vi n g t h e d e c i m a l p a r t / /   while ( a > 0 )

  / / i n t e g e r p ar a r t c on o n v e rt r t e d t o e q u i v a l e n t b in i n ar a r y f o r m // //   x = m  mo odulo (a,2)

37

 

64   b = b + ( 1 0 ^ q ) * x 65   a = a / 2 66 67 68 69 70

      

71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86

       

floor a q= =q +1 (a) end f o r   i = 1: 1: 1 0

k i n g v a l ue u e s a f t e r t he h e d ec e c im i m al a l p ar a r t and   / / t a ki c o n ve v e r t i n g t o e q u i v a l e n t b in i n a ry r y f o r m // //

                   

d=d*2 q = floor ( d ) s=s+q/(10^i) i f   d > = 1   then   d=d-1 end end y=b+s endfunction x=binary2decimal(11.011) y=binary2decimal(10.001) z=x+y a=decimal2binary(z) disp (   ’ t h hee a d d i ttii o n o f t h hee b in i n aarr y n u m b e r s i s disp ( a )

: ’)

  / / r e s u l t i s d i s p la la y e d //

Scilab code Exa 2.51   Binary mutiplication

1   // E xamp x ample le 2 − 51// 2   / / m u l t i p l i c a t i o n o f b i na n a r y n u m b e r s // // 3   / / t h i s p r o g r a m r e q u i r e s f u n c t i o n s b i n ar a r y 2 de d e c im 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 5 6 7

  clc

  / / c l e a r s t h e window / /   clear

  // 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   function   x = b i n a r y 2 d e c i m a l ( b i n ) 9   p =1 10 11 12 13 14 15 16 17

        

q z= =1 0 b =0 w =0 f =0

  // i n i t i a l i s i n g / /   d = m  mo odulo (bin,1)

  / / s e p a r at a t i n g t he h e d ec e c im i m al a l p ar a r t f r o m t he h e i n t e g e r p ar ar t //

18 19 20 21 22

  d=d*10^10   a = floor ( b i n )

23 24 25 26 27 28 29 30

             

31 32 33 34 35

       

36 37 38 39 40 41

           

  / / r em e m o vi vi n g t h e d e c i m a l p a r t / /   while ( a > 0 )

  / / l oo o o p t o e n t e r t h e b in i n ar a r y b i t s o f t he h e i n t e g e r p ar ar t i n t o a m aatt ri r i x // // r = m  mo odulo (a,10) b(1,q)=r a=a/10 a = floor ( a ) q=q+1 end f o r   m = 1: 1: q - 1

  / / m u lt l t i pl p l 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 ar t w i t h i t s c o r r e sp s p o n di d i n g p o s i t i o n a l v al a l ue u e a n d a dd d d in i n g // // c=m-1 f=f+b(1,m)*(2^c) end while ( d > 0 )

  / / l oo o o p t o t ak a k e t he h e b i t s o f t he h e d ec e c im i m al a l p ar ar t i n to a mat r ix // // e = m  mo odulo (d,2) w(1,p)=e d=d/10 d = floor ( d ) p=p+1 end

39

 

1: p - 1 42   f o r   n = 1: c h b i t w i th th 43   / / m u l t i p l y i n g e a ch 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77

       

its

c o r r e sp s p o n di di n g

s i1t,ino)n*a(l0 .v5 al a)l^ue u(e1 1a-nnd) a dd d d in i n g // // z = zp+ ow ( end z=z*10000 z = round ( z )

  / / r ou o u nd n d in i n g o f f t o 4 d e ci c i m al al p l a c e s //              

z=z/10000 x=f+z endfunction function   y = d e c i m a l 2 b i n a r y ( a ) q =0 b =0 s =0

  // i n i t i a l i s i n g / /   d = m  mo odulo (a,1)

h e d ec e c i ma m a l p a rt r t f r o m t he he i n t e g e r //   / / s e p a r a t i n g t he   a = floor ( a )

  / / r em e m o vi vi n g t h e d e c i m a l p a r t / /   while ( a > 0 )

  / / i n t e g e r p ar a r t c on o n v e rt r t e d t o e q u i v a l e n t b in i n ar a r y f o r m // //              

x = m  mo odulo (a,2) b=b+(10^q)*x a=a/2 a = floor ( a ) q=q+1 end f o r   i = 1: 1: 1 0

  / / t a ki k i n g v a l ue u e s a f t e r t he h e d ec e c im i m al a l p ar a r t and c o n ve v e r t i n g t o e q u i v a l e n t b in i n a ry r y f o r m // //        

d=d*2 q = floor ( d ) s=s+q/(10^i) i f   d > = 1   then   d=d-1   end   end

40

 

78   y = b + s 79   e n d f u n c t i o n 80 81 82 83 84

        

x= =b bi in na ar ry y2 2d de ec ci im ma al l( (0 1. 01 .1 0) 01) y z=x*y a=decimal2binary(z) disp (   ’ t h hee m u l t i p l i c a t i o n o f t h hee b in i n a rry y numbers i s ) 85   disp ( a ) 86   / / r e s u l t i s d i s p la la y e d //

41

:’

 

Chapter 3 Binary Codes

Scilab code Exa 3.1  Add 647 to 492 in BCD

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

x ample le 3 − 1/ 1///   // E xamp   // //B BCD ad di t i on //   clc

  / / c l e a r s t he he c o n s o l e //   clear

  // 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 //

  a=647   b=492   f o r   i =1 =1: 3   x ( i ) = m = mo odulo (a,10)

       

  a=a/10   a = floor ( a )   y ( i ) = m = mo odulo (b,10)   b=b/10   b = floor ( b ) end d=x(1)+y(1) db=dec2bin(d) i f   d > 9   then   db=dec2bin(d+6)   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

 

22   e n d 23   e = x ( 2 ) + y ( 2 ) 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

   i e fb =  dd > e 9c 2  then bin(e)   eb=dec2bin(e+1)   e=e+1   end   if   e >9   eb=dec2bin(e+6)   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 ’ ) )   end   f=x(3)+y(3)   fb=dec2bin(f)   i f   e > 9   then   fb=dec2bin(f+1)   f=f+1   end   if   f >9   fb=dec2bin(f+6)   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 ’ ) )   dc(4)=1   end   dc(1)=bin2dec(db)   dc(2)=bin2dec(eb)   dc(3)=bin2dec(fb)   z =0   f o r   i =1 =1 : 4   z=z+dc(i)*(10^(i-1))   end   disp ( z )   disp (   ’ e q u i v a l e n t b i n a r y f o rrm m ’)   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 ( dc(2),4)+dec2bin(dc(1),4)) 54   disp ( p ) 55   / / a ns n s we we r i s d i s p l a y e d / /

43

 

Scilab code Exa 3.2  Add 4318 and 7678 in BCD

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32

  // E xamp x ample le 3 − 2/ 2///   // //B BCD ad di t i on //   clc

  / / c l e a r s t he he c o n s o l e //   clear

  // 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 //

  a=4318   b=7678 =1: 4   f o r   i =1

       

  x ( i ) = m = mo odulo (a,10)   a=a/10   a = floor ( a )   y ( i ) = m = mo odulo (b,10)   b=b/10   b = floor ( b ) end d=x(1)+y(1) db=dec2bin(d) i f   d > 9   then   db=dec2bin(d+6)   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 ’ ) )

       

en e =d x(2)+y(2) eb=dec2bin(e) i f   d > 9   then   eb=dec2bin(e+1)   e=e+1   end   if   e >9   eb=dec2bin(e+6)   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 ’ ) )   end

44

 

33   f = x ( 3 ) + y ( 3 ) 34   f b = d e c 2 b i n ( f ) 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64

  i f    e > f 9b =  then dec2bin(f+1)   f=f+1   end   if   f >9   fb=dec2bin(f+6)   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 ’ ) )   end   g=x(4)+y(4)   gb=dec2bin(g)   i f   f > 9   then   gb=dec2bin(g+1)   g=g+1   end   if   g >9   gb=dec2bin(g+6)   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 ’ ) )   dc(5)=1   end   dc(1)=bin2dec(db)   dc(2)=bin2dec(eb)   dc(3)=bin2dec(fb)   dc(4)=bin2dec(gb)   z =0   f o r   i =1 =1 : 5   z=z+dc(i)*(10^(i-1))   end   disp ( z )   disp (   ’ e q u i v a l e n t b i n a r y f o rrm m ’)   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 ( dc(3),4)+dec2bin(dc(2),4)+dec2bin(dc(1),4)) 65   disp ( p ) 66   / / a ns n s we we r i s d i s p l a y e d / /

45

 

Scilab code Exa 3.3  Add 3 and 2 in XS3

1 2 3 4 5 6 7 8 9

  // E xamp x ample le 3 − 3/ 3///   / / aad d d 3 a n d 2 i n E xc x c es e s s 3 c o de de / /   clc

  / / c l e a r s t he he c o n s o l e //   clear

  // 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 //

  n =3   m =2   z =0

10 11 12 13 14 15 16 17 18 19 20 21

         

22 23 24 25 26 27 28 29 30 31 32

=1: 4   f o r   i =1   g(i)=bitand(x(i),y(i))   p(i)=bitor(x(i),y(i))   end   c(1)=0   f o r   i =1 =1: 4   c(i+1)=bitor(g(i),bitand(p(i),c(i)))   end   i f   c ( 5 ) = = 1   then   z=dec2bin(bin2dec(ea)+bin2dec(eb)+3)   end

a=0110 b=0101 ea=dec2bin(n+3) eb=dec2bin(m+3) f o r   i =1 =1: 4   x ( i ) = m = mo odulo (a,10)   a=a/10   a = floor ( a )   y ( i ) = m = mo odulo (b,10)   b=b/10   b = floor ( b )   end

46

 

33   i f   c ( 5 ) = = 0   then   z=dec2bin(bin2dec(ea)+bin2dec(eb)-3) 34 35 n d (   ’ e q u i v a l e n t b in disp i n aarr y n u m b e r a f t e r 36    e addition ’   ) 37   disp ( z ) 38   disp (   ’ e q u i v a l e n t d e c i m a l n u m b e r ’ ) 39   disp ( m + n ) la y e d // 40   / / r e s u l t i s d i s p la

Scilab code Exa 3.4  Add 6 and 8 in XS3

1 2 3 4 5 6 7 8 9 10 11 12

x ample le 3 − 4/ 4///   // E xamp   / / aad d d 6 a n d 8 i n E xc x c es e s s 3 c o de de / /   clc

  / / c l e a r s t he he c o n s o l e //   clear

  // 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 //

n =6 m =8 a=1001 b=1011 ea=dec2bin(n+3) eb=dec2bin(m+3)

13   f o r   14 15     16 17   18   19   20   e n d 21   f o r 22     23

  i =1 =1: 4 odulo (a,10) x ( i ) = m = mo a=a/10 a = floor ( a ) odulo (b,10) y ( i ) = m = mo b=b/10 b = floor ( b )   i =1 =1: 4 g(i)=bitand(x(i),y(i)) p(i)=bitor(x(i),y(i))

47

e x c es s 3

 

24   e n d 25   c ( 1 ) = 0 26 27 28 29 30 31 32 33 34 35

  f o  r   ic=1 =(1i:+ 14) = b i t o r ( g ( i ) , b i t a n d ( p ( i ) , c ( i ) ) )   end   i f   c ( 5 ) = = 1   then   z=dec2bin(bin2dec(ea)+bin2dec(eb)+3)   end   i f   c ( 5 ) = = 0   then   z=dec2bin(bin2dec(ea)+bin2dec(eb)-3)   end   disp (   ’ e q u i v a l e n t b in i n aarr y n u m b e r a f t e r e x c e s s 3 addition ’   ) 36   disp ( z ) 37   disp (   ’ e q u i v a l e n t d e c i m a l n u m b e r ’ ) 38   disp ( m + n ) 39   / / r e s u l t i s d i s p la la y e d //

Scilab code Exa 3.5  Convert binary 1011 to Gray code

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

x ample le 3 − 5/ 5///   // E xamp   / / b i n a r y t o g ra r a y c od od e / /   clc

  / / c l e a r s t he he c o n s o l e //   clear

  // 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 //

  a=1011 =1: 4   f o r   i =1 odulo (a,10)   x ( i ) = m = mo   a=a/10   a = floor ( a )   end   y(4)=x(4)   k =3   while ( k > 0 )

48

 

16 17

 

i f   b i t a n d ( x ( k + 1 ) , x ( k ) ) = = 1   then   y(k)=bitcmp(1,1)

18 19 20 21 22 23 24 25 26 27 28 29 30

 

  else y ( k ) = b i t o r ( x ( k + 1 ) , x ( k ) ) end k=k-1

      end

  // di sp la y //   z =0 =1: 4   f o r   i =1   z=z+y(i)*(10^(i-1))   end   disp (   ’ e q u i v a l e n t g rra a y c od od e ’ )   disp ( z )

  / / e q u i v a l e n t g ra r a y c od 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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

x ample le 3 − 6/ 6///   // E xamp   / / b i n a r y t o g ra r a y c od od e / /   clc

  / / c l e a r s t he he c o n s o l e //   clear

  // 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 //

  a=1001011 =1: 7   f o r   i =1   x ( i ) = m = mo odulo (a,10)   a=a/10   a = floor ( a )   end   y(7)=x(7)   k =6   while ( k > 0 )   i f   b i t a n d ( x ( k + 1 ) , x ( k ) ) = = 1   then   y(k)=bitcmp(1,1)

49

 

 

18 19 20 21 22 23 24 25 26 27 28 29 30

else  

y(k)=bitor(x(k+1),x(k))

en    k =d k-1   end

  // di sp la y //   z =0   f o r   i =1 =1: 7   z=z+y(i)*(10^(i-1))   end   disp (   ’ e q u i v a l e n t g rra a y c od od e ’ )   disp ( z )

  / / e q u i v a l e n t g ra r a y c od 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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

  // E xamp x ample le 3 − 7/ 7///   / / g ra r a y c od o d e t o b i n a ry ry / /   clc

  / / c l e a r s t he he c o n s o l e //   clear

  // 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 //

  a=1011   f o r   i =1 =1: 4

       

odulo (a,10)   x ( i ) = m = mo   a=a/10   a = floor ( a ) end y(4)=x(4) k =3 while ( k > 0 )   i f   b i t a n d ( y ( k + 1 ) , x ( k ) ) = = 1   then   y(k)=bitcmp(1,1)   else   y(k)=bitor(y(k+1),x(k))

50

 

   

20 21 22 23 24 25 26 27 28 29

end k=k-1

en    z =d 0   f o r   i =1 =1: 4   z=z+y(i)*(10^(i-1))   end   disp (   ’ e q u i v a l e n t b i n a r y c o od de ’)   disp ( z )

  / / e q u i v a l e n t b i na n a r y c od 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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

x ample le 3 − 8/ 8///   // E xamp   / / g ra r a y c od o d e t o b i n a ry ry / /   clc

  / / c l e a r s t he he c o n s o l e //   clear

  // 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 //

  a=1001011   f o r   i =1 =1: 7   x ( i ) = m = mo odulo (a,10)   a=a/10   a = floor ( a )        

end y(7)=x(7) k =6 while ( k > 0 )   i f   b i t a n d ( y ( k + 1 ) , x ( k ) ) = = 1   then   y(k)=bitcmp(1,1)   else   y(k)=bitor(y(k+1),x(k))   end   k=k-1   end

51

 

23   z = 0 =1: 7 24   f o r   i =1 25 26 27 28 29

  z=z+y(i)*(10^(i-1))   end   disp (   ’ e q u i v a l e n t b i n a r y c o od de : ’)   disp ( z )

  / / e q u i v a l e n t b i na n a r y c od od e i s d i s p l a y e d //

Scilab code Exa 3.9  Hamming code for 1011

1 2 3 4 5 6 7 8 9 10 11 12 13 14

  // E xamp x ample le 3 − 9/ 9///   //H //Haamming cod e //   clc

  / / c l e a r s t h e comman d w in in do do w / /   clear

  // 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 //

  z=1011

  // in pu t //   a=0;b=0;c=0;d=0;

  // taking the input //   for   i = 1 : 7   x(i)=0   i f   i = = 3   then   x(i)=1

15 end     i f   i = = 5   then 16 17   x(i)=1 end   18 19   i f   i = = 7   then 20   x(i)=1 end 21   22   e n d 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 24   f o r   i =1 =1: 7   i f   x ( i ) = = 1   then 25

52

1 , 3 , 5 , 7/ 7/ /

 

26 27

   

a=a+1 end

28 29 30 31 32 33 34 35 36 37 38 39

  end   d = m  mo odulo (a,2)   i f   ( d = = 1 )   then   x(1)=1   end

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

  e n  d e n d  mo odulo (b,2)   d = m   i f   ( d = = 1 )   then   x(2)=1   end

  / / 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/ 7/ /   f o r   i =2 =2: 7   i f   ( i = = 5 )   then   continue   end   i f   ( x ( i ) = = 1 )   then   b=b+1

7/ /   / / 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/   for   i = 5 : 7   i f   ( x ( i ) = = 1 )   then   c=c+1 end     end   d = m  mo odulo (c,2)   i f   ( d = = 1 )   then   x(4)=1   end

  / / d i s p l a y i n g t he he r e s u l t //   disp (   ’ t h hee h a m m i n g c od o d e f o r t h i s d aatt a i s g i v vee n a s ’ )   for   i = 1 : 7   disp ( x ( i ) )   end

53

 

Scilab code Exa 3.10  Hamming code for 0101

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32

  // E xamp x ample le 3 − 10//   //H //Haamming cod e //   clc

  / / c l e a r s t h e comman d w in in do do w / /   clear

  // 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 //

  z=0101

  // in pu t //   a=0;b=0;c=0;d=0;

  // taking the input //   for   i = 1 : 7   x(i)=0   i f   i = = 3   then   x(i)=1   end   i f   i = = 6   then   x(i)=1   end   end

  / / 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/ 7/ /   f o r   i =1 =1: 7  

       

i f   i = = 6   then continue end     i f   x ( i ) = = 1   then   a=a+1 end   end d = m  mo odulo (a,2) i f   ( d = = 1 )   then   x(1)=1 end  

54

 

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: 7 34   f o r   i =2 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57

         

 

2 , 3 , 6 , 7/ 7/ /

i f   x ( i ) = = 1   then b=b+1 end

  end d = m  mo odulo (b,2) i f   ( d = = 1 )   then   x(2)=1 end

  / / 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/ 7/ /   for   i = 5 : 7   i f   ( x ( i ) = = 1 )   then   c=c+1   e n  d e n d  mo odulo (c,2)   d = m   i f   ( d = = 1 )   then   x(4)=1   end

he r e s u l t //   / / d i s p l a y i n g t he   disp (   ’ t h hee h a m m i n g c od o d e f o r t h i s d aatt a i s g i v vee n a s ’ )   for   i = 1 : 7   disp ( x ( i ) )   end

Scilab code Exa 3.11  Correction of Hamming code 1111101

1 2 3 4 5 6 7

  // E xamp x ample le 3 − 11//   / / C o r r e c t i o n o f r e c e i v e d Hamming c o de de / /   clc

he c o n s o l e //   / / c l e a r s t he   clear

  // 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 //

  z=1111101

55

 

8   / / i n c o r r e c t c od o d e t ak a k en e n a s i n pu pu t // 9   a=0;b=0;c=0;d=0; 10 11 12 13 14 15 16 17 18 19 20 21

i n1 g: 7t he h e i n pu p u t i n a n a r ra ra y //    f/ /o rt a  ik =1 =in

22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45

en d odulo (a,2)    d = m  mo   i f   d = = 1   then   disp (   ’ w rro ong e n t r y f o r 4 , 5 , 6 , 7 ’ )   x(4)=1   else   disp (   ’ c o r r e c t e n ttrr y f o r 4 , 5 , 6 , 7 ’ )   end

   

x(i)=1 i f   i = = 2   then   x(i)=0   end   end

k i n g f o r 4 , 5 , 6 , 7 e ve ve n p a r i t y //   / / c h e c ki   f o r   i =4 =4: 7   i f   x ( i ) = = 1   then   a=a+1 end  

k i n g f o r 2 , 3 , 6 , 7 e ve ve n p a r i t y //   / / c h e c ki   f o r   i =2 =2: 7 i f   i = = 4   then     continue   end   i f   i = = 5   then   continue   end   i f   x ( i ) = = 1   then   a=a+1 end     end   d = m  mo odulo (a,2)   i f   d = = 1   then   disp (   ’ w rro ong e n t r y f o r   x(2)=1

2 ,3 ,6 ,7 ’)

56

 

46   else   disp (   ’ c o r r e c t 47

e n ttrr y f o r 2 , 3 , 6 , 7 ’ )

48   e n d 49   / / c h e c ki k i n g f o r 1 , 3 , 5 , 7 e ve ve n p a r i t y // 50   f o r   i =1 =1: 7 i f   i = = 2   then   51 52   continue   end 53 54   i f   i = = 4   then 55   continue end 56   57   i f   i = = 6   then   continue 58 end 59   60 61 62 63 64 65 66 67 68 69 70 71

     

     

    if   x a( =i a) += 1 = 1   then end   end d = m  mo odulo (a,2) i f   d = = 1   then   disp (   ’ c o r r e c t e n ttrr y f o r 1 , 3 , 5 , 7 ’ )   x(1)=1 else   disp (   ’ w rro ong e n t r y f o r 1 , 3 , 5 , 7 ’ ) end disp (   ’ T h hee re r e ffoo rree , b i t 2 i s i n e r r o r a n d t h hee c o r r e c t e d code i s : ’ ) f o r   i =1 =1: 7   disp ( x ( i ) ) end

72   73 74   75   / / c o r r e c t

c od od e i s d i s p l a y e d //

57

 

Chapter 4 Boolean Algebra

Scilab code Exa 4.1   Demorganize a function

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

x ample le 4 − 1/ 1///   // E xamp   / / d e m o rg r g a n i ze ze f u n c t i o n / /   clc

  / / c l e a r s t he he c o n s o l e //   clear

  // 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 // h e g i ve v e n f u n c ti ti o n i s a s f o l l o w s //   / / t he   disp (   ’ G iiv v en e n f u n c t i o n −   (AB (AB ’ ’+C) ’+C) ’ ’ ’ )   disp (   ’ no now d e m oorr g a n nii z i n ngg t h e f u n c t i o n ’ )   disp (  ’ c om om p pll em e m een nt fu nct io n ’)   disp (   ’ AB ’ ’+ ’+C C ’)   disp (   ’ c h a an n g iin ng operato rs ’)   disp (   ’ (A (A+ +B ’ ’ ) (C) ’ )   disp (  ’ c o om mp pll em e m een nt va ri ab le s ’)   disp (   ’ ( A ’ ’+ ’+B) B) ( C ’ ’ ) ’ )   / / f i n a l a n s w e r d i sp s p l ay 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

 

1   // E xamp x ample le 4 − 2/ 2/// 2   / / R ed e d u ci ci n g a n e x p r e s s i o n / / 3 4 5 6 7 8 9 10 11 12 13 14

  clc

  / / c l e a r s t he he c o n s o l e //   clear

  // 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 //   / / t he h e g i ve ve n e x p r e s s i o n i s a s f o l l o w s //   disp (   ’ G iiv v een n E x p r e s ssii o n −   A A+ +B( C+ C+((DE) ’ ’ ) ’ ’ ’ ) rg a n t h e o r eem m ’)   disp (   ’ a p p l y i n g D e M o rg   disp (   ’ r ed ed u ucc eed d e x p r es s i o n : ’ )   disp (   ’ A+B(C B(C+ +D ’ ’+ ’+E E’ ’) ’ ’ ’)   disp (   ’ A p p l y i n g D e M o rg rg a n t h e o r eem m aga in ’ )   disp (   ’ A+B(C ’ ’D ’DE) E) ’ )   disp (   ’ T h hee rree ffoo re re f i n a l re d u ced e x p r e ss i o n i s :

15 disp ’DE 16    / / f i n(  a’ l A+ eBxCp ’r e’D sE s i o’ n) d i s pl pl a y ed //

Scilab code Exa 4.3   Reduce an expression

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

x ample le 4 − 3/ 3///   // E xamp Ree du d u ci c i ng ng a g i v e n e x p r e s s i o n //   //R   clc

  / / c l e a r s t he he c o n s o l e //   clear

  // 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 //   / / t he h e g i ve ve n e x p r e s s i o n i s a s f o l l o w s //   disp (   ’ G iiv v een n E x p r e s ssii o n −   ( (A (AB) B) ’ ’+A ’+A ’ ’+ ’+A AB) ’ ’ ’ )   disp (   ’ ap pl y i ng De DeM M oorr gan gan T h hee oorr em em ’ )   disp (   ’ r ed ed u ucc ed ed e x p prr e s s i o n ’ )   disp (   ’ (A ’ ’’+ +B ’ ’+ ’+A A ’ ’+ ’+A AB) ’ ’ ’ )   disp (  ’ a s A+A=A ’ )   disp (   ’ (A’ ’+ ’+B B ’ ’+ ’+A AB) ’ ’ ’ )   //B By y l a w 2 0 // //   disp (   ’ (A’ ’’+ +B ’ ’+ ’+A) A) ’ ’ ’ )   disp (  ’ r e a r r a n g i n g ’ ) 59

’)

 

17   disp (   ’ (A (A+ +A ’ ’’+ +B ’ ’ ) ’ ’ 18   / / by b y l aw a w 1 3/ 3/ / 19 20 21 22 23

’)

  disp (   ’ ( 1 +B ’ ’ ) ’ ’

’)   / / by a w 1 1/ 1/ / b y l aw   disp (   ’ 1 ’ ’ ’ )   disp (  ’ 0 ’ ) e d uc u c ed e d e x p r e s s i o n i s d i s p la la y e d /   / / f i n a l r ed

Scilab code Exa 4.4  Reduce a boolean expression

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

  // E xamp x ample le 4 − 4/ 4///   //R Ree du d u ci c i ng ng a g i v e n e x p r e s s i o n //   clc

  / / c l e a r s t h e window / /   clear

  // 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 //   / / t he h e g i ve ve n e x p r e s s i o n i s a s f o l l o w s //   disp (   ’ G iiv v een n E x p r e s ssii o n −   AB+( AB+(A AC) ’ ’+ ’+A AB ’ ’C ’C(AB (AB+ +C)   disp (  ’ M u l t i p l y i n g ’ ) B+(A AC) ’ ’’+ +AABB ’ ’’C C+AB ’ ’CC ’ )   disp (   ’ AB+(   // using laws 18 ,6 ,8 ,9//   disp (   ’ AB+( +(A AC) ’ ’+ ’+A AB ’ ’C ’ )   disp (   ’ a p p l y i n g D e M o rg rg a n t h e o r eem m ’)   disp (   ’ AB+A ’ ’+ ’+C C ’ ’+ ’+A AB ’ ’C ’ )   disp (  ’ r e a r r a n g e ’ )   disp (   ’ AB+C ’ ’+ ’+A A ’ ’+ ’+A AB ’ ’C ’ )   / / r e d uc u c e u s i n g l aw a w 2 0/ 0/ /   disp (   ’ AB+C ’ ’+ ’+A A ’ ’+ ’+B B ’ ’C ’ )   disp (   ’ r e a r r a n g i n g a g a i n ’ )   disp (   ’ A ’ ’+ ’+A AB+C ’ ’+ ’+B B ’ ’C ’ )   / / r e d uc u c e u s i n g l aw a w 2 0/ 0/ /   disp (   ’ A ’ ’+ ’+B B+C ’ ’’+ +B ’ ’ ’ )   / / u s i n g l a ws w s 1 1 a n d 1 3/ 3/ /   disp (  ’ 1 ’ ) e d uc u c ed e d e x p r e s s i o n i s d i s p la la y e d //   / / f i n a l r ed 60

’)

 

Scilab code Exa 4.5  Reduce a Boolean expression

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

x ample le 4 − 5/ 5///   // E xamp   //R Ree d uc uc e a g i v e n e x p r e s s i o n / /   clc

  / / c l e a r s t h e window / /   clear

  // 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 //   / / t he h e g i ve ve n e x p r e s s i o n i s a s f o l l o w s //   disp (   ’ G iiv v een n E x p r e s ssii o n −   ( ( AB ’ ’+ ’+A ABC) ’ ’+A( ’+A( B+AB ’ ’ ) ) ’ ’ ’ )(  ’ f a c t o r i s e ’ )   disp   disp (  ’ ( (A(B ’ ’+ ’+B BC) ) ’ ’+ ’+A(B A(B+ +AB ’ ’ ) ) ’ ’

’)

  / / r e d uc u c e u s i n g l a ws w s 1 8 a n d 2 0/ 0/ /   disp (   ’ ( (A(B ’ ’+ ’+C) C) ) ’ ’+A(B ’+A(B+ +A) ) ’ ’ ’ )   disp (  ’ m u l t i p l y ’ )   disp (   ’ ( (AB ’ ’+ ’+A AC) ’ ’+ ’+A AA+AB) ’ ’ ’ )   //reduce using laws 7 ,8 ,11 ,18//   disp (   ’ ( (A (AB B ’ ’’+ +AC) ’ ’+ ’+A) A) ’ ’ ’ )   disp (  ’ de mo r gan iz e ’ )   disp (   ’ ( (A ’ ’’+ +B) (A ’ ’’+ +C ’ ’ )+ )+A) A) ’ ’ ’ )   disp (  ’ m u l t i p l y ’ )   disp (   ’ (A ’ ’A’ ’+ ’+A A ’ ’C ’ ’+ ’+A A ’ ’B ’B+ +BC ’ ’+ ’+A) A) ’ ’ ’ )   / / Re R e d uc uc e u s i n g l a w s 1 8 , 8 / /   disp (   ’ (A ’ ’(1+C’ ’+ ’+B)+ B)+B BC ’ ’+ ’+A) A) ’ ’ ’ )   / / r e d u c e u s i n g l aw aw 1 8 , 7 , 1 1 / /   disp (   ’ 1 ’ ’ ’ )   disp (  ’ 0 ’ )   / / f i n a l r ed e d uc u c ed e d e x p r e s s i o n i s d i s p la la y e d //

61

 

Chapter 6 Combinational Logic

Scilab code Exa 6.1  Convert A plus B to minterms

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

x ample le 6 − 1/ 1///   // E xamp   / / c o n v e r t A+B t o m i n t er er m s / /   clc

  / / c l e a r s t he he c o n s o l e //   clear

  // 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 // i n te t e rm rm s / /   / / c o n v e r s i o n t o m in   disp (   ’ G i v e n e x p r eess s i o n −   A+B ’)   disp (   ’ o n s o l v i n g ’ )   disp (   ’ A(B A(B+ +B ’ ’ )+ )+B(A B(A+ +A ’ ’ ) ’ )   disp (  ’ m u l t i p l y i n g ’ )   disp (   ’ AB+AB ’ ’+A ’+AB+A ’ ’B ’ )   disp (   ’ we know A+A=A ’ )   disp (   ’ AB+AB ’ ’+ ’+A A ’ ’B ’ )   / / f i n a l r e s u l t i s d i sp s p l ay a y e d //

Scilab code Exa 6.2  Find minterms for A plus BC

62

 

1   // E xamp x ample le 6 − 2/ 2/// 2   // f i n d mint e r ms f o r A+BC// 3 4 5 6 7 8 9 10 11 12 13 14

  clc

  / / c l e a r s t he he c o n s o l e //   clear

  // 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 //   / / c o n v e r s i o n t o m in i n te t e rm rm s / /   disp (   ’ G iiv v een n e x p r e s s i o n −   A+BC ’ )   disp (   ’ o n s o l v i n g ’ )   disp (   ’ A(B A(B+ +B ’ ’ ) (C (C+ +C ’ ’ )+ )+B BC(A C(A+ +A ’ ’ ) ’) (AB B+AB ’ ’ ) (C (C+ +C ’ ’ )+ )+B BCA+BCA ’ ’ ’)   disp (   ’ (A   disp (  ’ m u l t i p l y i n g ’ )   disp (   ’ C ’ ’AB ’AB+ +AB ’ ’C ’C+ +AB ’ ’’C C+AB ’ ’C ’ ’+ ’+B BCA+BCA ’ ’ ’ )   / / f i n a l r e s u l t i s d i sp s p l ay a y e d //

Scilab code Exa 6.3  Find minterms for AB plus ACD

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

  // E xamp x ample le 6 − 3/ 3///   // fi n d minterms f o r AB+ACD//   clc

  / / c l e a r s t h e window / /   clear

  // 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 //   / / c o n v e r s i o n t o m in i n te t e rm rm s / /   disp (   ’ G i v e n e x p r eess s i o n −   AB AB+ +AC ACD D ’)   disp (   ’ o n s o l v i n g ’ )   disp (  ’ AB( B(C C+C ’ ’ ) (D (D+ +D ’ ’ )+ )+A ACD(B D(B+ +B ’ ’ ) ’ )   disp (  ’ m u l t i p l y i n g ’ )   disp (   ’ ABCD+ABC ’ ’D ’ ’+ ’+A ABC BC’’ ’D ’D+ +ABCD ’ ’+ ’+AB AB ’ ’CD ’CD+ +ABCD   disp (   ’ we know A+A=A ’ )   disp (   ’ ABC ’ ’D ’ ’+ ’+A ABC ’ ’D ’D+ +ABCD ’ ’+ ’+A AB ’ ’C ’CD D+ABCD ’ )   / / r e s u l t i s d i s p la la y e d //

63

’)

 

Scilab code Exa 6.4  Minterm designation

1 2 3 4 5 6 7 8 9

  // E xamp x ample le 6 4/ 4///   //m mii nt n t er er m d− e s i g n a t i o n o f AB ’ ’ C ’ ’ D’ D’ ’ / /   clc

  / / c l e a r s t h e window / /   clear

  // 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 //   disp (   ’ c o p py y o r i g i n a l term ’ )   disp (   ’ AB ’ ’ C ’ ’ D ’ ’ ’)   disp (   ’ s u b s t i t u t e o n nee s f o r n oon nb baa rrrr ed ed l e t t e r s and z e r o e s f o r b aarr re re d l e t t e r s ’ ) 10   disp (   ’ a f t e r s u b s t i t u t i o n ’ ) 11   disp (   ’ 1 0 0 0 ’ ) 12 13 14 15 16 17

        

a = b i( n 2’ dt h e ec (  ’d 100 0 ’aal)l e q u i v a l e n t o f 1 0 0000 i s : ’ ) disp he eecc iim m disp ( a ) disp (   ’ T h hee rree ffoo re r e d e c im i m al al s u b s c r i p t o f m i s 8 ’ ) disp (   ’ AB ’ ’ C ’ ’ D ’ ’ = m m88 ’ )

  / / r e s u l t i s d i s p la la y e d //

Scilab code Exa 6.5  Minterm designation

1 2 3 4 5 6 7 8 9

x ample le 6 − 5/ 5///   // E xamp i n te t e rm r m d e s i g n a t i o n o f W’ ’ X ’ ’ YZ YZ ’ ’ / /   / / m in   clc

  / / c l e a r s t h e window / /   clear

  // 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 //   disp (   ’ c o p py y o r i g i n a l term ’ )   disp (   ’ W W’’ ’ X ’ ’ YZ ’ ’ ’)   disp (   ’ s u b s t i t u t e o n nee s f o r n oon nb baa rrrr ed ed l e t t e r s and z e r o e s f o r b aarr re re d l e t t e r s ’ ) 10   disp (   ’ a f t e r s u b s t i t u t i o n ’ ) 11   disp (   ’ 0 0 1 0 ’ ) 64

 

12   a = b i n 2 d e c (  ’ 001 0 ’ ) hee d eecc iim m aall e q u i v a l e n t o f 0 0011 0 i s : ’ ) 13   disp (   ’ t h e r e f o r e t h 14 15 16 17

  disp ( a )   disp (   ’ T h hee rree ffoo re r e d e c im i m al al s u b s c r i p t o f m i s 2 ’ )   disp (   ’ W W’’ ’ X ’ ’ Y YZ Z’ ’ = m m22 ’ )

  / / r e s u l t i s d i s p la la y e d //

Scilab code Exa 6.6   2 variable mapping

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

  // E xamp x ample le 6 − 6/ 6///   //map C=A’ ’ B’ ’+AB’ ’ / /   clc

  / / c l e a r s t he he c o n s o l e //   clear

  // 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 //   / / Ma Ma p p i n g t h e e x p r e s s i o n / /   disp (   ’ B’ ’ B ’) 0 ’)   disp (   ’ A ’ ’ 1   disp (   ’ A 1 0 ’)   disp (   ’ From t he h e map , h ig ig h o u utt p pu u ttss f o r 0 a n d 2 ’ )   a =[ = [0 0 ; 1 0]   disp ( a )   f o r   i =1 =1: 2   i f   a ( i , 1 ) = = 1   then   b ( i , 1 ) =  ’  ’A A’   else   b ( i , 1 ) =  ’A’ ’ ’   end   i f   a ( i , 2 ) = = 1   then   b ( i , 2 ) =  ’ B ’   else   b ( i , 2 ) =  ’B’ ’ ’   end   end   m = strcat ( [ b ( 1 , 1 ) , b ( 1 , 2 ) ] )

65

 

27   n = strcat ( [ b ( 2 , 1 ) , b ( 2 , 2 ) ] ) uaa t i n g e x p r e s s i o n f r o m t r u t h t a b l e a n d 28   disp (   ’ e v a l u

map ’ ) 29   x = strcat ( [ m ”+” , n ] ) ; 30   disp ( x ) 31   / / E x p r e ss ss i o n i s d i s p l a y e d / /

Scilab code Exa 6.7   3 variable mapping

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

  // E xamp x ample le 6 − 7/ 7///   / /Map X=ABC+AB ’ ’ C+AB’ ’ C ’ ’ / /   clc

  / / c l e a r s t he he c o n s o l e //   clear

  // 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 //   / / Ma Ma p p i n g t h e e x p r e s s i o n / /   disp (   ’ B ’ ’ C ’ ’ B ’ ’ C BC BC ’ ’ ’ )   disp (   ’ A ’ ’ 0 0 0 0 ’)   disp (   ’ A 1 1 1 0 ’)   disp (   ’ From t he h e map , h ig i g h o u ttp p u ttss f o r 4 , 5 a n d 7 ’ )   a =[1 0 0 ; 1 0 1 ; 1 1 1]   disp ( a )   f o r   i =1 =1: 3   i f   a ( i , 1 ) = = 1   then   b ( i , 1 ) =  ’  ’A A’   else   b ( i , 1 ) =  ’A’ ’ ’   end   i f   a ( i , 2 ) = = 1   then   b ( i , 2 ) =  ’ B ’   else   b ( i , 2 ) =  ’B’ ’ ’   end   i f   a ( i , 3 ) = = 1   then   b ( i , 3 ) =  ’  ’C C’

66

 

27 28

 

else  

b ( i , 3 ) =  ’C ’ ’ ’

29   end 30   e n d 31   disp (   ’ e v a l u uaa t i n g e x p r e s s i o n f r o m t r u t h t a b l e a n d map ’ ) 32   l = strcat ( [ b ( 1 , 1 ) , b (1 ( 1 , 2 ) , b ( 1 , 3) 3) ] ) ( 2 , 2 ) , b ( 2 , 3) 3) ] ) 33   m = strcat ( [ b ( 2 , 1 ) , b (2 ( 3 , 2 ) , b ( 3 , 3) 3) ] ) 34   n = strcat ( [ b ( 3 , 1 ) , b (3 35   x = strcat ( [ l ”+” , m ”+” , n ] ) 36   disp ( x ) ss i o n i s d i s p l a y e d / / 37   / / E x p r e ss

Scilab code Exa 6.8   Mapping an expression

1 2 3 4 5 6 7 8 9 10 11 12 13 14

  // E xamp x ample le 6 − 8/ 8///   / / M ap ap pi pi ng ng a n e q u a t i o n / /   clc

he c o n s o l e //   / / c l e a r s t he   clear

  // 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 //   / / Ma Ma p p i n g t h e e x p r e s s i o n / /   disp (  ’Z  ’Z= =m(9 ,2 0 ,2 1 ,2 9 ,3 0 ,3 1) ’ )   disp (   ’ A’ ’ A ’)   disp (   ’ D ’ ’ E ’ ’ D ’ ’ E DE DE ’ ’ ’ ’E DE DE’ ’ ’ )   disp (   ’ B ’ ’ C ’ ’ 0 0 0 0 0 0 0 ’)   disp (   ’ BC ’ ’ 0 0 0 0 1 0 0 ’)   disp (   ’ BC 0 0 0 0 1 1 1 ’) 0 1 0 0   disp (   ’ BC ’ ’ 0 0 0 ’) 67

D ’ ’E ’ ’ D 0 1 0 0

 

h e map , 15   disp (   ’ From t he

h ig i g h o u ttp p u ttss f o r

9 ,20 ,21 ,29 ,30 ,31 ’ ) 16   / / T h e r e f o r e t h e kmap i s

displayed //

Scilab code Exa 6.9   Mapping an expression

1 2 3 4 5 6 7 8

9 10 11 12 13 14 15

  // E xamp x ample le 6 − 8/ 8/// ap pi pi ng ng a n e q u a t i o n / /   / / M ap   clc

  / / c l e a r s t he he c o n s o l e //   clear

  // 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 //   / / Ma Ma p p i n g t h e e x p r e s s i o n / /   disp (  ’ Z= Z=m m (0 ,1 ,3 ,5 ,7 ,10 ,11 ,2 1 ,22 ,23 ,24 ,2 6 ,3 0 ,32 ,34 ,35 ,4 0 ,41 ,46 ,47 ,5 0 ,51 ,52 , ’)   disp (   ’ B’ ’ B ’)   disp (   ’ E ’ ’ F ’ ’ E ’ ’ F EF EF ’ ’ E ’ ’F ’ ’ E ’ ’F EF EF ’ ’ ’ )   disp (   ’ C ’ ’D ’ ’ 1 1 1 0 0 0 0 0 ’)   disp (   ’ A ’ ’ CD ’ ’ 0 1 1 0 0 1 1 1 ’)   disp (   ’ CD 0 0 0 0 0 0 0 1 ’)   disp (   ’ CD ’ ’ 0 0 1 1 1 0 0 1 ’)   disp (  ’ ’)

16   disp (   ’

0 17   disp (   ’ A 0

C ’ ’D ’ ’ 1 1 CD ’ ’ 0 0

1

0

1

1

0

’) 0 ’)

0

0

0

1

68

 

18   disp (   ’

1

CD 0

0 0

0

1

1

1

’)

19   disp (   ’

CD ’ ’ 1 1 0 0 0 0 0 0 ’) h e map , h ig i g h o u ttp p u ttss f o r 20   disp (   ’ From t he 0 ,1 ,3 ,5 ,7 ,1 0 ,1 1 ,2 1 ,2 2 ,23 ,24 ,26 ,30 ,32 ,3 4 ,3 5 ,4 0 ,4 1 ,46 ,47 ,50 ,51 ,52 ,6 ’) 21   / / T h e r e f o r e t h e kmap i s d i s p l a y e d / /

Scilab code Exa 6.10   4 variable mapping

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

  // E xamp x ample le 6 − 10//   / /Map L=W’ ’ X ’ ’ YZ YZ+ +WX’ ’ YZ’ ’ +W +WX X’ ’ Y ’ ’ Z ’ ’ +W +W’’ ’ XYZ/ Z///   clc

  / / c l e a r s t he he c o n s o l e //   clear

  // 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 //   / / Ma Ma p p i n g t h e e x p r e s s i o n / /   disp (   ’ C ’ ’ D ’ ’ C ’ ’ D CD CD ’ ’ ’ )   disp (   ’ A ’ ’ B ’ ’ 0 0 1 0 ’)   disp (   ’ AB ’ ’ 0 0 1 0 ’)   disp (   ’ AB 0 0 0 0 ’)   disp (   ’ AB ’ ’ 1 0 0 1 ’)   disp (   ’ From t he h e map , h ig i g h o u ttp p u ttss f o r 3 , 7 , 8 , 1 0 ’ )   a =[0 0 1 1 ; 0 1 1 1 ; 1 0 0 0 ; 1 0 1 0 ]   disp ( a )   f o r   i =1 =1: 4 i f   a ( i , 1 ) = = 1   then     b ( i , 1 ) =  ’W’   else   b ( i , 1 ) =  ’W’ ’ ’   end   i f   a ( i , 2 ) = = 1   then   b ( i , 2 ) =  ’  ’X X’   else

69

 

b ( i , 2 ) =  ’X’ ’ ’

 

25 26

 

end

27   i f   a ( i , 3 ) = = 1   then   b ( i , 3 ) =  ’ 28  ’Y Y’ 29   else   b ( i , 3 ) =  ’Y’ ’ ’ 30   end 31   i f   a ( i , 4 ) = = 1   then 32 33   b ( i , 4 ) =  ’ Z ’ 34   else 35   b ( i , 4 ) =  ’ Z ’ ’ ’ end 36   37   e n d 38   disp (   ’ e v a l u uaa t i n g e x p r e s s i o n f r o m t r u t h t a b l e a n d 39 40 41 42 43 44 45

           

map ’ )( [ b ( 1 , 1 ) , b (1 ( 1 , 2 ) , b ( 1 , 3) 3) , b ( 1 , 4 ) ] ) l = strcat m = strcat ( [ b ( 2 , 1 ) , b (2 ( 2 , 2 ) , b ( 2 , 3) 3) , b ( 2 , 4 ) ] ) ( 3 , 2 ) , b ( 3 , 3) 3) , b ( 3 , 4 ) ] ) n = strcat ( [ b ( 3 , 1 ) , b (3 o = strcat ( [ b ( 4 , 1 ) , b (4 ( 4 , 2 ) , b ( 4 , 3) 3) , b ( 4 , 4 ) ] ) x = strcat ( [ l ”+” , m ”+” , n ”+” , o ] ) disp ( x )

  / / E x p r e ss ss i o n i s d i s p l a y e d / /

Scilab code Exa 6.11  Reduce an expression by Kmap

1 2 3 4 5 6 7 8 9 10

  // E xamp x ample le 6 − 11//   / / r ed e d u ce c e e x p r e s s i o n u s i ng n g k−map//   clc

  / / c l e a r s t h e window / /   clear

  // 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 // Ma p p i n g t h e e x p r e s s i o n / /   / / Ma   disp (   ’ C ’ ’ D ’ ’ C ’ ’ D CD CD ’ ’ ’ ) 1 1 1 1 ’)   disp (   ’ A ’ ’ B ’ ’   disp (   ’ AB ’ ’ 0 0 1 1 ’) 70

 

11   disp (   ’ AB 12   disp (   ’ AB ’ ’

0 0

1 0

1 0

0 ’) 0 ’)

13   disp (   ’ From t he h e map ,

h ig i g h o u ttp p u ttss f o r 0 ,1 ,2 ,3 ,6 ,7 ,13 ,15 ’ ) 14   / / g i v e n l o g i c e q u a t i o n / / 15   a = [ 0 0 0 0 ; 0 0 0 1 ; 0 0 1 0 ; 0 0 1 1 ; 0 1 1 0 ; 0 1 1 1;1 1 0 1; 1 1 1 1] 16   disp ( a ) =1: 8 17   f o r   i =1 i f   a ( i , 1 ) = = 1   then 18   19   b ( i , 1 ) =  ’  ’A A’ 20   else   b ( i , 1 ) =  ’A’ ’ ’ 21 22   end 23 24 25 26 27 28 29 30 31 32 33 34

             

i f   a ( i , 2 ) = = 1   then b ( i , 2 ) =  ’ B ’ else   b ( i , 2 ) =  ’B’ ’ ’ end i f   a ( i , 3 ) = = 1   then   b ( i , 3 ) =  ’  ’C C’ else   b ( i , 3 ) =  ’C ’ ’ ’ end i f   a ( i , 4 ) = = 1   then   b ( i , 4 ) =  ’  ’D D’  

35     else b ( i , 4 ) =  ’ D ’ ’ ’ 36   end 37 38   e n d 39   disp (   ’ e v a l u uaa t i n g e x p r e s s i o n f r o m t r u t h map ’ ) 40   x 1 = strcat ( [ b ( 1 , 1 ) , b (1 ( 1 , 2 ) , b ( 1 , 3) 3) , b ( 1 , 4 ) ( 2 , 2 ) , b ( 2 , 3) 3) , b ( 2 , 4 ) 41   x 2 = strcat ( [ b ( 2 , 1 ) , b (2 42   x 3 = strcat ( [ b ( 3 , 1 ) , b (3 ( 3 , 2 ) , b ( 3 , 3) 3) , b ( 3 , 4 ) 43   x 4 = strcat ( [ b ( 4 , 1 ) , b (4 ( 4 , 2 ) , b ( 4 , 3) 3) , b ( 4 , 4 ) ( 5 , 2 ) , b ( 5 , 3) 3) , b ( 5 , 4 ) 44   x 5 = strcat ( [ b ( 5 , 1 ) , b (5 45   x 6 = strcat ( [ b ( 6 , 1 ) , b (6 ( 6 , 2 ) , b ( 6 , 3) 3) , b ( 6 , 4 )

71

t a b l e and ]) ]) ]) ]) ]) ])

 

( 7 , 2 ) , b ( 7 , 3) 3) , b ( 7 , 4 ) ] ) 46   x 7 = strcat ( [ b ( 7 , 1 ) , b (7 ( 8 , 2 ) , b ( 8 , 3) 3) , b ( 8 , 4 ) ] ) 47   x 8 = strcat ( [ b ( 8 , 1 ) , b (8 48 49 50 51

  x = ( [ x 1 ”+” , x 2 ”+” , x 3 ”+” , x 4 ”+” , x 5 ”+” , x 6 ”+” , x 7 ”+” , x8 x8 ] )   disp ( x )

  / / E x p r e ss ss i o n i s d i s p l a y e d / /   disp (   ’ n noow r e d u ucc i n g e x p r e s s i o n u s i n ngg b o ooll ea ea n a l g e b r a ’

) 52   disp (  ’A ’ ’ B ’ ’+ ’+A A ’ ’ C+ABD ’ )

Scilab code Exa 6.12  Reduce an expression using Kmap

1 2 3 4 5 6 7 8 9 10 11 12 13

  // E xamp x ample le 6 − 12//   / / r ed e d u ce c e e x p r e s s i o n u s i ng n g k−map//   clc

  / / c l e a r s t h e window / /   clear

  // 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 //   / / Ma Ma p p i n g t h e e x p r e s s i o n / /   disp (   ’ C ’ ’ D ’ ’ C ’ ’ D CD CD ’ ’ ’ )   disp (   ’ A ’ ’ B ’ ’ 0 0 1 1 ’)   disp (   ’ AB ’ ’ 0 1 1 0 ’)   disp (   ’ AB 1 1 1 1 ’)   disp (   ’ AB ’ ’ 0 1 1 0 ’)   disp (   ’ From t he h e map , h ig i g h o u ttp p u ttss f o r 2 ,3 ,5 ,7 ,9 ,11 ,12 ,13 ,14 ,15 ’ ) 14   / / g i v e n l o g i c e q u a t i o n / / 15   a = [ 0 0 1 0 ; 0 0 1 1 ; 0 1 0 1 ; 0 1 1 1 ; 1 0 0 1 ; 1 0 1 1;1 1 0 0;1 1 0 1;1 1 1 0;1 1 1 1] 16   disp ( a ) 17   f o r   i = 1: 1: 1 0 18   i f   a ( i , 1 ) = = 1   then 19   b ( i , 1 ) =  ’  ’A A’ 20   else 21   b ( i , 1 ) =  ’A’ ’ ’   end 22

72

 

 

23 24 25 26 27 28 29 30 31 32 33 34 35 36

 

i f   a ( i , 2 ) = = 1   then   b ( i , 2 ) =  ’ B ’  

else

b ( i , 2 ) =  ’B’ ’ ’

 

end   i f   a ( i , 3 ) = = 1   then   b ( i , 3 ) =  ’  ’C C’   else   b ( i , 3 ) =  ’C ’ ’ ’   end   i f   a ( i , 4 ) = = 1   then   b ( i , 4 ) =  ’  ’D D’   else   b ( i , 4 ) =  ’ D ’ ’ ’

37   end 38   e n d 39   disp (   ’ e v a l u uaa t i n g e x p r e s s i o n f r o m t r u t h map ’ ) ( 1 , 2 ) , b ( 1 , 3) 3) , b ( 1 , 4 ) 40   x 1 = strcat ( [ b ( 1 , 1 ) , b (1 ( 2 , 2 ) , b ( 2 , 3) 3) , b ( 2 , 4 ) 41   x 2 = strcat ( [ b ( 2 , 1 ) , b (2 42   x 3 = strcat ( [ b ( 3 , 1 ) , b (3 ( 3 , 2 ) , b ( 3 , 3) 3) , b ( 3 , 4 ) 43   x 4 = strcat ( [ b ( 4 , 1 ) , b (4 ( 4 , 2 ) , b ( 4 , 3) 3) , b ( 4 , 4 ) ( 5 , 2 ) , b ( 5 , 3) 3) , b ( 5 , 4 ) 44   x 5 = strcat ( [ b ( 5 , 1 ) , b (5 ( 6 , 2 ) , b ( 6 , 3) 3) , b ( 6 , 4 ) 45   x 6 = strcat ( [ b ( 6 , 1 ) , b (6 46   x 7 = strcat ( [ b ( 7 , 1 ) , b (7 ( 7 , 2 ) , b ( 7 , 3) 3) , b ( 7 , 4 ) ( 8 , 2 ) , b ( 8 , 3) 3) , b ( 8 , 4 ) 47   x 8 = strcat ( [ b ( 8 , 1 ) , b (8

t a b l e and ]) ]) ]) ]) ]) ]) ]) ])

48 strcat [ [b b (( 91 ,0 1, ) 1) , )b ,(9 (b 9( ,1 20 ), ,2 b) (, 9b ,( 3) 3)10 ,, b3 () 9, ,b 4( )1] == x9 10 strcat( ( 1 10 0) , 4) 4) ] ) 49    x 50   x = ( [ x 1 ”+” , x 2 ”+” , x 3 ”+” , x 4 ”+” , x 5 ”+” , x 6 ”+” , x 7 ”+” , x 8 ”+” , x 9 ”+” , x1 x1 0 ] ) 51   disp ( x ) 52   / / E x p r e ss ss i o n i s d i s p l a y e d / / 53   disp (   ’ n noow r e d u ucc i n g e x p r e s s i o n u s i n ngg b o ooll ea ea n a l g e b r a ’ ) 54   disp (  ’A ’ ’ B ’ ’ C+BD BD+ +AB AB+ +AD ’ )

73

 

Scilab code Exa 6.13   Reduce expression by kmap

1 2 3 4 5 6 7 8 9

  // E xamp x ample le 6 − 13//   / / r ed e d u ce c e e x p r e s s i o n u s i ng n g k−map//   clc

  / / c l e a r s t he he c o n s o l e //   clear

  // 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 //   / / Ma Ma p p i n g t h e e x p r e s s i o n / /   disp (   ’ B ’ ’ C ’ ’ B ’ ’ C BC BC ’ ’ ’ )   disp (   ’ A ’ ’ 1 0 1 1 ’) 10   disp (   ’ A 1 1 1 0 ’) gh h outp pu u t s f o r 0 , 2 , 3 , 4 , 5 and 11   disp (   ’ From t h e map , h i g 7 ’) 12   a = [ 0 0 0 ; 0 1 0 ; 0 1 1 ; 1 0 0 ; 1 0 1 ; 1 1 1 ] 13   disp ( a ) =1: 6 14   f o r   i =1 15   i f   a ( i , 1 ) = = 1   then   b ( i , 1 ) =  ’ 16  ’A A’ 17   else   b ( i , 1 ) =  ’A’ ’ ’ 18   end 19   i f   a ( i , 2 ) = = 1   then 20 21   b ( i , 2 ) =  ’ B ’ 22   else 23   b ( i , 2 ) =  ’B’   end 24   if   a ( i , 3 ) = = 1 25 26   b ( i , 3 ) =  ’  ’C C’ 27   else 28   b ( i , 3 ) =  ’C ’   end 29 30   e n d 31   disp (   ’ e v a l u uaa t i n g

’’   then

’’

e x p r e s s i o n f r o m t r u t h t a b l e and 74

 

map ’ ) ( 1 , 2 ) , b ( 1 , 3) 3) ] ) 32   x 1 = strcat ( [ b ( 1 , 1 ) , b (1 33 34 35 36 37 38 39 40 41 42

             

( 2 , 2 ) , b ( 2 , 3) 3) ] ) x 2 = strcat ( [ b ( 2 , 1 ) , b (2 x 3 = strcat ( [ b ( 3 , 1 ) , b (3 ( 3 , 2 ) , b ( 3 , 3) 3) ] ) ( 4 , 2 ) , b ( 4 , 3) 3) ] ) x 4 = strcat ( [ b ( 4 , 1 ) , b (4 x 5 = strcat ( [ b ( 5 , 1 ) , b (5 ( 5 , 2 ) , b ( 5 , 3) 3) ] ) ( 6 , 2 ) , b ( 6 , 3) 3) ] ) x 6 = strcat ( [ b ( 6 , 1 ) , b (6 x = ( [ x 1 ”+” , x 2 ”+” , x 3 ”+” , x 4 ”+” , x 5 ”+” , x 6 ] ) disp ( x )

  / / E x p r e ss ss i o n i s d i s p l a y e d / /   disp (   ’ a p p lly yin ngg l aaw w s o f b o ooll e aan n algebra ’)   disp (  ’A  ’AC C+A ’ ’ B+B ’ ’ C ’ ’ ’ )

Scilab code Exa 6.14.a   Inputs Required

1 2 3 4 5 6 7 8 9 10 11 12 13

  //examp pll e 6 − 14a//   / //N Number o f i n p u t s r e q u i r e d / /   clc

  / / c l e a r s t h e window / /   clear

  // 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 //   disp (   ’ W=AB ’ ’’D D+ACD ’ ’’+ +EF ’ )   disp (   ’ c ount AND in pu t s ’ )   disp (  ’3+3+2=8 ’ )   disp (   ’ c o u n t OR i n p u t s ’ )   disp (  ’1+1+1=3 ’ )   disp (   ’ t h e r e f o r e t o t a l i n p u t s = =111 ’ )   / / r e s u l t i s d i s p la la y e d //

Scilab code Exa 6.14.b   Inputs required

1   //examp pll e 6 − 14b// 2   / /N / Number o f i n p u t s

requ ired // 75

 

3   clc 4   // cl ea rs 5 6 7 8 9 10 11 12 13

t h e window / /

  clear

  // 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 //   disp (   ’ X=LM+N ’ ’PQ ’PQ+ +LM ’ ’PQ ’ ’ ’ )   disp (   ’ c ount AND in pu t s ’ )   disp (  ’2+3+4=9 ’ )   disp (   ’ c o u n t OR i n p u t s ’ )   disp (  ’1+1+1=3 ’ )   disp (   ’ t h e r e f o r e t o t a l i n p u t s = =112 ’ )   / / r e s u l t i s d i s p la la y e d //

Scilab code Exa 6.14.c   Inputs required

1 2 3 4 5 6 7 8 9 10

p l e 6 −1 4 c / /   / / e x a m pl   / //N Number o f i n p u t s r e q u i r e d / /   clc

  / / c l e a r s t h e window / /   clear

  // 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 //   disp (  ’ Y=ST ’ ’’U U ’ ’’V V+ST STO O ’ ’’V V+UV ’ ’+ ’+S SUV UV+ +TU ’ ’V ’V ’ ’ ’ )   disp (   ’ c ount AND in pu t s ’ )   disp (  ’ 4+4+2+ 4+4+2+3+3=1 3+3=166 ’ )   disp (   ’ c o u n t OR i n p u t s ’ ) 11   disp (  ’1+1+1+1+1=5 ’ ) 12   disp (   ’ t h e r e f o r e t o t a l i n p u t s = =221 ’ ) 13   / / r e s u l t i s d i s p la la y e d //

Scilab code Exa 6.14.d   Inputs required

1   //examp pll e 6 − 14d// 2   / /N / Number o f i n p u t s 3   clc

requ ired //

76

 

4   // cl ea rs 5   clear 6 7 8 9 10 11 12 13

t h e window / /

  // 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 //   disp (   ’ (A (A+ +B ’ ’+ ’+C) C) (A ’ ’+ ’+D) D) (B (B+ +D ’ ’ ) ’ )   disp (   ’ c o u n t OR i n p u t s ’ )   disp (  ’3+2+2=7 ’ )   disp (   ’ c ount AND in pu t s ’ )   disp (  ’1+1+1=3 ’ )   disp (   ’ t h e r e f o r e t o t a l i n p u t s = =110 ’ )   / / r e s u l t i s d i s p la la y e d //

Scilab code Exa 6.15   Minimise expression using mapping

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

  // E xamp x ample le 6 − 15//   / / M ap ap pi pi ng ng a n e q u a t i o n / /   clc

  / / c l e a r s t he he c o n s o l e //   clear

  // 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 //   / / Ma Ma p p i n g t h e e x p r e s s i o n / /   disp (  ’ Z= Z=m m (2 ,3 ,6 ,7 ,9 ,10 ,12 ,1 3 ,1 4 ,1 6 ,1 8 ,2 2 ,2 6 ,2 8 ,2 9 ,3 0) ’ ) A’ ’   disp (   ’ A ’)   disp (   ’ D ’ ’ E ’ ’ D ’ ’ E DE DE ’ ’ D ’ ’E ’ ’ D ’ ’E DE DE’ ’ ’ ) 0 0 1 1 1   disp (   ’ B ’ ’ C ’ ’ 0 0 1 ’) 0 0 1 1 0   disp (   ’ BC ’ ’ 0 0 1 ’)   disp (   ’ BC 1 1 0 1 1 1 0 1 ’) 0 1 0 1 0   disp (   ’ BC ’ ’ 0 0 1 ’)   disp (   ’ From t he h e map , h ig i g h o u ttp p u ttss f o r 77

 

2 ,3 ,6 ,7 ,9 ,10 ,12 ,13 ,14 ,16 ,18 ,22 ,26 ,28 ,29 ,30 16   / / T h e r e f o r e t h e kmap i s d i s p l a y e d / /

’)

17   a = [ 0 0 0 1 0 ; 0 0 0 1 1 ; 0 0 1 1 0 ; 0 0 1 1 1 ; 0 1 0 0 1;0 1 0 1 0;0 1 1 0 0;0 1 1 0 1;0 1 1 1 0;1 0 0 0 0;1 0 0 1 0;1 0 1 1 0;1 1 0 1 0;1 1 1 0 0;1 1 1 0 1;1 1 1 1 0] 18   disp ( a ) 1: 1 6 19   f o r   i = 1: i f   a ( i , 1 ) = = 1   then 20   21   b ( i , 1 ) =  ’  ’A A’ 22   else 23   b ( i , 1 ) =  ’A’ ’ ’   end 24 25   i f   a ( i , 2 ) = = 1   then 26 27 28 29 30 31 32 33 34 35 36 37

 

 

 

else

b ( i , 2 ) =  ’ B ’ b ( i , 2 ) =  ’B’ ’ ’

 

end   i f   a ( i , 3 ) = = 1   then   b ( i , 3 ) =  ’  ’C C’   else   b ( i , 3 ) =  ’C ’ ’ ’   end   i f   a ( i , 4 ) = = 1   then   b ( i , 4 ) =  ’  ’D D’   else

38     e n d b ( i , 4 ) =  ’ D ’ ’ ’ 39 40   i f   a ( i , 5 ) = = 1   then   b ( i , 5 ) =  ’ E ’ 41 42   else 43   b ( i , 5 ) = ’ E ’ ’ ’ 44   end 45   e n d 46   disp (   ’ e v a l u uaa t i n g e x p r e s s i o n f r o m t r u t h t a b l e a n d map ’ ) ( 1 , 2 ) , b ( 1 , 3) 3) , b ( 1 , 4 ) , b ( 1 , 5) 5) ] ) 47   x 1 = strcat ( [ b ( 1 , 1 ) , b (1 48   x 2 = strcat ( [ b ( 2 , 1 ) , b (2 ( 2 , 2 ) , b ( 2 , 3) 3) , b ( 2 , 4 ) , b ( 2 , 5) 5) ] )

78

 

49   x 3 = strcat ( [ b ( 3 , 1 ) , b (3 ( 3 , 2 ) , b ( 3 , 3) 3) , b ( 3 , 4 ) , b ( 3 , 5) 5) ] ) ( 4 , 2 ) , b ( 4 , 3) 3) , b ( 4 , 4 ) , b ( 4 , 5) 5) ] ) 50   x 4 = strcat ( [ b ( 4 , 1 ) , b (4 51 52 53 54 55 56

           

x 5 = strcat ( [ b ( 5 , 1 ) , b (5 ( 5 , 2 ) , b ( 5 , 3) 3) , b ( 5 , 4 ) , b ( 5 , 5) 5) ] ) ( 6 , 2 ) , b ( 6 , 3) 3) , b ( 6 , 4 ) , b ( 6 , 5) 5) ] ) x 6 = strcat ( [ b ( 6 , 1 ) , b (6 ( 7 , 2 ) , b ( 7 , 3) 3) , b ( 7 , 4 ) , b ( 7 , 5) 5) ] ) x 7 = strcat ( [ b ( 7 , 1 ) , b (7 x 8 = strcat ( [ b ( 8 , 1 ) , b (8 ( 8 , 2 ) , b ( 8 , 3) 3) , b ( 8 , 4 ) , b ( 8 , 5) 5) ] ) ( 9 , 2 ) , b ( 9 , 3) 3) , b ( 9 , 4 ) , b ( 9 , 5) 5) ] ) x 9 = strcat ( [ b ( 9 , 1 ) , b (9 x 1 0 = strcat ( [ b ( 1 0 , 1) 1 ) , b ( 1 0 , 2 ) , b ( 10 1 0 , 3 ) , b ( 1 0 , 4) 4) , b ( 1 0 , 5 ) ]) 1 ) , b ( 1 1 , 2 ) , b ( 11 1 1 , 3 ) , b ( 1 1 , 4) 4) , b ( 1 1 , 5 ) 57   x 1 1 = strcat ( [ b ( 1 1 , 1) ]) 58   x 1 2 = strcat ( [ b ( 1 2 , 1) 1 ) , b ( 1 2 , 2 ) , b ( 12 1 2 , 3 ) , b ( 1 2 , 4) 4) , b ( 1 2 , 5 ) ]) 59   x 1 3 = strcat ( [ b ( 1 3 , 1) 1 ) , b ( 1 3 , 2 ) , b ( 13 1 3 , 3 ) , b ( 1 3 , 4) 4) , b ( 1 3 , 5 ) ]) 1 ) , b ( 1 4 , 2 ) , b ( 14 1 4 , 3 ) , b ( 1 4 , 4) 4) , b ( 1 4 , 5 ) 60   x 1 4 = strcat ( [ b ( 1 4 , 1) ]) 1 ) , b ( 1 5 , 2 ) , b ( 15 1 5 , 3 ) , b ( 1 5 , 4) 4) , b ( 1 5 , 5 ) 61   x 1 5 = strcat ( [ b ( 1 5 , 1) ]) 62   x 1 6 = strcat ( [ b ( 1 6 , 1) 1 ) , b ( 1 6 , 2 ) , b ( 16 1 6 , 3 ) , b ( 1 6 , 4) 4) , b ( 1 6 , 5 ) ]) 63   x = ( [ x 1 ”+” , x 2 ”+” , x 3 ”+” , x 4 ”+” , x 5 ”+” , x 6 ”+” , x 7 ”+” , x 8 ”+” , x 9 ”+” ,x10 ”+” ,x11 ”+” ,x12 ”+” ,x13 ”+” ,x14 ”+” ,x15 ”+” , x 16 16 ] ) 64   disp ( x ) du u ccee d e x p r e s s i o n ’ ) 65   disp (   ’ R e d 66 disp ’BD Cl’a’yEe’d’+ ’+A ’D+ +BCD ’ ’+ ’+D DE ’ ’ ’ ) 67    / / E x(p ’A r e’ss s ’B s i oDn’ ’ Ei + s AB d i’ s’ p /A / ’ ’ B ’ ’D

Scilab code Exa 6.16   Reduce using mapping

1 2 3 4

  // E xamp x ample le 6 − 16//   / / R e du d u ce c e u s i n g m ap ap pi p i ng ng / /   clc

  / / c l e a r s t he he c o n s o l e // 79

 

5   clear 6   // c l e a r s

all

e x i s t i n g v a r i a b l e s //

7   / / Ma Ma p p i n g t h e e x p r e s s i o n / /  ’ Y=m 8   disp (  ’Y=

(0 ,2 ,4 ,8 ,1 0 ,1 3 ,1 5 ,1 6 ,1 8 ,2 0 ,2 3 ,2 4 ,2 6 ,3 2 ,3 4 ,4 0 ,4 2 ,4 5 ,4 7 ,4 8 ,5 0 ,5 6 ,5 7 ’) B’ ’ 9   disp (   ’ B ’) 10   disp (   ’ E ’ ’ F ’ ’ E ’ ’ F EF EF ’ ’ E ’ ’F ’ ’ E ’ ’F EF EF ’ ’ ’ ) 11   disp (   ’ C ’ ’D ’ ’ 1 0 0 1 1 0 0 1 ’) 12   disp (   ’ A ’ ’ CD ’ ’ 1 0 0 0 1 0 1 0 ’) 13   disp (   ’

0

14   disp (   ’

0

CD 0 0 CD ’ ’ 0 1

0 ’) 1 ’)

1

1

0

0

0

0

1

1

1

0

0

1

1

0

0

0

0

1

1

0

1

15   disp (  ’

’) 16   disp (   ’

0 17   disp (   ’ A 0 18   disp (   ’

C ’ ’D ’ ’ 0 1 CD ’ ’ 0 0 CD

’) 0 ’) 0

’ )1 C0D ’ ’0 0 0 1 1 1 0 1 ’) h e map , h ig i g h o u ttp p u ttss f o r 20   disp (   ’ From t he 0 ,2 ,4 ,8 ,10 ,13 ,15 ,16 ,18 ,2 0 ,2 3 ,2 4 ,2 6 ,3 2 ,34 ,40 ,42 ,45 ,47 ,48 ,5 0 ,56 ,57 , ’) 21   / / T h e r e f o r e t h e kmap i s d i s p l a y e d / / 19   disp (   ’ 1

22   a = [ 0 0 0 0 0 0 ; 0 0 0 0 1 0 ; 0 0 0 1 0 0 ; 0 0 1 0 0 0 ; 0 0 1 0 1 0;0 0 1 1 0 1;0 0 1 1 1 1;0 1 0 0 0 0;0 1 0 0 1 0;0 1 0 1 0 0;0 1 0 1 1 1;0 1 1 0 0 0;0 1 1 0 1 0;1 0 0 0 0 0;1 0 0 0 1 0;1 0 1 0 0 0;1 0 1 0 1 0;1 0 1 1 0 1;1 0 1 1 1 1;1 1 0 0 0 0;1 1 0

80

 

0 1 0;1 1 1 0 0 0;1 1 1 0 0 1;1 1 1 0 1 0;1 1 1 1 0 0; 0; 1 1 1 1 0 1 ] 23   f o r   i = 1: 1: 2 6 i f   a ( i , 1 ) = = 1   then   24 25   b ( i , 1 ) =  ’  ’A A’ 26   else 27   b ( i , 1 ) =  ’A’ ’ ’   end 28 29   i f   a ( i , 2 ) = = 1   then 30   b ( i , 2 ) =  ’ B ’ 31   else 32   b ( i , 2 ) =  ’B’ ’ ’   end 33 34   i f   a ( i , 3 ) = = 1   then 35 36 37 38 39 40 41 42 43 44 45 46

 

 

 

else

b ( i , 3 ) =  ’  ’C C’ b ( i , 3 ) =  ’C ’ ’ ’

   

end i f   a ( i , 4 ) = = 1   then   b ( i , 4 ) =  ’  ’D D’   else   b ( i , 4 ) =  ’ D ’ ’ ’   end   i f   a ( i , 5 ) = = 1   then   b ( i , 5 ) =  ’ E ’   else

47     e n d b ( i , 5 ) = ’ E ’ ’ ’ 48 49   i f   a ( i , 6 ) = = 1   then   b ( i , 6 ) =  ’ F ’ 50 51   else 52   b ( i , 6 ) =  ’ F ’ ’ ’ 53   end 54   e n d 55   disp (   ’ e v a l u uaa t i n g e x p r e s s i o n f r o m t r u t h t a b l e a n d map ’ ) ( 1 , 2 ) , b ( 1 , 3) 3) , b ( 1 , 4 ) , b ( 1 , 5) 5) , b 56   x 1 = strcat ( [ b ( 1 , 1 ) , b (1 ( 1 ,6 ,6 ) ] )

81

 

( 2 , 2 ) , b ( 2 , 3) 3) , b ( 2 , 4 ) , b ( 2 , 5) 5) , b 57   x 2 = strcat ( [ b ( 2 , 1 ) , b (2 ( 2 ,6 ,6 ) ] ) 58   x 3 = strcat ( [ ( 3 ,6 ,6 ) ] ) 59   x 4 = strcat ( [ ( 4 ,6 ,6 ) ] ) 60   x 5 = strcat ( [ ( 5 ,6 ,6 ) ] ) 61   x 6 = strcat ( [ ( 6 ,6 ,6 ) ] ) 62   x 7 = strcat ( [ ( 7 ,6 ,6 ) ] ) 63   x 8 = strcat ( [ ( 8 ,6 ,6 ) ] )

b ( 3 , 1 ) , b (3 ( 3 , 2 ) , b ( 3 , 3) 3) , b ( 3 , 4 ) , b ( 3 , 5) 5) , b b ( 4 , 1 ) , b (4 ( 4 , 2 ) , b ( 4 , 3) 3) , b ( 4 , 4 ) , b ( 4 , 5) 5) , b b ( 5 , 1 ) , b (5 ( 5 , 2 ) , b ( 5 , 3) 3) , b ( 5 , 4 ) , b ( 5 , 5) 5) , b b ( 6 , 1 ) , b (6 ( 6 , 2 ) , b ( 6 , 3) 3) , b ( 6 , 4 ) , b ( 6 , 5) 5) , b b ( 7 , 1 ) , b (7 ( 7 , 2 ) , b ( 7 , 3) 3) , b ( 7 , 4 ) , b ( 7 , 5) 5) , b b ( 8 , 1 ) , b (8 ( 8 , 2 ) , b ( 8 , 3) 3) , b ( 8 , 4 ) , b ( 8 , 5) 5) , b

64   x 9 = strcat ( [ b ( 9 , 1 ) , b (9 ( 9 , 2 ) , b ( 9 , 3) 3) , b ( 9 , 4 ) , b ( 9 , 5) 5) , b ( 9 ,6 ,6 ) ] ) 1 ) , b ( 1 0 , 2 ) , b ( 10 1 0 , 3 ) , b ( 1 0 , 4) 4) , b ( 1 0 , 5 ) 65   x 1 0 = strcat ( [ b ( 1 0 , 1) , b ( 10 10 , 6 ) ] ) 1 ) , b ( 1 1 , 2 ) , b ( 11 1 1 , 3 ) , b ( 1 1 , 4) 4) , b ( 1 1 , 5 ) 66   x 1 1 = strcat ( [ b ( 1 1 , 1) , b ( 11 11 , 6 ) ] ) 1 ) , b ( 1 2 , 2 ) , b ( 12 1 2 , 3 ) , b ( 1 2 , 4) 4) , b ( 1 2 , 5 ) 67   x 1 2 = strcat ( [ b ( 1 2 , 1) , b ( 12 12 , 6 ) ] ) 68   x 1 3 = strcat ( [ b ( 1 3 , 1) 1 ) , b ( 1 3 , 2 ) , b ( 13 1 3 , 3 ) , b ( 1 3 , 4) 4) , b ( 1 3 , 5 ) , b ( 13 13 , 6 ) ] ) 69   x 1 4 = strcat ( [ b ( 1 4 , 1) 1 ) , b ( 1 4 , 2 ) , b ( 14 1 4 , 3 ) , b ( 1 4 , 4) 4) , b ( 1 4 , 5 ) , b ( 14 14 , 6 ) ] ) 70   x 1 5 = strcat ( [ b ( 1 5 , 1) 1 ) , b ( 1 5 , 2 ) , b ( 15 1 5 , 3 ) , b ( 1 5 , 4) 4) , b ( 1 5 , 5 ) , b ( 15 15 , 6 ) ] )

1 ) , b ( 1 6 , 2 ) , b ( 16 1 6 , 3 ) , b ( 1 6 , 4) 4) , b ( 1 6 , 5 ) 71   x 1 6 = strcat ( [ b ( 1 6 , 1) , b ( 16 16 , 6 ) ] ) 72   x 1 7 = strcat ( [ b ( 1 7 , 1) 1 ) , b ( 1 7 , 2 ) , b ( 17 1 7 , 3 ) , b ( 1 7 , 4) 4) , b ( 1 7 , 5 ) , b ( 17 17 , 6 ) ] ) 73   x 1 8 = strcat ( [ b ( 1 8 , 1) 1 ) , b ( 1 8 , 2 ) , b ( 18 1 8 , 3 ) , b ( 1 8 , 4) 4) , b ( 1 8 , 5 ) , b ( 18 18 , 6 ) ] ) 74   x 1 9 = strcat ( [ b ( 1 9 , 1) 1 ) , b ( 1 9 , 2 ) , b ( 19 1 9 , 3 ) , b ( 1 9 , 4) 4) , b ( 1 9 , 5 ) , b ( 19 19 , 6 ) ] ) 1 ) , b ( 2 0 , 2 ) , b ( 20 2 0 , 3 ) , b ( 2 0 , 4) 4) , b ( 2 0 , 5 ) 75   x 2 0 = strcat ( [ b ( 2 0 , 1) , b ( 20 20 , 6 ) ] )

82

 

76   x 2 1 = strcat ( [ b ( 2 1 , 1) 1 ) , b ( 2 1 , 2 ) , b ( 21 2 1 , 3 ) , b ( 2 1 , 4) 4) , b ( 2 1 , 5 ) , b ( 21 21 , 6 ) ] ) 77   x 2 2 = strcat ( [ b ( 2 2 , 1) 1 ) , b ( 2 2 , 2 ) , b ( 22 2 2 , 3 ) , b ( 2 2 , 4) 4) , b ( 2 2 , 5 ) , b ( 22 22 , 6 ) ] ) 1 ) , b ( 2 3 , 2 ) , b ( 23 2 3 , 3 ) , b ( 2 3 , 4) 4) , b ( 2 3 , 5 ) 78   x 2 3 = strcat ( [ b ( 2 3 , 1) , b ( 23 23 , 6 ) ] ) 1 ) , b ( 2 4 , 2 ) , b ( 24 2 4 , 3 ) , b ( 2 4 , 4) 4) , b ( 2 4 , 5 ) 79   x 2 4 = strcat ( [ b ( 2 4 , 1) , b ( 24 24 , 6 ) ] ) 80   x 2 5 = strcat ( [ b ( 2 5 , 1) 1 ) , b ( 2 5 , 2 ) , b ( 25 2 5 , 3 ) , b ( 2 5 , 4) 4) , b ( 2 5 , 5 ) , b ( 25 25 , 6 ) ] ) 81   x 2 6 = strcat ( [ b ( 2 6 , 1) 1 ) , b ( 2 6 , 2 ) , b ( 26 2 6 , 3 ) , b ( 2 6 , 4) 4) , b ( 2 6 , 5 ) , b ( 26 26 , 6 ) ] ) 82   x = ( [ x 1 ”+” , x 2 ”+” , x 3 ”+” , x 4 ”+” , x 5 ”+” , x 6 ”+” , x 7 ”+” , x 8 ”+” , x 9 ”+” ,x10 ”+” ,x11 ”+” ,x12 ”+” ,x13 ”+” ,x14 ”+” ,x15 ”+” ,

x 1 6 ”+” ,x17 ”+” ,x18 ”+” ,x19 ”+” ,x20 ”+” ,x21 ”+” ,x22 ”+” , x 2 3 ”+” ,x24 ”+” ,x25 ”+” , x2 x2 6 ] ) 83   disp ( x ) du u ccee d e x p r e s s i o n ’ ) 84   disp (   ’ R e d 85   disp (  ’A ’ ’’B BC ’ ’ DEF+A ’ ’ C ’ ’ E ’ ’ F ’ ’+ ’+B B ’ ’C ’ CDF+ABCE ’ ’+ ’+D D’ ’F ’ ’ ’) ss i o n i s d i s p l a y e d / / 86   / / E x p r e ss

Scilab code Exa 6.17  Kmap POS SOP

1 x ample le 6 − 17// 2   // E xamp 3   / / r ed e d u ce ce e x p r e s s i o n

u s i ng n g k−map by both POS and SOP

// 4 5 6 7 8 9 10 11

  clc

  / / c l e a r s t h e window / /   clear

  // 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 // Ma p p i n g t h e e x p r e s s i o n / /   / / Ma   disp (   ’ C ’ ’ D ’ ’ C ’ ’ D CD CD ’ ’ ’ ) 1 1 1 0 ’)   disp (   ’ A ’ ’ B ’ ’   disp (   ’ AB ’ ’ 0 0 1 1 ’) 83

 

12   disp (   ’ AB 13   disp (   ’ AB ’ ’

0 1

1 1

1 0

0 ’) 0 ’)

14   disp (   ’ From t he h e map ,

h ig i g h o u ttp p u ttss f o r 0 ,1 ,3 ,6 ,7 ,8 ,9 ,13 ,15 ’ ) maap , l oow w outp pu uts for 15   disp (   ’ From t h e m 2 ,4 ,5 ,10 ,11 ,12 ,14 ’ ) 16   / / g i v e n l o g i c e q u a t i o n / / 17   a = [ 0 0 0 0 ; 0 0 0 1 ; 0 0 1 1 ; 0 1 1 0 ; 0 1 1 1 ; 1 0 0 0 ;1 0 0 1;1 1 0 1;1 1 1 1] 18   disp ( a ) 19   c =[ =[ 0 0 1 0 ; 0 1 0 0 ; 0 1 0 1 ; 1 0 1 0 ; 1 0 1 1 ; 1 1 0 0 ; 1 1 1 0] 20   disp ( c ) 21   f o r   i =1 =1: 9 22 23 24 25 26 27 28 29 30 31 32 33

 

i f   a ( i , 1 ) = = 1   then b ( i , 1 ) =  ’  ’A A’ else   b ( i , 1 ) =  ’A’ ’ ’ end i f   a ( i , 2 ) = = 1   then   b ( i , 2 ) =  ’ B ’ else   b ( i , 2 ) =  ’B’ ’ ’ end i f   a ( i , 3 ) = = 1   then   b ( i , 3 ) =  ’  ’C C’  

           

34   else   b ( i , 3 ) =  ’C ’ ’ ’ 35   end 36   i f   a ( i , 4 ) = = 1   then 37 38   b ( i , 4 ) =  ’  ’D D’ 39   else 40   b ( i , 4 ) =  ’ D ’ ’ ’   end 41 42   e n d 43   f o r   i =1 =1 : 7 i f   c ( i , 1 ) = = 1   then   44 45   d ( i , 1 ) =  ’A’ ’ ’

84

 

 

46 47

else  

48 49 50 51 52 53 54 55 56 57 58 59

               

d ( i , 1 ) =  ’  ’A A’

end i f   c ( i , 2 ) = = 1   then   d ( i , 2 ) =  ’B’ ’ ’ else   d ( i , 2 ) =  ’ B ’ end i f   c ( i , 3 ) = = 1   then   d ( i , 3 ) =  ’C ’ ’ ’ else   d ( i , 3 ) =  ’  ’C C’ end i f   c ( i , 4 ) = = 1   then

60   d ( i , 4 ) =  ’D’ ’ ’ 61   else   d ( i , 4 ) =  ’ D ’ 62   end 63 64   e n d 65   disp (   ’ e v a l u a t i n g e x p r e s s i o n ( m iin n ttee rm r m s ) f ro ro m t r u t h t a b l e and m maap ’ ) 66   x 1 = strcat ( [ b ( 1 , 1 ) , b (1 ( 1 , 2 ) , b ( 1 , 3) 3) , b ( 1 , 4 ) ] ) ( 2 , 2 ) , b ( 2 , 3) 3) , b ( 2 , 4 ) ] ) 67   x 2 = strcat ( [ b ( 2 , 1 ) , b (2 ( 3 , 2 ) , b ( 3 , 3) 3) , b ( 3 , 4 ) ] ) 68   x 3 = strcat ( [ b ( 3 , 1 ) , b (3 69   x 4 = strcat ( [ b ( 4 , 1 ) , b (4 ( 4 , 2 ) , b ( 4 , 3) 3) , b ( 4 , 4 ) ] ) ( 5 , 2 ) , b ( 5 , 3) 3) , b ( 5 , 4 ) ] ) 70   x 5 = strcat ( [ b ( 5 , 1 ) , b (5 71 72 73 74 75

         

x 6 = strcat ( [ b ( 6 , 1 ) , b (6 ( 6 , 2 ) , b ( 6 , 3) 3) , b ( 6 , 4 ) ] ) x 7 = strcat ( [ b ( 7 , 1 ) , b (7 ( 7 , 2 ) , b ( 7 , 3) 3) , b ( 7 , 4 ) ] ) ( 8 , 2 ) , b ( 8 , 3) 3) , b ( 8 , 4 ) ] ) x 8 = strcat ( [ b ( 8 , 1 ) , b (8 x 9 = strcat ( [ b ( 9 , 1 ) , b (9 ( 9 , 2 ) , b ( 9 , 3) 3) , b ( 9 , 4 ) ] ) x = ( [ x 1 ”+” , x 2 ”+” , x 3 ”+” , x 4 ”+” , x 5 ”+” , x 6 ”+” , x 7 ”+” , x 8 ”+” , x9 ]) 76   disp ( x ) 77   disp (   ’ e v a l u a t i n g e x p r e s s i o n ( m a ax x t e r m s ) f ro ro m t r u t h t a b l e and m maap ’ ) 78   disp (  ’ (A (A+ +B+C ’ ’’+ +D) . ( A+B ’ ’’+ +C+D) . ( A+B ’ ’+ ’ +C+D ’ ’ ) . ( A ’ ’+ ’ +B+

C ’ ’+ ’+D) D) . ( A ’ ’+ ’+B B+C ’ ’+ ’+D D ’ ’ ) . ( A ’ ’+ ’+B B ’ ’+ ’+C C+D) . ( A ’ ’+ ’+B B ’ ’+ ’+C C’ ’ +D) ’ ) 85

 

ss i o n i s d i s p l a y e d / / 79   / / E x p r e ss 80   disp (   ’ n noow r e d u ucc i n g e x p r e s s i o n

usin ngg b o ooll ea ea n a l g e b r a ’

)  ’SOP− −   B ’ ’ C ’ ’ +A ’ ’ B ’ ’ D+A ’ ’ BC BC+ABD i e 15 81   disp (  ’SOP inputs ’) 82   disp (  ’POS  ’POS− −   (B+ (B+C ’ ’+ ’+D) D) (A ’ ’+B ’+B ’ ’+D) ’+D) (A (A+ +B ’ ’+ ’+C) C) (A ’ ’+B ’+B+C ’ ’ ) i e 16 i n p pu uts ’) 83   disp (   ’ T h eerr e f o orr e SOP f o rm r m i s l e s s e x p een nsive ’)

Scilab code Exa 6.18   Minimise expression

1 2 3 4 5 6 7 8

9 10 11 12 13 14 15

−    // xin / /E Mxamp iample n im i m le i se s e 6 a n18// e x p r e s s i o n //   clc

  / / c l e a r s t he he c o n s o l e //   clear

  // 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 //   / / Ma Ma p p i n g t h e e x p r e s s i o n / /   disp (  ’Y=  ’ Y=m (2 ,3 ,4 ,5 ,6 ,7 ,12 ,13 ,14 ,15 ,18 ,19 ,2 0 ,2 1 ,2 2 ,2 3 ,2 8 ,2 9 ,3 0 ,3 1) ’) A’ ’   disp (   ’ A ’)   disp (   ’ D ’ ’ E ’ ’ D ’ ’ E DE DE ’ ’ D ’ ’E ’ ’ D ’ ’E DE DE’ ’ ’ )   disp (   ’ B ’ ’ C ’ ’ 0 0 1 1 0 0 1 1 ’)   disp (   ’ BC ’ ’ 1 1 1 1 1 1 1 1 ’)   disp (   ’ BC 1 1 1 1 1 1 1 1 ’)   disp (   ’ BC ’ ’ 0 0 0 0 0 0 0 0 ’) h e map , h ig i g h o u ttp p u ttss f o r   disp (   ’ From t he 2 ,3 ,4 ,5 ,6 ,7 ,12 ,13 ,1 4 ,15 ,18 ,19 ,20 ,21 ,2 2 ,2 3 ,2 8 ,2 9 ,30 ,31 86

 

’) 16   / / T h e r e f o r e t h e kmap i s d i s p l a y e d / / 17   disp (   ’ T h e r e f o r e t h e m in i mu m e x p r e s s i o n  ’Y= =C+B ’ ’D ’ ) 18   disp (  ’Y la y e d // 19   / / r e s u l t i s d i s p la

is : ’)

Scilab code Exa 6.19  Reduce kmap by POS

1 2 3 4 5 6 7 8 9 10 11 12 13 14

  // E xamp x ample le 6 − 19//   / / r ed e d u ce c e e x p r e s s i o n u s i ng n g k−map b y f i n d i n g POS / /   clc

  / / c l e a r s t h e window / /   clear

  // 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 //   / / Ma Ma p p i n g t h e e x p r e s s i o n / /   disp (   ’ C ’ ’ D ’ ’ C ’ ’ D CD CD ’ ’ ’ ) 0 0 0 1 ’)   disp (   ’ A ’ ’ B ’ ’   disp (   ’ AB ’ ’ 1 0 0 0 ’)   disp (   ’ AB 1 1 0 0 ’)   disp (   ’ AB ’ ’ 1 1 1 0 ’)   disp (   ’ From t h e m maap , l oow w outp pu uts for 0 ,1 ,3 ,5 ,6 ,7 ,1 0 ,1 4 ,1 5 ’ ) 15   / / g i v e n l o g i c e q u a t i o n / /

16   c =[ =[ 0 0 0 0 ; 0 0 0 1 ; 0 0 1 1 ; 0 1 0 1 ; 0 1 1 0 ; 0 1 1 1 ; 1 0 1 0;1 1 1 0; 1 1 1 1] 17   disp ( c ) =1: 9 18   f o r   i =1 i f   c ( i , 1 ) = = 1   then 19   20   d ( i , 1 ) =  ’A’ ’ ’ 21   else 22   d ( i , 1 ) =  ’  ’A A’   end 23 24   i f   c ( i , 2 ) = = 1   then   d ( i , 2 ) =  ’B’ ’ ’ 25

87

 

26 27

 

else  

d ( i , 2 ) =  ’ B ’

28   end   i f   c ( i , 3 ) = = 1   then 29 30   d ( i , 3 ) =  ’C ’ ’ ’ 31   else 32   d ( i , 3 ) =  ’  ’C C’   end 33 34   i f   c ( i , 4 ) = = 1   then 35   d ( i , 4 ) =  ’D’ ’ ’ 36   else 37   d ( i , 4 ) =  ’ D ’   end 38 39   e n d 40   disp (   ’ e v a l u uaa t i n g e x p r e s s i o n f r o m t r u t h t a b l e a n d map ’ ) ( 1 , 2 ) , d ( 1 , 3) 3) , d ( 1 , 4 ) ] ) 41   l = strcat ( [ d ( 1 , 1 ) , d (1 ( 2 , 2 ) , d ( 2 , 3) 3) , d ( 2 , 4 ) ] ) 42   m = strcat ( [ d ( 2 , 1 ) , d (2 ( 3 , 2 ) , d ( 3 , 3) 3) , d ( 3 , 4 ) ] ) 43   n = strcat ( [ d ( 3 , 1 ) , d (3 ( 4 , 2 ) , d ( 4 , 3) 3) , d ( 4 , 4 ) ] ) 44   o = strcat ( [ d ( 4 , 1 ) , d (4 45   x = strcat ( [ l ”+” , m ”+” , n ”+” , o ] ) 46   disp (   ’ t h hee sum o f p ro ro d du u cctt e x p r e s s i o n i s : ’ ) 47   disp ( x ) 48   disp (   ’ R e ading t he SOP f or m ’ ) 49   disp (   ’ A ’ ’B ’ ’C ’CD D ’ ’+ ’+B BC ’ ’D ’ ’+ ’+A AB ’ ’D ’D+ +AC ’ ’ ’ ) 50   / / E x p r e ss ss i o n i s d i s p l a y e d / / 51   disp (   ’ n wOSr ’e)d u ucc i n g e x p r e s s i o n u s i n ngg b o ooll ea ea n a l g e b r a fromnooP (A+ +B+C) (A ’ ’+ ’+C C ’ ’+ ’+D) D) (A (A+ +D ’ ’ ) (B ’ ’+ ’+C C’ ’ ) ’) 52   disp (  ’ (A 53   disp (   ’ PO POS h a s 1 4 i n p u t s , SO SOP h a s 1 6 i n p u t s ’ ) PO OS f oorr m i s l e s s e x p e n s i v e ’ ) 54   disp (   ’ T h e rree f o rree , P

Scilab code Exa 6.20  Find minimum of expression

1   // E xamp x ample le 6 − 20//

88

 

2   / / F i nd n d mi n im um o f 3   clc 4 5 6 7 8 9

exp res si on //

  / / c l e a r s t he he c o n s o l e //   clear

  // 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 //   / / Ma Ma p p i n g t h e e x p r e s s i o n / /   disp (  ’Y  ’Y= =M( 0 ,1 , 9 ,1 0 ,11 , 13 , 14 ,1 5 ,16 , 17 , 22 ,2 3 ,26 , 27 ) ’ )   disp (   ’ A’ ’ A ’) 10   disp (   ’ D ’ ’ E ’ ’ D ’ ’ E DE DE ’ ’ D ’ ’E ’ ’ D ’ ’E DE DE’ ’ ’ ) 0 0 1 1 0 11   disp (   ’ B ’ ’ C ’ ’ 0 1 1 ’) 12   disp (   ’ BC ’ ’ 1 1 1 1 1 ’) 1 0 0 0 1 1 1 1 ’) 1 0 0 0 1 disp (   ’ BC ’ ’ 1 0 0 ’) disp (   ’ From t he h e map , h ig i g h o u ttp p u ttss f o r 0 ,1 ,9 ,10 ,11 ,13 ,14 ,15 ,16 ,17 ,22 ,23 ,26 ,27 ’ ) / / T h e r e f o r e t h e km a p i s d i s p l a y e d / / disp (   ’ T Th he S SO OP o f e x p r e s s i o n i s : ’ ) disp (  ’Y  ’Y= =B ’ ’C ’ ’’D D+A ’ ’ B ’ ’ C+ABC+BD ’ ’ E ’ ’+ ’ +ACD ’ ’+A ’+ABD ’ ’ ’ ) disp (   ’ 2 4 i n p u ttss ’ ) disp (   ’ T Th he P PO OS o f e x p r e s s i o n i s : ’ ) 1

0

13   disp (   ’ BC 14   15   16 17 18 19 20

         

0

21   disp (  ’Y=(B+ B ’ ’+C+ ’+C+D ’ ’ ) (A ’ ’+B ’+B+ +C+D) ( A+B ’ ’+E ’ ’ ) (A (A+ +B ’ ’+D ’+ ’Y=(B+C D ’ ’ ) ’ )C+D ) ((B 22   disp (   ’ 2 1 i n p u ttss ’ ) 23   disp (   ’ T h e r e f o r e POS f o orr m i s t h e min imu m e x p r e s s i o n ’ ) la y e d // 24   / / r e s u l t i s d i s p la

Scilab code Exa 6.24   Multiple output equation using mapping

x ample le 6 − 24// 1   // E xamp 89

 

2   // Solve multiple 3   clc 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

o ut u t p ut u t e q u a t io io n u s i n g mapping / /

  / / c l e a r s t h e window / /   clear

  // 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 //   disp (  ’ f 1= 1=Sig Sigma ma m( 0 ,1 ,2 ,4 ,6 ,7 ,1 0 ,1 4 ,15 ) ’ )   / / F i r s t f u n c t i o n i s d i s p la la y e d //   disp (  ’ f 2 = =S S iigg m maa m ( 3 , 4 , 5 , 9 , 1 0 , 1 1 , 1 4 ) ’ )   / / S ec on d f u n c t i o n i s d i s p l a y e d / / e c on   disp (  ’ f 1 . f 2= 2=Sig Sigma ma m( 4 ,1 0 ,14 ) ’ )   / / T a ki k i n g t h e c om mo n e n t r i e s / /   disp (   ’ M a ap pping f o r f 1 . f 2 ’ )   disp (   ’ C ’ ’ D ’ ’ C ’ ’ D CD CD ’ ’ ’ )   disp (   ’ A ’ ’ B ’ ’ 0 0 0 0 ’)        

disp (   ’ AB ’ ’ 1 0 disp (   ’ AB 0 0 disp (   ’ AB ’ ’ 0 0 disp (   ’ From t h e map , h i g gh h

0 0 ’) 0 1 ’) 0 1 ’) outp pu uts for 4 ,10 ,14 ’)

  / / g i v e n l o g i c e q u a t i o n //   a =[ =[ 0 1 0 0 ; 1 0 1 0 ; 1 1 1 0 ]   disp ( a )   f o r   i =1 =1: 3 i f   a ( i , 1 ) = = 1   then     b ( i , 1 ) =  ’  ’A A’   else   b ( i , 1 ) =  ’A’ ’ ’                

end i f   a ( i , 2 ) = = 1   then   b ( i , 2 ) =  ’ B ’ else   b ( i , 2 ) =  ’B’ ’ ’ end i f   a ( i , 3 ) = = 1   then   b ( i , 3 ) =  ’  ’C C’ else   b ( i , 3 ) =  ’C ’ ’ ’ end i f   a ( i , 4 ) = = 1   then

90

 

40 41

b ( i , 4 ) =  ’  ’D D’

   

else

42   b ( i , 4 ) = ’ D ’ ’ ’   end 43 44   e n d 45   disp (   ’ e v a l u uaa t i n g e x p r e s s i o n f r o m t r u t h t a b l e a n d map ’ ) ( 1 , 2 ) , b ( 1 , 3) 3) , b ( 1 , 4 ) ] ) 46   l = strcat ( [ b ( 1 , 1 ) , b (1 ( 2 , 2 ) , b ( 2 , 3) 3) , b ( 2 , 4 ) ] ) 47   m = strcat ( [ b ( 2 , 1 ) , b (2 ( 3 , 2 ) , b ( 3 , 3) 3) , b ( 3 , 4 ) ] ) 48   n = strcat ( [ b ( 3 , 1 ) , b (3 49   x = strcat ( [ l ”+” , m ”+” ,n]) 50   disp ( x ) 51   / / E x p r e ss ss i o n i s d i s p l a y e d / / 52   disp (   ’ n noow r e d u ucc i n g e x p r e s s i o n u s i n ngg b o ooll ea ea n a l g e b r a ’

) 53   disp (  ’ACD’ ’+A’ ’BC’ ’D’ ’ ’ )

Scilab code Exa 6.25   Multiple output equation using mapping

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

  // E xamp x ample le 6 − 25//   / / S o l v e m u l t i p l e o ut u t p ut u t e q u a t io io n u s i n g mapping / /   clc

  / / c l e a r s t h e window / /   clear

  // 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 //   disp (  ’ f 1= 1=Sig Sigma ma m( 2 ,3 ,7 ,1 0 ,1 1 ,1 4) +d +d ( 1 ,5 ,1 5) ’ )   / / F i r s t f u n c t i o n i s d i s p la la y e d //   disp (  ’ f 2 = =S S iigg m maa m ( 0 , 1 , 4 , 7 , 1 3 , 1 5 ) + +d d (5 ,8 ,1 5) ’ )   / / S ec on d f u n c t i o n i s d i s p l a y e d / / e c on   disp (  ’ f 1 . f 2= 2=Sig Sigma ma m( 1 ,7 ,1 4) + +d d ( 5 ,1 5) ’ )   / / T a ki k i n g t h e c om mo n e n t r i e s / /   disp (   ’ M a ap pping f o r f 1 . f 2 ’ )   disp (   ’ C ’ ’ D ’ ’ C ’ ’ D CD CD ’ ’ ’ )   disp (   ’ A ’ ’ B ’ ’ 0 1 0 0 ’)   disp (   ’ AB ’ ’ 0 X 1 0 ’) 91

 

17   disp (   ’ AB 18   disp (   ’ AB ’ ’

0 0

0 0

X 0

1 ’) 0 ’)

19   disp (   ’ From t h e map ,

h i ggh h outp pu u t s f o r 1 , 7 , 1 4 and d o n t c a r e s f o r 5 , 1155 ’ ) 20   / / g i v e n l o g i c e q u a t i o n / /

=[ 0 0 0 1 ; 0 1 0 1 ; 0 1 1 1 ; 1 1 1 0 ; 1 1 1 1 ] 21   a =[ 22   disp ( a ) =1: 5 23   f o r   i =1   i f   a ( i , 1 ) = = 1   then 24 25   b ( i , 1 ) =  ’  ’A A’ 26   else 27   b ( i , 1 ) =  ’A’ ’ ’   end 28 29   i f   a ( i , 2 ) = = 1   then 30 31 32 33 34 35 36 37 38 39 40 41

 

 

 

else

b ( i , 2 ) =  ’ B ’ b ( i , 2 ) =  ’B’ ’ ’

 

end   i f   a ( i , 3 ) = = 1   then   b ( i , 3 ) =  ’  ’C C’   else   b ( i , 3 ) =  ’C ’ ’ ’   end   i f   a ( i , 4 ) = = 1   then   b ( i , 4 ) =  ’  ’D D’   else

42   b ( i , 4 ) = ’ D ’ ’ ’   end 43 44   e n d 45   disp (   ’ e v a l u uaa t i n g e x p r e s s i o n f r o m t r u t h t a b l e a n d map ’ ) ( 1 , 2 ) , b ( 1 , 3) 3) , b ( 1 , 4 ) ] ) 46   l = strcat ( [ b ( 1 , 1 ) , b (1 47   m = strcat ( [ b ( 2 , 1 ) , b (2 ( 2 , 2 ) , b ( 2 , 3) 3) , b ( 2 , 4 ) ] ) ( 3 , 2 ) , b ( 3 , 3) 3) , b ( 3 , 4 ) ] ) 48   n = strcat ( [ b ( 3 , 1 ) , b (3 49   o = strcat ( [ b ( 4 , 1 ) , b (4 ( 4 , 2 ) , b ( 4 , 3) 3) , b ( 4 , 4 ) ] ) 50   p = strcat ( [ b ( 5 , 1 ) , b (5 ( 5 , 2 ) , b ( 5 , 3) 3) , b ( 5 , 4 ) ] ) 51   x = strcat ( [ l ”+” , m ”+” , n ”+” , o ”+” , p ] ) 52   disp ( x )

92

 

53   / / E x p r e ss ss i o n i s d i s p l a y e d / / 54   disp (   ’ n noow r e d u ucc i n g e x p r e s s i o n

usin ngg b o ooll ea ea n a l g e b r a ’

)  ’AB BC+A ’ ’C ’ ’D ’D+ +A ’ ’B ’BD D’) 55   disp (  ’A

Scilab code Exa 6.26  Reduce by variable mapping

1 2 3 4 5 6 7 8 9 10 11 12

  // E xamp x ample le 6 − 26//   / / R e du d u ce c e b y V a r i a b l e M a pp pp in in g // //   clc

  / / c l e a r s t he he c o n s o l e //   clear

  // 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 //   disp (  ’M  ’M=A ’ ’ B ’ ’C ’CD D ’ ’+A ’+A ’ ’ B ’ ’CD+AB ’ ’CD ’CD’’ ’+A ’+AB ’ ’CD ’CD+A ’ ’BC ’BCD D+A ’ ’B ’ BC ’ ’D ’ D ’ ’’+ +ABC ’ ’’D D+ABC ’ ’D ’ ’ ’ ) on nv vee rt r t in i n g t he h e 4 v a r i a b l e m iin n te te r m mss t o 3   disp (   ’ C o v a r i a b l e m iin n ttee rm rm s ’ )   disp (  ’ M=m1D ’ ’ +m1D m1D+ +m5D ’ ’ +m5D m5D+ +m3D m3D+ +m3D ’ ’ +m6D m6D+ +m6D ’ ’ +m2D ’ ’ ’)   disp (  ’ u s i n g D+D ’ ’=1 ’ )   disp (  ’ R e s u l t M= B ’ ’C+ ’C+A ’ ’C ’CD D+BC ’ ’D ’ ’+ ’+A ABC ’ ’ ’ ) la y e d //   / / f i n a l e x p r e s s i o n i s d i s p la

Scilab code Exa 6.27  Reduce by variable mapping

1 2 3 4 5 6 7

  // E xamp x ample le 6 − 27//   / / R e du c e b y V a r i a b l e M a pp pp in in g // // d u ce   clc

  / / c l e a r s t he he c o n s o l e //   clear

  // 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 //   disp (  ’ Z=A ’ ’B ’ ’C ’CD DE+A ’ ’BC ’ ’DE ’ ’+A ’+A ’ ’BC ’BCD+AB ’ ’CD ’CD ’ ’ F+ABC ’ ’ D ’ ’+ ’ +ABCD ’ ’ ’ ) 93

 

on nv vee rt r t in i n g t he h e 6 v a r i a b l e m iin n te te r m mss t o 4 8   disp (   ’ C o v a r i a b l e m iin n ttee rm rm s ’ ) 9   disp (  ’ m7−E+E ’ ’ ; m14−F+F ’ ’ ’ ) ’CDE+A ’ ’BD ’BDE E ’ ’+ ’+A ABD ’ ’+ ’+A ACD ’ ’ F ’ ) 10   disp (  ’ R e s u l t Z= A ’ ’CD la y e d // 11   / / f i n a l e x p r e s s i o n i s d i s p la

Scilab code Exa 6.28  Solve using 3 variable mapping

1 2 3 4 5 6 7 8 9 10 11 12

  // E xamp x ample le 6 − 28//   / / S o l v e u s i n g 3 V a r i a b l e Ma pping / /   clc

  / / c l e a r s t he he c o n s o l e //   clear

  // 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 //   disp (  ’W=Dm2 Dm2+ +m6+ m6+Dm5 Dm5+d +d ( m1+ m1+D D ’ ’ m7 ) ’ )   disp (   ’D ’Dm m5 combined with XDm1 ’ )   disp (   ’D ’Dm m2 combined with Dm6 ’ )   disp (   ’ D ’ ’ m m66 c om o m b iin n eed d e i t h e r w i t h Dm6 o r XD ’ ’ m m77 ’ )   disp (   ’ R e s u l t W=B ’ ’C ’CD D+ABD ’ ’+ ’+B BC ’ ’D ’ )   / / f i n a l e x p r e s s i o n i s d i s p la la y e d //

Scilab code Exa 6.29   Reduce by mapping

1 2 3 4 5 6 7

  // E xamp x ample le 6 − 29//   // R e duc d uc e by M app appin ing// g//   clc

he c o n s o l e //   / / c l e a r s t he   clear

  // 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 //   disp (  ’R=  ’R=m0+ m0+(E+ (E+G ) m2 m2+ +E ’ ’ m5 m5+ +Gm10+ Gm10+Fm1 Fm13+ 3+m1 m14+ 4+m1 m15+d 5+d(( m1 m1+ +Em4 Em4+ + F ’ ’m8+F ’ ’m9) ’ ) 8   disp (   ’ C o on nv vee rt r t in i n g t he h e 7 v a r i a b l e m iin n te te r m mss t o 4 v a r i a b l e m iin n ttee rm rm s ’ ) 94

 

9   disp (   ’ Em Em2 c o v e r s Em0 ’ ) m00 c o m b iin n e s w i t h m1 ’ ) 10   disp (   ’ E ’ ’ m 11   disp (  ’ R e s u l t R=A ’ ’B ’ ’D ’ ’E ’E+ +A ’ ’B ’ ’C ’ ’+ ’+B B ’ ’C ’CD D ’ ’G ’G+ +ABDF+ ABC ’ ) 12   / / f i n a l e x p r e s s i o n i s d i s p la la y e d //

Scilab code Exa 6.30   Reducing expression by Kmap

1   // E xamp x ample le 6 − 30// 2   / / r ed e d u ce ce e x p r e s s i o n 3   clc 4 5 6 7 8 9 10 11 12 13

u s i ng n g k−map//

   clear / / c l e a r s t h e window / /

  // 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 //   / / Ma Ma p p i n g t h e e x p r e s s i o n / /   disp (   ’ C ’ ’ D ’ ’ C ’ ’ D CD CD ’ ’ ’ )   disp (   ’ A ’ ’ B ’ ’ 1 0 1 1 ’)   disp (   ’ AB ’ ’ 0 0 1 1 ’)   disp (   ’ AB 0 1 0 0 ’)   disp (   ’ AB ’ ’ 1 1 0 1 ’)   disp (   ’ From t he h e map , h ig i g h o u ttp p u ttss f o r 0 ,2 ,3 ,6 ,7 ,8 ,9 ,10 ,13 ’ ) 14   / / g i v e n l o g i c e q u a t i o n / / 15   a = [ 0 0 0 0 ; 0 0 1 0 ; 0 0 1 1 ; 0 1 1 0 ; 0 1 1 1 ; 1 0 0 0 ;1 0 0 1;1 0 1 0;1 1 0 1] 16   disp ( a ) 17   f o r   i =1 =1: 9 i f   a ( i , 1 ) = = 1   then   18 19   b ( i , 1 ) =  ’  ’A A’ 20   else 21   b ( i , 1 ) =  ’A’ ’ ’   end 22 23   i f   a ( i , 2 ) = = 1   then 24   b ( i , 2 ) =  ’ B ’ 25   else

95

 

b ( i , 2 ) =  ’B’ ’ ’

 

26 27

 

end

28   i f   a ( i , 3 ) = = 1   then   b ( i , 3 ) =  ’ 29  ’C C’ 30   else   b ( i , 3 ) =  ’C ’ ’ ’ 31   end 32   i f   a ( i , 4 ) = = 1   then 33 34   b ( i , 4 ) =  ’  ’D D’ 35   else 36   b ( i , 4 ) =  ’ D ’ ’ ’   end 37 38   e n d 39   disp (   ’ e v a l u uaa t i n g e x p r e s s i o n f r o m t r u t h t a b l e a n d 40 41 42 43 44 45 46 47 48 49

                   

map ’ ) ( 1 , 2 ) , b ( 1 , 3) 3) , b ( 1 , 4 ) ] ) x 1 = strcat ( [ b ( 1 , 1 ) , b (1 x 2 = strcat ( [ b ( 2 , 1 ) , b (2 ( 2 , 2 ) , b ( 2 , 3) 3) , b ( 2 , 4 ) ] ) ( 3 , 2 ) , b ( 3 , 3) 3) , b ( 3 , 4 ) ] ) x 3 = strcat ( [ b ( 3 , 1 ) , b (3 x 4 = strcat ( [ b ( 4 , 1 ) , b (4 ( 4 , 2 ) , b ( 4 , 3) 3) , b ( 4 , 4 ) ] ) ( 5 , 2 ) , b ( 5 , 3) 3) , b ( 5 , 4 ) ] ) x 5 = strcat ( [ b ( 5 , 1 ) , b (5 x 6 = strcat ( [ b ( 6 , 1 ) , b (6 ( 6 , 2 ) , b ( 6 , 3) 3) , b ( 6 , 4 ) ] ) x 7 = strcat ( [ b ( 7 , 1 ) , b (7 ( 7 , 2 ) , b ( 7 , 3) 3) , b ( 7 , 4 ) ] ) ( 8 , 2 ) , b ( 8 , 3) 3) , b ( 8 , 4 ) ] ) x 8 = strcat ( [ b ( 8 , 1 ) , b (8 x 9 = strcat ( [ b ( 9 , 1 ) , b (9 ( 9 , 2 ) , b ( 9 , 3) 3) , b ( 9 , 4 ) ] ) x = ( [ x 1 ”+” , x 2 ”+” , x 3 ”+” , x 4 ”+” , x 5 ”+” , x 6 ”+” , x 7 ”+” , x 8 ”+” , x9 ])

50   disp ( x ) ss i o n i s d i s p l a y e d / / 51   / / E x p r e ss 52   disp (   ’ n noow r e d u ucc i n g e x p r e s s i o n ) 53   disp (  ’B ’ ’D ’ ’+ ’+A A ’ ’ C+AC ’ ’D ’ )

96

usin ngg b o ooll ea ea n a l g e b r a ’

 

Chapter 11 Analog Digital Conversion

Scilab code Exa 11.1  Percentage resolution of 5bit DA converter

1 2 3 4 5 6 7 8 9

  // E xamp x ample le 11 11− − 1/ /   / / % R e so s o l ut u t i on o n o f a f i v e b i t D/A c o n v e r t e r / /   clc

  / / c l e a r s t he he c o n s o l e //   clear

  // 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 //

  n =5

  / / h e re r e n i s t he h e number o f b i t s / /   disp (   ’ M Maax n u um mber t h a t c a n b e r e p r e s e n t e d u s i n g 5 b i t s i s t he h e b in i n aarr y n u um m b e r 1 1111 1111 i e 3 1 i n d eecc im i m aall form ’ )

10   p r e s = ( 1 / ( ( 2 ^ n ) - 1 ) ) * 1 0 0 11   / / p r e s d e no n o t es e s t he h e p e r ce ce n t r e s o l u t i o n // 12   disp (   ’ T h hee rree ffoo re r e t h e p er e r ccee n ntt r e s o l u t i o n o f t h e 5 b i t D/A c o n v e r t e r i s : ’ ) 13   disp ( p r e s ) n s we we r i s d i s p l a y e d / / 14   / / a ns

Scilab code Exa 11.2  6 bit analog to digital converter

97

 

x ample le 11 11− 1   // E xamp − 2/ / 2   clc 3 4 5 6 7 8

  / / c l e a r s t he he c o n s o l e //   clear

  // 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 //

  n =6   mpsv=20

  / /n / n d e n o t es e s t h e n u m b e r o f b i t s , mp s v d e n o t es e s t h e max . p r e c i s i o n s up u p pl pl y v o l t a g e // 9   disp (   ’ Ea E a c h b i t r e p r e s e n t s 1 / ((22 ˆ 66)) −1 o f t h e t o t a l 2 0V ’) 10   E 1 = ( 1 / 6 3 ) * 2 0 11   / / g i v e n a b i n a r y n u m b e r 1 00 0 0 11 1 1 0 w h o se se v o l t a g e

value

i s t o b e f o un u n d // // 12 13 14 15

       

a=100110 b = b i n 2 d e c (  ’ 1001 10 ’ ) E2=(b /((2^6) -1))*20 disp (   ’ T Th he v o l t a g e c h haa n ngg e t h aatt e aacc h L LS SB r e p r e s e n t s : ’) 16   disp ( E 1 ) 17   disp (   ’ T Th h e v o l t a g e t h a t t h e b i n a r y n u m b e r 1 0000 1111 0 r e pr e se n ts i s : ’ ) 18   disp ( E 2 ) 19   / / a n sw s w e rs rs a r e d i s p l a y e d / /

Scilab code Exa 11.3  Compute the gain of an Opamp

1 2 3 4 5 6 7 8

  // E xamp x ample le 11− 11 − 3/ /   / / co c o mp mp ut ut e g a i n o f a n o p− p−amp//   clc

  / / c l e a r s t he he c o n s o l e //   clear

  // 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 //

  Rf=40   Rs=5

98

is

 

9   Ein=3.2 G i v e n t he he 10   // / / Gi

r e s i s t a n c e v a l ue u e s i n k i l o o h m s an a n d t he he i np np u utt v o l t ag ag e i n v o l t s //

11 12 13 14 15 16 17

  Av=Rf/Rs

  / /A e n o te t e s t he h e g ai a i n o f t he h e o p−amp// / Av d en   Eo=Av*Ein

/ Eo d e n o t es e s t h e o ut u t pu pu t v o l t a g e / /   / /E   disp (   ’ T Th h e v o l t a g e g aaii n o f t h hee g iv i v een n o p−amp i s :

  disp ( A v )   disp (   ’ T Th he o u utt p u utt v o l t a g e o f t h hee op a mp i n v o l t s ) 18   disp ( E o ) 19   / / r e s u l t s a r e d i s p l a y e d / /

’) is :



Scilab code Exa 11.4   Compute the output voltage

1 2 3 4 5 6 7 8 9 10 11 12 13 14

  // E xamp x ample le 11− 11 − 4/ /   // / / co c o m p u t e o u ttp p ut u t v o lt l t a ge g e f o r c i r c u i t i n f i g 1 1 − 12//   clc

  / / c l e a r s t h e window / /   clear

  // c l e a r s a l l              

e x i s t i n g v a r i b l e s //

R1=10 R2=10 R3=10 Rf=10 E1=3 E2=-2 E3=-4

  / / Gi G i v e n a l l t he h e r e s i s t a n c e v a l u es e s i n k i l o o h m s aan nd th e v o l ta g e s i n v o l t s a t t h hee i n p u t s / / 15   disp (   ’ t h e c u r r e n t t h hrr ou o u ggh h t r e s i s t o r R1 i n m i l l i a m p s i s : ’) 16   I 1 = E 1 / R 1 17   disp ( I 1 )

99

 

hrr ou o u ggh h t r e s i s t o r R2 i n m i l l i a m p s 18   disp (   ’ t h e c u r r e n t t h i s : ’) 19   I 2 = E 2 / R 2 20   disp ( I 2 ) hrr ou o u ggh h t r e s i s t o r R3 i n m i l l i a m p s 21   disp (   ’ t h e c u r r e n t t h i s : ’) 22   I 3 = E 3 / R 3 23   disp ( I 3 ) 24   E o = - ( I 1 + I 2 + I 3 ) * R f 25   / /E / Eo d e n o t es e s t h e o ut u t pu pu t v o l t a g e / / 26   disp (   ’ T Th he o u t p u t v o l t a g e i n v o l t s o f t h e c i r c u i t i s : ’) 27   disp ( E o ) 28   / / a ns n s we we r i s d i s p l a y e d / /

Scilab code Exa 11.5   Compute output voltage

x ample le 11− 11 − 5/ / 1   // E xamp 2   / / co p ut u t v o lt l t a ge ge co m p u t e o u ttp

f o r th e c i r c u i t i n f i g .

1 1 − 13// 3 4 5 6 7 8 9 10 11 12 13 14

  clc

  / / c l e a r s t he he c o n s o l e //   clear

  // 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 //   disp (   ’ s w i t c h c o n f i g u r a t i o n ABCD i s 0 1 111 0 ’ )            

E =8 RA=1 RB=2 RC=4 RD=8 Rf=1

G i v e n a l l t he h e r e s i s t a n c e v a l u es e s i n k i l o o h m s an a nd   / / Gi t h e c o mmo n v o l t a g e E i n v o l t s / / 15   disp (   ’ t h hee c u r r e n ntt t h hrr oou u ggh h t r e s i s t o r RA i n m i l l i a m p s i s : ’) 100

 

16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

  IA=0

c h A i s o pe p e n // //   / / s w i t ch   disp ( I A )   disp (   ’ t h hee c u r r e n ntt t h hrr oou u ggh h t r e s i s t o r RB i n i s : ’)   IB=E/RB   disp ( I B )   disp (   ’ t h hee c u r r e n ntt t h hrr oou u ggh h t r e s i s t o r RC i n i s : ’)   IC=E/RC   disp ( I C )   disp (   ’ t h hee c u r r e n ntt t h hrr oou u ggh h t r e s i s t o r RD i n i s : ’)   ID=0

m i l l i amps

m i l l i amps

m i l l i amps

  / / s w i t ch c h D i s o pe p e n // //   disp ( I D )   Itot=IA+IB+IC+ID

  / / t o t a l c u r r e nt n t i s d en e n ot o t ed ed by I t o t / /   If=Itot   Eo=If*Rf   Eo=Eo*(-1)

  / /E / Eo d e n o t es e s o u tp t p ut ut v o l t a g e //   disp (   ’ O Ou utput v o l t a g e o f t h e c i r c u i t i n v o l t s i s : ’ )   disp ( E o )

  / / r e s u l t i s d i s p la la y e d //

Scilab code Exa 11.6   Resolution and Percent resolution 12bit DAcon DAconverter verter

1   // E xamp x ample le 11 11− − 6/ / 2   / / r e s o l u t i o n a n d p e r c e nt nt

r e s o l u t i o n o f a 1 2 b i t D/ D/A c o n v e rt r t e r , o ut ut p u utt v a r i e s f ro r o m   −50 t o 5 0/ 0/ /

3   clc 4   // cl ea rs 5   clear

t h e window / /

101

 

6 7 8 9

  // 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 //

  E=50-(-50)   n=12

  / / g i v en e n t he h e v o l t a g e r an a n ge g e E i n v o l t s a n d t he h e number o f b i t s n // // 10   disp (   ’ 1 2 b i t c o n v e r t eerr c a n h a v e ( 2 2ˆˆ 1 22)) −1 n oon n zero s ta te s ’) 11 12 13 14 15 16 17

           

Res=E/((2^n)-1) Pres=1/((2^n)-1)*(100) disp (   ’ t h e r e s o l u t i o n i n v o l t s i s : ’ ) disp ( R e s ) disp (   ’ t h e p eerr ccee n t r e s o l u t i o n i s : ’ ) disp ( P r e s )

  / / a n sw s w e rs rs a r e d i s p l a y e d / /

Scilab code Exa 11.7   Resolution and Percent resolution 10bit ADconv ADconverter erter

1   // E xamp x ample le 11 11− − 7/ / 2   / / r e s o l u t i o n a n d p e r c e nt nt

r e s o l u t i o n o f a 1 0 b i t D/ D/A c o n ve v e r te t e r , i n pu p u t v o l t a g e v a r i e s f r o m   −10 t o 1 0/ 0/ /

3 4 5 6 7 8 9

  clc

  / / c l e a r s t h e window / /   clear

  // 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 //

  E=10-(-10)   n=10

  / / g i v en e n t he h e v o l t a g e r an a n ge g e E i n v o l t s a n d t he h e number o f b i t s n // // 10   disp (   ’ 1 0 b i t c o n v e r t eerr c a n h a v e ( 2 2ˆˆ 1 00)) −1 n oon n zero s ta te s ’) 11 12 13 14

       

Res=E/((2^n)-1) Pres=1/((2^n)-1)*(100) disp (   ’ t h e r e s o l u t i o n i n disp ( R e s * 1 0 0 0 )

m i l l iv o l ts i s :

102

’)

 

15   disp (   ’ t h e p eerr ccee n t r e s o l u t i o n 16   disp ( P r e s ) 17   / / a n ssw w e rs rs a r e d i s p l a y e d / /

103

is :

’)

View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF