Scilab Textbook Companion For Digital Electronics: Circuits and Systems by V. K. Puri
Scilab Textbook Companion For Digital Electronics: Circuits and Systems by V. K. Puri
Scilab Textbook Companion For Digital Electronics: Circuits and Systems by V. K. Puri
Created by
Rishav Dutta
B.Tech
Electronics Engineering
NIT Durgapur
College Teacher
Prof. Sabyasachi Sengupta,iit Kharagpur
Cross-Checked by
Mukul R. Kulkarni
Author: V. K. Puri
Edition: 1
Year: 1997
ISBN: 0-07-463317-1
1
Scilab numbering policy used in this document and the relation to 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
3
List of Scilab Codes
4
Exa 1.17.b division of two binary numbers . . . . . . . 43
Exa 1.18.a binary to hex conversion . . . . . . . . . . . 47
Exa 1.18.b decimal to octal conversion . . . . . . . . . 48
Exa 1.18.c binary to decimal conversion . . . . . . . . . 48
Exa 1.19.a decimal to octal conversion . . . . . . . . . 50
Exa 1.19.b decimal to octal conversion . . . . . . . . . 51
Exa 1.19.c decimal to octal conversion . . . . . . . . . 52
Exa 1.20.a decimal to signed binary number conversion 53
Exa 1.20.b decimal to signed binary system conversion . 54
Exa 1.20.c decimal to signed binary system conversion . 54
Exa 1.21.a subtraction using ones complement . . . . . 55
Exa 1.21.b subtraction using ones complement . . . . . 55
Exa 1.21.c subtraction using ones complement . . . . . 56
Exa 1.21.d subtraction using ones complement . . . . . 56
Exa 1.21.e subtraction using ones complement . . . . . 59
Exa 1.22.a subtraction using twos complement . . . . . 62
Exa 1.22.b subtraction using twos complement . . . . . 63
Exa 1.22.c subtraction using twos complement . . . . . 64
Exa 1.22.d subtraction using twos complement . . . . . 66
Exa 1.22.e subtraction using twos complement . . . . . 69
Exa 1.22.f subtraction using twos complement . . . . . 72
Exa 1.23.a BCD to decimal equivalent conversion . . . 75
Exa 1.23.b BCD to decimal equivalent conversion . . . 76
Exa 1.24.a binary to gray code conversion . . . . . . . 78
Exa 1.24.b binary to gray code conversion . . . . . . . . 79
Exa 1.24.c binary to gray code conversion . . . . . . . . 80
Exa 1.25.a subtraction using ones complement . . . . . 81
Exa 1.25.b gray code to binary conversion . . . . . . . . 81
Exa 1.25.c decimal to BCD conversion . . . . . . . . . 82
Exa 1.25.d subtraction using ones complement . . . . . 83
Exa 1.26 finding equivalent in decimal for excess 3 code 84
Exa 1.27.a gray code to binary conversion . . . . . . . . 85
Exa 1.27.b gray code to binary conversion . . . . . . . . 86
Exa 1.27.c gray code to binary conversion . . . . . . . . 87
Exa 1.28.a constructing an even parity 7 bit hamming
code . . . . . . . . . . . . . . . . . . . . . . 88
Exa 1.28.b constructing an even parity 7 bit hamming
code . . . . . . . . . . . . . . . . . . . . . . 89
5
Exa 1.29 locating error in hamming code and correct-
ing it . . . . . . . . . . . . . . . . . . . . . . 91
Exa 2.8.a simplification of boolean expression . . . . . 94
Exa 2.8.b simplification of boolean expression . . . . . 95
Exa 2.9.a simplification of boolean expression . . . . . 95
Exa 2.9.b simplification of boolean expression . . . . . 96
Exa 2.9.c simplification of boolean expression . . . . . 96
Exa 2.10.a simplification of boolean expression . . . . . 97
Exa 2.10.b simplification of boolean expression . . . . . 97
Exa 2.10.c simplification of boolean expression . . . . . 98
Exa 2.11 simplification of boolean expression . . . . . 98
Exa 2.13.a simplification of boolean expression . . . . . 99
Exa 2.13.b simplification of boolean expression . . . . . 99
Exa 2.29 finding SOP . . . . . . . . . . . . . . . . . . 100
Exa 2.30 finding POS . . . . . . . . . . . . . . . . . . 100
Exa 2.31.a conversion to canonical SOP . . . . . . . . . 101
Exa 2.31.b conversion to canonical POS . . . . . . . . . 102
Exa 2.32 finding SOP and POS . . . . . . . . . . . . 102
Exa 2.33 finding SOP and POS . . . . . . . . . . . . 103
Exa 2.34 finding maxterms and minterms . . . . . . . 104
Exa 2.35 finding maxterms . . . . . . . . . . . . . . . 104
Exa 2.36 finding required data . . . . . . . . . . . . . 105
Exa 2.37 finding SOP . . . . . . . . . . . . . . . . . . 105
Exa 2.38 finding sum of minterms and product of max-
terms . . . . . . . . . . . . . . . . . . . . . 106
Exa 3.1 finding required data . . . . . . . . . . . . . 107
Exa 3.2 finding maximum amplification . . . . . . . 108
Exa 3.3.a finding clamping level voltage . . . . . . . . 108
Exa 3.3.b finding clamping level voltage . . . . . . . . 109
Exa 3.4 finding output voltage . . . . . . . . . . . . 109
Exa 3.6 finding resistance and capacitance . . . . . . 110
Exa 3.7 finding peak voltage . . . . . . . . . . . . . 111
Exa 5.1 finding required time . . . . . . . . . . . . . 112
Exa 5.2 finding frequency and duty cycle . . . . . . 112
Exa 5.3 finding frequency and duty cycle . . . . . . 113
Exa 8.1 finding binary equivalent weights . . . . . . 115
Exa 8.2 calculating required data . . . . . . . . . . . 116
Exa 8.3 calculating required data . . . . . . . . . . . 117
6
Exa 8.4 finding required data . . . . . . . . . . . . . 117
Exa 8.5 calculating required data . . . . . . . . . . . 118
Exa 8.6 calculating required data . . . . . . . . . . . 119
Exa 8.7 calculating value of resistors . . . . . . . . . 119
Exa 8.8 finding value of resistor . . . . . . . . . . . . 120
Exa 8.9 finding required data . . . . . . . . . . . . . 120
Exa 8.10 finding required data . . . . . . . . . . . . . 121
Exa 8.11 calculating output voltages . . . . . . . . . . 122
Exa 8.12 finding output voltages . . . . . . . . . . . . 122
Exa 8.13 calculating maximum scale voltage . . . . . 123
Exa 8.14 finding required data . . . . . . . . . . . . . 123
Exa 8.15 calculating number of bits . . . . . . . . . . 124
Exa 8.16 finding resolution . . . . . . . . . . . . . . . 125
Exa 8.17 calculating required data . . . . . . . . . . . 125
Exa 8.18 calculating conversion time . . . . . . . . . 126
Exa 8.19 finding resolution . . . . . . . . . . . . . . . 127
Exa 8.20 finding number of bits . . . . . . . . . . . . 127
Exa 8.21 calculating maximum conversion time . . . . 128
Exa 8.22 finding error in voltage . . . . . . . . . . . . 128
Exa 8.23 finding aperture time . . . . . . . . . . . . . 129
Exa 8.24 finding quantisation error . . . . . . . . . . 129
Exa 8.25 finding digital output in binary form . . . . 130
7
Chapter 1
1 // d e c i m a l t o b i n a r y c o n v e r s i o n //
2 // e x a m p l e 1 . a //
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s //
7 // d e c i m a l t o b i n a r y c o n v e r s i o n //
8 a = dec2bin (1993)
9 disp ( ’ b i n a r y e q u i v a l e n t o f d e c i m a l number : ’ )
10 disp ( a )
11 // a n s w e r i n b i n a r y form //
1 // b i n a r y t o d e c i m a l c o n v e r s i o n //
2 // e x a m p l e 1 . b //
8
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s //
7 // b i n a r y t o d e c i m a l c o n v e r s i o n //
8 a = bin2dec ( ’ 1 1 1 1 1 0 0 1 0 0 1 ’ )
9 disp ( ’ d e c i m a l e q u i v a l e n t o f b i n a r y number : ’ )
10 disp ( a )
11 // a n s w e r i n d e c i m a l form //
1 // b i n a r y t o d e c i m a l c o n v e r s i o n //
2 // e x a m p l e 2//
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s //
7 p =1;
8 // i n i t i a l i s i n g //
9 q =1;
10 z =0;
11 b =0;
12 w =0;
13 f =0;
14 // b i n= i n p u t ( E n t e r t h e b i n a r y no t o be c o n v e r t e d
to i t s decimal e q u i v a l e n t : )
15 // a c c e p t i n g t h e b i n a r y i n p u t from u s e r //
16 bin =11.0111;
17 d = modulo ( bin ,1) ;
18 // s e p a r a t i n g t h e d e c i m a l p a r t and t h e i n t e g e r p a r t //
19 d = d *10^10;
20 a = floor ( bin ) ;
21 // r e m o v i n g t h e d e c i m a l p a r t //
22 while ( a >0)
9
23 // Loop t o t a k e t h e b i n a r y b i t s o f i n t e g e r i n t o a
m a t r i x //
24 r = modulo ( a ,10) ;
25 b (1 , q ) = r ;
26 a = a /10;
27 a = floor ( a ) ;
28 q = q +1;
29 end
30 for m =1: q -1
31 // m u l t i p l i y i n g t h e b i t s o f i n t e g e r p o s i t i o n v a l u e s
and a d d i n g //
32 c = m -1;
33 f = f + b (1 , m ) *(2^ c ) ;
34 end
35 while ( d >0)
36 // Loop t o t a k e t h e b i n a r y b i t s o f d e c i m a l i n t o a
m a t r i x //
37 e = modulo ( d ,2)
38 w (1 ,p ) = e
39 d = d /10;
40 d = floor ( d )
41 p = p +1;
42 end
43 for n =1: p -1
44 // m u l t i p l i y i n g t h e b i t s o f d e c i m a l w i t h t h e i r
p o s i t i o n v a l u e s and a d d i n g //
45 z = z + w (1 ,n ) *(0.5) ^(11 -n ) ;
46 end
47 z = z *10000;
48 // r o u n d i n g o f t o 4 d e c i m a l v a l u e s //
49 z = round ( z ) ;
50 z = z /10000;
51 x=f+z;
52 disp ( ’ The D e c i m a l e q u i v a l e n t o f t h e b i n a r y number
g i v e n i s ’ );
53 disp ( x ) ; // r e s u l t i s d i s p l a y e d //
10
Scilab code Exa 1.3 binary to decimal conversion
1 // b i n a r y t o d e c i m a l c o n v e r s i o n //
2 // e x a m p l e 3//
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s //
7 p =1;
8 // i n i t i a l i s i n g //
9 q =1;
10 z =0;
11 b =0;
12 w =0;
13 f =0;
14 // b i n= i n p u t ( E n t e r t h e b i n a r y no t o be c o n v e r t e d
to i t s decimal e q u i v a l e n t : )
15 // a c c e p t i n g t h e b i n a r y i n p u t from u s e r //
16 bin =1100.11;
17 d = modulo ( bin ,1) ;
18 // s e p a r a t i n g t h e d e c i m a l p a r t and t h e i n t e g e r p a r t //
19 d = d *10^10;
20 a = floor ( bin ) ;
21 // r e m o v i n g t h e d e c i m a l p a r t //
22 while ( a >0)
23 // Loop t o t a k e t h e b i n a r y b i t s o f i n t e g e r i n t o a
m a t r i x //
24 r = modulo ( a ,10) ;
25 b (1 , q ) = r ;
26 a = a /10;
27 a = floor ( a ) ;
28 q = q +1;
29 end
30 for m =1: q -1
11
31 // m u l t i p l i y i n g t h e b i t s o f i n t e g e r p o s i t i o n v a l u e s
and a d d i n g //
32 c = m -1;
33 f = f + b (1 , m ) *(2^ c ) ;
34 end
35 while ( d >0)
36 // Loop t o t a k e t h e b i n a r y b i t s o f d e c i m a l i n t o a
m a t r i x //
37 e = modulo ( d ,2)
38 w (1 ,p ) = e
39 d = d /10;
40 d = floor ( d )
41 p = p +1;
42 end
43 for n =1: p -1
44 // m u l t i p l i y i n g t h e b i t s o f d e c i m a l w i t h t h e i r
p o s i t i o n v a l u e s and a d d i n g //
45 z = z + w (1 ,n ) *(0.5) ^(11 -n ) ;
46 end
47 z = z *10000;
48 // r o u n d i n g o f t o 4 d e c i m a l v a l u e s //
49 z = round ( z ) ;
50 z = z /10000;
51 x=f+z;
52 disp ( x )
53 // r e s u l t i s d i s p l a y e d //
1 // d e c i m a l t o b i n a r y c o n v e r s i o n //
2 // e x a m p l e 4//
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s //
12
7 // d e c i m a l t o b i n a r y c o n v e r s i o n //
8 q =0;
9 b =0;
10 s =0;
11 // e n t e r t h e d e c i m a l number t o be c o n v e r t e d //
12 a =27/32;
13 d = modulo (a ,1) ; // s e p a r a t i n g d e c i m a l and i n t e g e r p a r t
//
14 a = floor ( a ) ; // r e m o v i n g d e c i m a l p a r t //
15 while (a >0)
16 // t a k i n g i n t e g e r p a r t i n t o a m a t r i x and c o n v e r t i n g
t o e q u i v a l e n t b i n a r y //
17 x = modulo (a ,2)
18 b = b +(10^ q ) * x
19 a = a /2
20 a = floor ( a )
21 q = q +1
22 end
23 for i =1:10
24 // f o r v a l u e s a f t e r d e c i m a l t h a t s h o u l d be c o n v e r t e d
t o b i n a r y //
25 d = d *2
26 q = floor ( d )
27 s = s + q /(10^ i ) ;
28 if d >=1 then
29 d =d -1
30 end
31 end
32 k=b+s;
33 disp ( ’ t h e b i n a r y e q u i v a l e n t o f t h e g i v e n d e c i m a l
number i s ’ )
34 disp ( k ) ; // d i s p l a y i n g t h e r e s u l t //
13
1 // d e c i m a l t o o c t a l c o n v e r s i o n //
2 // e x a m p l e 5//
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s //
7 // d e c i m a l t o o c t a l c o n v e r s i o n //
8 x =1993
9 a = dec2oct ( x )
10 disp ( ’ t h e number i n o c t a l form i s : ’ )
11 disp ( a )
1 // O c t a l t o d e c i m a l c o n v e r s i o n //
2 // e x a m p l e 6//
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s //
7 // o c t a l t o d e c i m a l c o n v e r s i o n //
8 x= ’ 3711 ’
9 a = oct2dec ( x )
10 disp ( ’ t h e number i n d e c i m a l form : ’ )
11 disp ( a )
1 // b i n a r y t o o c t a l c o n v e r s i o n //
2 // e x a m p l e 7//
3 clc
4 // c l e a r s t h e c o n t r o l window //
5 clear
14
6 // c l e a r s //
7 // b i n a r y t o o c t a l c o n v e r s i o n //
8 x= ’ 11111001001 ’
9 a = bin2dec ( ’ 1 1 1 1 1 0 0 1 0 0 1 ’ )
10 // b i n a r y t o d e c i m a l c o n v e r s i o n //
11 z = dec2oct ( a )
12 // d e c i m a l t o o c t a l c o n v e r s i o n //
13 disp ( ’ t h e number i n o c t a l form : ’ )
14 disp ( z )
1 // d e c i m a l t o b i n a r y c o n v e r s i o n //
2 // e x a m p l e 8 . a //
3 clc
4 // c l e a r s command window //
5 clear
6 // c l e a r s //
7 // d e c i m a l t o b i n a r y c o n v e r s i o n //
8 x =1996
9 a = dec2bin (1996)
10 disp ( ’ t h e r e s u l t i n b i n a r y form i s ’ )
11 disp ( a )
1 // b i n a r y t o d e c i m a l c o n v e r s i o n //
2 // e x a m p l e 8 . b //
3 clc
4 // c l e a r s command window //
5 clear
6 // c l e a r s //
7 x= ’ 11111001100 ’
15
8 a = bin2dec ( ’ 1 1 1 1 1 0 0 1 1 0 0 ’ ) ; // b i n a r y t o d e c i m a l
c o n v e r s i o n //
9 disp ( ’ t h e number i n d e c i m a l form i s : ’ )
10 disp ( a ) ; // r e s u l t i s d i s p l a y e d //
1 // d e c i m a l t o b i n a r y c o n v e r s i o n //
2 // e x a m p l e 9 . a //
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s //
7 // d e c i m a l t o b i n a r y c o n v e r s i o n //
8 q =0;
9 b =0;
10 s =0;
11 // e n t e r t h e d e c i m a l number t o be c o n v e r t e d //
12 a =274.1875;
13 d = modulo (a ,1) ; // s e p a r a t i n g d e c i m a l and i n t e g e r p a r t
//
14 a = floor ( a ) ; // r e m o v i n g d e c i m a l p a r t //
15 while (a >0)
16 // t a k i n g i n t e g e r p a r t i n t o a m a t r i x and c o n v e r t i n g
t o e q u i v a l e n t b i n a r y //
17 x = modulo (a ,2)
18 b = b +(10^ q ) * x
19 a = a /2
20 a = floor ( a )
21 q = q +1
22 end
23 for i =1:10
24 // f o r v a l u e s a f t e r d e c i m a l t h a t s h o u l d be c o n v e r t e d
t o b i n a r y //
25 d = d *2
16
26 q = floor ( d )
27 s = s + q /(10^ i ) ;
28 if d >=1 then
29 d =d -1
30 end
31 end
32 k = b + s ;
33 disp ( ’ t h e b i n a r y e q u i v a l e n t of the given decimal
number i s ’ )
34 disp ( k ) ; // d i s p l a y i n g t h e r e s u l t //
1 // d e c i m a l t o b i n a r y c o n v e r s i o n //
2 // e x a m p l e 9 . b //
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s //
7 // d e c i m a l t o b i n a r y c o n v e r s i o n //
8 q =0;
9 b =0;
10 s =0;
11 // e n t e r t h e d e c i m a l number t o be c o n v e r t e d //
12 a =.65625;
13 d = modulo (a ,1) ; // s e p a r a t i n g d e c i m a l and i n t e g e r p a r t
//
14 a = floor ( a ) ; // r e m o v i n g d e c i m a l p a r t //
15 while (a >0)
16 // t a k i n g i n t e g e r p a r t i n t o a m a t r i x and c o n v e r t i n g
t o e q u i v a l e n t b i n a r y //
17 x = modulo (a ,2)
18 b = b +(10^ q ) * x
19 a = a /2
20 a = floor ( a )
17
21 q = q +1
22 end
23 for i =1:10
24 // f o r v a l u e s a f t e r d e c i m a l t h a t s h o u l d be c o n v e r t e d
t o b i n a r y //
25 d = d *2
26 q = floor ( d )
27 s = s + q /(10^ i ) ;
28 if d >=1 then
29 d =d -1
30 end
31 end
32 k=b+s;
33 disp ( ’ t h e b i n a r y e q u i v a l e n t o f t h e g i v e n d e c i m a l
number i s : ’ )
34 disp ( k ) ; // d i s p l a y i n g t h e r e s u l t //
1 // d e c i m a l t o b i n a r y c o n v e r s i o n //
2 // e x a m p l e 9 . c //
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s //
7 // d e c i m a l t o b i n a r y c o n v e r s i o n //
8 a = dec2bin (624)
9 disp ( ’ b i n a r y e q u i v a l e n t o f d e c i m a l number : ’ )
10 disp ( a )
11 // a n s w e r i n b i n a r y form //
18
1 // b i n a r y t o d e c i m a l c o n v e r s i o n //
2 // e x a m p l e 9 . d //
3 clc
4 // c l e a r s command window //
5 clear
6 // c l e a r s //
7 a = bin2dec ( ’ 1 1 0 1 0 ’ ) ; // b i n a r y t o d e c i m a l c o n v e r s i o n //
8 printf ( ’ a c t u a l a g e i s =%d y e a r s ’ ,a ) ; // p e r s o n ’ s a c t u a l
a g e i s d i s p l a y e d //
1 // hex t o d e c i m a l c o n v e r s i o n //
2 // e x a m p l e 1 0 . a //
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s //
7 // hex t o d e c i m a l c o n v e r s i o n //
8 x = ’ 7C9 ’
9 a = hex2dec ( x )
10 disp ( ’ t h e number i n d e c i m a l form i s : ’ )
11 disp ( a )
1 // d e c i m a l t o hex c o n v e r s i o n //
2 // e x a m p l e 1 0 . b //
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s //
7 // d e c i m a l t o hex c o n v e r s i o n //
19
8 x =2047
9 a = dec2hex (2047)
10 disp ( ’ t h e r e s u l t i n hex form i s : ’ )
11 disp ( a )
1 // hex t o b i n a r y and d e c i m a l c o n v e r s i o n //
2 // e x a m p l e 1 1 / /
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s //
7 // d e c i m a l c o n v e r s i o n //
8 x = ’ CD42 ’
9 a = hex2dec ( ’ CD42 ’ ) ; // hex t o d e c i m a l c o n v e r s i o n //
10 disp ( a ) ; // a n s w e r d i s p l a y e d i n d e c i m a l form //
11 // b i n a r y c o n v e r s i o n //
12 b = dec2bin ( a ) ; // d e d c i m a l t o b i n a r y c o n v e r s i o n //
13 disp ( b ) ; // a n s w e r d i s p l a y e d i n b i n a r y form //
Scilab code Exa 1.12 hex to binary and octal and decimal conversion
1 // hex t o b i n a r y , o c t a l and d e c i m a l c o n v e r s i o n //
2 // e x a m p l e 1 2 / /
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s //
7 // d e c i m a l c o n v e r s i o n //
8 x = ’ 100 ’
9 d = hex2dec ( x ) ; // hex t o d e c i m a l c o n v e r s i o n //
10 b = dec2bin ( d ) ; // d e c i m a l t o b i n a r y c o n v e r s i o n //
20
11 o = dec2oct ( d ) ; // d e c i m a l t o o c t a l c o n v e r s i o n //
12 disp ( d ) ; // a n s w e r i n d e c i m a l form //
13 disp ( b ) ; // a n s w e r i n b i n a r y form //
14 disp ( o ) ; // a n s w e r i n o c t a l form //
1 // hex t o d e c i m a l c o n v e r s i o n //
2 // e x a m p l e 1 3 . a //
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s //
7 x = ’ D2763 ’
8 a = hex2dec ( ’ D2763 ’ )
9 disp ( ’ t h e r e s u l t i n d e c i m a l form : ’ )
10 disp ( a )
1 // hex t o d e c i m a l c o n v e r s i o n //
2 // e x a m p l e 1 3 . b //
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s //
7 x = ’ B3D8 ’
8 a = hex2dec ( ’ B3D8 ’ )
9 disp ( ’ t h e r e s u l t i n d e c i m a l form : ’ )
10 disp ( a )
21
Scilab code Exa 1.14.a addition of two binary numbers
1 // a d d i t i o n o f two numbers //
2 // e x a m p l e 1 4 . a //
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s //
7 // a d d i t i o n o f two b i n a r y numbers //
8 // b i n a r y t o d e c i m a l c o n v e r s i o n //
9 x = bin2dec ( ’ 1 0 0 1 0 1 ’ )
10 y = bin2dec ( ’ 1 0 0 1 0 1 ’ )
11 z=x+y
12 // a d d i t i o n //
13 a = dec2bin ( z )
14 // d e c i m a l t o b i n a r y c o n v e r s i o n //
15 disp ( ’ t h e a d d i t i o n o f two b i n a r y numbers i s : ’ )
16 disp ( a )
1 // a d d i t i o n o f two b i n a r y numbers //
2 // e x a m p l e 1 4 . b //
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s / / ; / / d e c i m a l e q u i v a l e n t o f t h e first
b i n a r y number //
7 // a d d i t i o n o f two b i n a r y numbers //
8 p =1;
9 // i n i t i a l i s i n g //
10 q =1;
11 z =0;
12 b =0;
13 w =0;
22
14 f =0;
15 // b i n =1 s t i n p u t ( E n t e r t h e b i n a r y no t o be
converted to i t s decimal e q u i v a l e n t : )
16 // a c c e p t i n g t h e b i n a r y i n p u t from u s e r //
17 bin =1011.01;
18 d = modulo ( bin ,1) ;
19 // s e p a r a t i n g t h e d e c i m a l p a r t and t h e i n t e g e r p a r t //
20 d = d *10^10;
21 a = floor ( bin ) ;
22 // r e m o v i n g t h e d e c i m a l p a r t //
23 while ( a >0)
24 // Loop t o t a k e t h e b i n a r y b i t s o f i n t e g e r i n t o a
m a t r i x //
25 r = modulo ( a ,10) ;
26 b (1 , q ) = r ;
27 a = a /10;
28 a = floor ( a ) ;
29 q = q +1;
30 end
31 for m =1: q -1
32 // m u l t i p l i y i n g t h e b i t s o f i n t e g e r p o s i t i o n v a l u e s
and a d d i n g //
33 c = m -1;
34 f = f + b (1 , m ) *(2^ c ) ;
35 end
36 while ( d >0)
37 // Loop t o t a k e t h e b i n a r y b i t s o f d e c i m a l i n t o a
m a t r i x //
38 e = modulo ( d ,2)
39 w (1 ,p ) = e
40 d = d /10;
41 d = floor ( d )
42 p = p +1;
43 end
44 for n =1: p -1
45 // m u l t i p l i y i n g t h e b i t s o f d e c i m a l w i t h t h e i r
p o s i t i o n v a l u e s and a d d i n g //
46 z = z + w (1 ,n ) *(0.5) ^(11 -n ) ;
23
47 end
48 z = z *10000;
49 // r o u n d i n g o f t o 4 d e c i m a l v a l u e s //
50 z = round ( z ) ;
51 z = z /10000;
52 x1 = f + z ;
53 disp ( ’ The D e c i m a l e q u i v a l e n t o f t h e f i r s t b i n a r y
number g i v e n i s ’ ) ;
54 disp ( x1 ) ;
55 // t o f i n d d e c i m a l e q u i v a l e n t o f s e c o n d b i n a r y number
//
56 // b i n =2nd i n p u t ( E n t e r t h e b i n a r y no t o be
converted to i t s decimal e q u i v a l e n t : )
57 p =1;
58 // i n i t i a l i s i n g //
59 q =1;
60 z =0;
61 b =0;
62 w =0;
63 f =0;
64 // a c c e p t i n g t h e b i n a r y i n p u t from u s e r //
65 bin =1001.11;
66 d = modulo ( bin ,1) ;
67 // s e p a r a t i n g t h e d e c i m a l p a r t and t h e i n t e g e r p a r t //
68 d = d *10^10;
69 a = floor ( bin ) ;
70 // r e m o v i n g t h e d e c i m a l p a r t //
71 while ( a >0)
72 // Loop t o t a k e t h e b i n a r y b i t s o f i n t e g e r i n t o a
m a t r i x //
73 r = modulo ( a ,10) ;
74 b (1 , q ) = r ;
75 a = a /10;
76 a = floor ( a ) ;
77 q = q +1;
78 end
79 for m =1: q -1
80 // m u l t i p l i y i n g t h e b i t s o f i n t e g e r p o s i t i o n v a l u e s
24
and a d d i n g //
81 c = m -1;
82 f = f + b (1 , m ) *(2^ c ) ;
83 end
84 while ( d >0)
85 // Loop t o t a k e t h e b i n a r y b i t s o f d e c i m a l i n t o a
m a t r i x //
86 e = modulo ( d ,2)
87 w (1 ,p ) = e
88 d = d /10;
89 d = floor ( d )
90 p = p +1;
91 end
92 for n =1: p -1
93 // m u l t i p l i y i n g t h e b i t s o f d e c i m a l w i t h t h e i r
p o s i t i o n v a l u e s and a d d i n g //
94 z = z + w (1 ,n ) *(0.5) ^(11 -n ) ;
95 end
96 z = z *10000;
97 // r o u n d i n g o f t o 4 d e c i m a l v a l u e s //
98 z = round ( z ) ;
99 z = z /10000;
100 x2 = f + z ;
101 disp ( ’ The D e c i m a l e q u i v a l e n t o f t h e s e c o n d b i n a r y
number g i v e n i s ’ ) ;
102 disp ( x2 ) ;
103 res = x1 + x2 ; // a d d i t i o n //
104 // d e c i m a l t o b i n a r y c o n v e r s i o n //
105 q =0;
106 b =0;
107 s =0;
108 // e n t e r t h e d e c i m a l number t o be c o n v e r t e d //
109 a = res ;
110 d = modulo (a ,1) ; // s e p a r a t i n g d e c i m a l and i n t e g e r p a r t
//
111 a = floor ( a ) ; // r e m o v i n g d e c i m a l p a r t //
112 while (a >0)
113 // t a k i n g i n t e g e r p a r t i n t o a m a t r i x and c o n v e r t i n g
25
t o e q u i v a l e n t b i n a r y //
114 x = modulo (a ,2)
115 b = b +(10^ q ) * x
116 a = a /2
117 a = floor ( a )
118 q = q +1
119 end
120 for i =1:10
121 // f o r v a l u e s a f t e r d e c i m a l t h a t s h o u l d be c o n v e r t e d
t o b i n a r y //
122 d = d *2
123 q = floor ( d )
124 s = s + q /(10^ i ) ;
125 if d >=1 then
126 d =d -1
127 end
128 end
129 k=b+s;
130 disp ( ’ t h e a d d i t i o n o f two b i n a r y numbers i s ’ )
131 disp ( k ) ; // d i s p l a y i n g t h e r e s u l t //
1 // a d d i t i o n o f two b i n a r y numbers //
2 // e x a m p l e 1 4 . c //
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s / / ;
7 // a d d i t i o n o f two b i n a r y numbers //
8 // d e c i m a l e q u i v a l e n t o f t h e f i r s t b i n a r y number //
9 p =1;
10 // i n i t i a l i s i n g //
11 q =1;
12 z =0;
26
13 b =0;
14 w =0;
15 f =0;
16 // b i n =1 s t i n p u t ( E n t e r t h e b i n a r y no t o be
converted to i t s decimal e q u i v a l e n t : )
17 // a c c e p t i n g t h e b i n a r y i n p u t from u s e r //
18 bin =1100.011;
19 d = modulo ( bin ,1) ;
20 // s e p a r a t i n g t h e d e c i m a l p a r t and t h e i n t e g e r p a r t //
21 d = d *10^10;
22 a = floor ( bin ) ;
23 // r e m o v i n g t h e d e c i m a l p a r t //
24 while ( a >0)
25 // Loop t o t a k e t h e b i n a r y b i t s o f i n t e g e r i n t o a
m a t r i x //
26 r = modulo ( a ,10) ;
27 b (1 , q ) = r ;
28 a = a /10;
29 a = floor ( a ) ;
30 q = q +1;
31 end
32 for m =1: q -1
33 // m u l t i p l i y i n g t h e b i t s o f i n t e g e r p o s i t i o n v a l u e s
and a d d i n g //
34 c = m -1;
35 f = f + b (1 , m ) *(2^ c ) ;
36 end
37 while ( d >0)
38 // Loop t o t a k e t h e b i n a r y b i t s o f d e c i m a l i n t o a
m a t r i x //
39 e = modulo ( d ,2)
40 w (1 ,p ) = e
41 d = d /10;
42 d = floor ( d )
43 p = p +1;
44 end
45 for n =1: p -1
46 // m u l t i p l i y i n g t h e b i t s o f d e c i m a l w i t h t h e i r
27
p o s i t i o n v a l u e s and a d d i n g //
47 z = z + w (1 ,n ) *(0.5) ^(11 -n ) ;
48 end
49 z = z *10000;
50 // r o u n d i n g o f t o 4 d e c i m a l v a l u e s //
51 z = round ( z ) ;
52 z = z /10000;
53 x1 = f + z ;
54 disp ( ’ The D e c i m a l e q u i v a l e n t o f t h e f i r s t b i n a r y
number g i v e n i s ’ ) ;
55 disp ( x1 ) ;
56 // t o f i n d d e c i m a l e q u i v a l e n t o f s e c o n d b i n a r y number
//
57 // b i n =2nd i n p u t ( E n t e r t h e b i n a r y no t o be
converted to i t s decimal e q u i v a l e n t : )
58 p =1;
59 // i n i t i a l i s i n g //
60 q =1;
61 z =0;
62 b =0;
63 w =0;
64 f =0;
65 // a c c e p t i n g t h e b i n a r y i n p u t from u s e r //
66 bin =1011.011;
67 d = modulo ( bin ,1) ;
68 // s e p a r a t i n g t h e d e c i m a l p a r t and t h e i n t e g e r p a r t //
69 d = d *10^10;
70 a = floor ( bin ) ;
71 // r e m o v i n g t h e d e c i m a l p a r t //
72 while ( a >0)
73 // Loop t o t a k e t h e b i n a r y b i t s o f i n t e g e r i n t o a
m a t r i x //
74 r = modulo ( a ,10) ;
75 b (1 , q ) = r ;
76 a = a /10;
77 a = floor ( a ) ;
78 q = q +1;
79 end
28
80 for m =1: q -1
81 // m u l t i p l i y i n g t h e b i t s o f i n t e g e r p o s i t i o n v a l u e s
and a d d i n g //
82 c = m -1;
83 f = f + b (1 , m ) *(2^ c ) ;
84 end
85 while ( d >0)
86 // Loop t o t a k e t h e b i n a r y b i t s o f d e c i m a l i n t o a
m a t r i x //
87 e = modulo ( d ,2)
88 w (1 ,p ) = e
89 d = d /10;
90 d = floor ( d )
91 p = p +1;
92 end
93 for n =1: p -1
94 // m u l t i p l i y i n g t h e b i t s o f d e c i m a l w i t h t h e i r
p o s i t i o n v a l u e s and a d d i n g //
95 z = z + w (1 ,n ) *(0.5) ^(11 -n ) ;
96 end
97 z = z *10000;
98 // r o u n d i n g o f t o 4 d e c i m a l v a l u e s //
99 z = round ( z ) ;
100 z = z /10000;
101 x2 = f + z ;
102 disp ( ’ The D e c i m a l e q u i v a l e n t o f t h e s e c o n d b i n a r y
number g i v e n i s ’ ) ;
103 disp ( x2 ) ;
104 res = x1 + x2 ; // a d d i t i o n //
105 q =0; // b i n a r y c o n v e r s i o n o f t h e d e c i m a l a n s w e r b e g i n s
h e r e //
106 b =0;
107 s =0;
108 // e n t e r t h e d e c i m a l number t o be c o n v e r t e d //
109 a = res ;
110 d = modulo (a ,1) ; // s e p a r a t i n g d e c i m a l and i n t e g e r p a r t
//
111 a = floor ( a ) ; // r e m o v i n g d e c i m a l p a r t //
29
112 while (a >0)
113 // t a k i n g i n t e g e r p a r t i n t o a m a t r i x and c o n v e r t i n g
t o e q u i v a l e n t b i n a r y //
114 x = modulo (a ,2)
115 b = b +(10^ q ) * x
116 a = a /2
117 a = floor ( a )
118 q = q +1
119 end
120 for i =1:10
121 // f o r v a l u e s a f t e r d e c i m a l t h a t s h o u l d be c o n v e r t e d
t o b i n a r y //
122 d = d *2
123 q = floor ( d )
124 s = s + q /(10^ i ) ;
125 if d >=1 then
126 d =d -1
127 end
128 end
129 k=b+s;
130 disp ( ’ t h e a d d i t i o n o f two numbers g i v e : ’ )
131 disp ( k ) ; // d i s p l a y i n g t h e r e s u l t //
1 // s u b t r a c t i o n o f two b i n a r y numbers //
2 // e x a m p l e 1 5 . a //
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s / / ;
7 // d e c i m a l e q u i v a l e n t o f t h e f i r s t b i n a r y number //
8 p =1;
9 // i n i t i a l i s i n g //
10 q =1;
30
11 z =0;
12 b =0;
13 w =0;
14 f =0;
15 // b i n =1 s t i n p u t ( E n t e r t h e b i n a r y no t o be
converted to i t s decimal e q u i v a l e n t : )
16 // a c c e p t i n g t h e b i n a r y i n p u t from u s e r //
17 bin1 =110.01;
18 d = modulo ( bin1 ,1) ;
19 // s e p a r a t i n g t h e d e c i m a l p a r t and t h e i n t e g e r p a r t //
20 d = d *10^10;
21 a = floor ( bin1 ) ;
22 // r e m o v i n g t h e d e c i m a l p a r t //
23 while ( a >0)
24 // Loop t o t a k e t h e b i n a r y b i t s o f i n t e g e r i n t o a
m a t r i x //
25 r = modulo ( a ,10) ;
26 b (1 , q ) = r ;
27 a = a /10;
28 a = floor ( a ) ;
29 q = q +1;
30 end
31 for m =1: q -1
32 // m u l t i p l i y i n g t h e b i t s o f i n t e g e r p o s i t i o n v a l u e s
and a d d i n g //
33 c = m -1;
34 f = f + b (1 , m ) *(2^ c ) ;
35 end
36 while ( d >0)
37 // Loop t o t a k e t h e b i n a r y b i t s o f d e c i m a l i n t o a
m a t r i x //
38 e = modulo ( d ,2)
39 w (1 ,p ) = e
40 d = d /10;
41 d = floor ( d )
42 p = p +1;
43 end
44 for n =1: p -1
31
45 // m u l t i p l i y i n g t h e b i t s o f d e c i m a l w i t h t h e i r
p o s i t i o n v a l u e s and a d d i n g //
46 z = z + w (1 ,n ) *(0.5) ^(11 -n ) ;
47 end
48 z = z *10000;
49 // r o u n d i n g o f t o 4 d e c i m a l v a l u e s //
50 z = round ( z ) ;
51 z = z /10000;
52 x1 = f + z ;
53 disp ( ’ The D e c i m a l e q u i v a l e n t o f t h e f i r s t b i n a r y
number g i v e n i s ’ ) ;
54 disp ( x1 ) ;
55 // t o f i n d d e c i m a l e q u i v a l e n t o f s e c o n d b i n a r y number
//
56 // b i n =2nd i n p u t ( E n t e r t h e b i n a r y no t o be
converted to i t s decimal e q u i v a l e n t : )
57 p =1;
58 // i n i t i a l i s i n g //
59 q =1;
60 z =0;
61 b =0;
62 w =0;
63 f =0;
64 // a c c e p t i n g t h e b i n a r y i n p u t from u s e r //
65 bin2 =100.10;
66 d = modulo ( bin2 ,1) ;
67 // s e p a r a t i n g t h e d e c i m a l p a r t and t h e i n t e g e r p a r t //
68 d = d *10^10;
69 a = floor ( bin2 ) ;
70 // r e m o v i n g t h e d e c i m a l p a r t //
71 while ( a >0)
72 // Loop t o t a k e t h e b i n a r y b i t s o f i n t e g e r i n t o a
m a t r i x //
73 r = modulo ( a ,10) ;
74 b (1 , q ) = r ;
75 a = a /10;
76 a = floor ( a ) ;
77 q = q +1;
32
78 end
79 for m =1: q -1
80 // m u l t i p l i y i n g t h e b i t s o f i n t e g e r p o s i t i o n v a l u e s
and a d d i n g //
81 c = m -1;
82 f = f + b (1 , m ) *(2^ c ) ;
83 end
84 while ( d >0)
85 // Loop t o t a k e t h e b i n a r y b i t s o f d e c i m a l i n t o a
m a t r i x //
86 e = modulo ( d ,2)
87 w (1 ,p ) = e
88 d = d /10;
89 d = floor ( d )
90 p = p +1;
91 end
92 for n =1: p -1
93 // m u l t i p l i y i n g t h e b i t s o f d e c i m a l w i t h t h e i r
p o s i t i o n v a l u e s and a d d i n g //
94 z = z + w (1 ,n ) *(0.5) ^(11 -n ) ;
95 end
96 z = z *10000;
97 // r o u n d i n g o f t o 4 d e c i m a l v a l u e s //
98 z = round ( z ) ;
99 z = z /10000;
100 x2 = f + z ;
101 disp ( ’ The D e c i m a l e q u i v a l e n t o f t h e s e c o n d b i n a r y
number g i v e n i s ’ ) ;
102 disp ( x2 ) ;
103 res = x1 - x2 ; // s u b t r a c t i o n //
104 q =0; // b i n a r y c o n v e r s i o n o f t h e d e c i m a l a n s w e r b e g i n s
h e r e //
105 b =0;
106 s =0;
107 // e n t e r t h e d e c i m a l number t o be c o n v e r t e d //
108 a = res ;
109 d = modulo (a ,1) ; // s e p a r a t i n g d e c i m a l and i n t e g e r p a r t
//
33
110 a = floor ( a ) ; // r e m o v i n g d e c i m a l p a r t //
111 while (a >0)
112 // t a k i n g i n t e g e r p a r t i n t o a m a t r i x and c o n v e r t i n g
t o e q u i v a l e n t b i n a r y //
113 x = modulo (a ,2)
114 b = b +(10^ q ) * x
115 a = a /2
116 a = floor ( a )
117 q = q +1
118 end
119 for i =1:10
120 // f o r v a l u e s a f t e r d e c i m a l t h a t s h o u l d be c o n v e r t e d
t o b i n a r y //
121 d = d *2
122 q = floor ( d )
123 s = s + q /(10^ i ) ;
124 if d >=1 then
125 d =d -1
126 end
127 end
128 k=b+s;
129 disp ( ’ t h e s u b t r a c t i o n o f two numbers g i v e : ’ )
130 disp ( k ) ; // d i s p l a y i n g t h e r e s u l t //
1 // s u b t r a c t i o n o f two b i n a r y numbers //
2 // e x a m p l e 1 5 . b //
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s / / ; / / d e c i m a l e q u i v a l e n t o f t h e first
b i n a r y number //
7 p =1;
8 // i n i t i a l i s i n g //
34
9 q =1;
10 z =0;
11 b =0;
12 w =0;
13 f =0;
14 // b i n =1 s t i n p u t ( E n t e r t h e b i n a r y no t o be
converted to i t s decimal e q u i v a l e n t : )
15 // a c c e p t i n g t h e b i n a r y i n p u t from u s e r //
16 bin =11.01111;
17 d = modulo ( bin ,1) ;
18 // s e p a r a t i n g t h e d e c i m a l p a r t and t h e i n t e g e r p a r t //
19 d = d *10^10;
20 a = floor ( bin ) ;
21 // r e m o v i n g t h e d e c i m a l p a r t //
22 while ( a >0)
23 // Loop t o t a k e t h e b i n a r y b i t s o f i n t e g e r i n t o a
m a t r i x //
24 r = modulo ( a ,10) ;
25 b (1 , q ) = r ;
26 a = a /10;
27 a = floor ( a ) ;
28 q = q +1;
29 end
30 for m =1: q -1
31 // m u l t i p l i y i n g t h e b i t s o f i n t e g e r p o s i t i o n v a l u e s
and a d d i n g //
32 c = m -1;
33 f = f + b (1 , m ) *(2^ c ) ;
34 end
35 while ( d >0)
36 // Loop t o t a k e t h e b i n a r y b i t s o f d e c i m a l i n t o a
m a t r i x //
37 e = modulo ( d ,2)
38 w (1 ,p ) = e
39 d = d /10;
40 d = floor ( d )
41 p = p +1;
42 end
35
43 for n =1: p -1
44 // m u l t i p l i y i n g t h e b i t s o f d e c i m a l w i t h t h e i r
p o s i t i o n v a l u e s and a d d i n g //
45 z = z + w (1 ,n ) *(0.5) ^(11 -n ) ;
46 end
47 z = z *10000;
48 // r o u n d i n g o f t o 4 d e c i m a l v a l u e s //
49 z = round ( z ) ;
50 z = z /10000;
51 x1 = f + z ;
52 disp ( ’ The D e c i m a l e q u i v a l e n t o f t h e f i r s t b i n a r y
number g i v e n i s ’ ) ;
53 disp ( x1 ) ;
54 // t o f i n d d e c i m a l e q u i v a l e n t o f s e c o n d b i n a r y number
//
55 // b i n =2nd i n p u t ( E n t e r t h e b i n a r y no t o be
converted to i t s decimal e q u i v a l e n t : )
56 p =1;
57 // i n i t i a l i s i n g //
58 q =1;
59 z =0;
60 b =0;
61 w =0;
62 f =0;
63 // a c c e p t i n g t h e b i n a r y i n p u t from u s e r //
64 bin =10.01001;
65 d = modulo ( bin ,1) ;
66 // s e p a r a t i n g t h e d e c i m a l p a r t and t h e i n t e g e r p a r t //
67 d = d *10^10;
68 a = floor ( bin ) ;
69 // r e m o v i n g t h e d e c i m a l p a r t //
70 while ( a >0)
71 // Loop t o t a k e t h e b i n a r y b i t s o f i n t e g e r i n t o a
m a t r i x //
72 r = modulo ( a ,10) ;
73 b (1 , q ) = r ;
74 a = a /10;
75 a = floor ( a ) ;
36
76 q = q +1;
77 end
78 for m =1: q -1
79 // m u l t i p l i y i n g t h e b i t s o f i n t e g e r p o s i t i o n v a l u e s
and a d d i n g //
80 c = m -1;
81 f = f + b (1 , m ) *(2^ c ) ;
82 end
83 while ( d >0)
84 // Loop t o t a k e t h e b i n a r y b i t s o f d e c i m a l i n t o a
m a t r i x //
85 e = modulo ( d ,2)
86 w (1 ,p ) = e
87 d = d /10;
88 d = floor ( d )
89 p = p +1;
90 end
91 for n =1: p -1
92 // m u l t i p l i y i n g t h e b i t s o f d e c i m a l w i t h t h e i r
p o s i t i o n v a l u e s and a d d i n g //
93 z = z + w (1 ,n ) *(0.5) ^(11 -n ) ;
94 end
95 z = z *10000;
96 // r o u n d i n g o f t o 4 d e c i m a l v a l u e s //
97 z = round ( z ) ;
98 z = z /10000;
99 x2 = f + z ;
100 disp ( ’ The D e c i m a l e q u i v a l e n t o f t h e s e c o n d b i n a r y
number g i v e n i s ’ ) ;
101 disp ( x2 ) ;
102 res = x1 - x2 ; // s u b t r a c t i o n //
103 q =0; // b i n a r y c o n v e r s i o n o f t h e d e c i m a l a n s w e r b e g i n s
h e r e //
104 b =0;
105 s =0;
106 // e n t e r t h e d e c i m a l number t o be c o n v e r t e d //
107 a = res ;
108 d = modulo (a ,1) ; // s e p a r a t i n g d e c i m a l and i n t e g e r p a r t
37
//
109 a = floor ( a ) ; // r e m o v i n g d e c i m a l p a r t //
110 while (a >0)
111 // t a k i n g i n t e g e r p a r t i n t o a m a t r i x and c o n v e r t i n g
t o e q u i v a l e n t b i n a r y //
112 x = modulo (a ,2)
113 b = b +(10^ q ) * x
114 a = a /2
115 a = floor ( a )
116 q = q +1
117 end
118 for i =1:10
119 // f o r v a l u e s a f t e r d e c i m a l t h a t s h o u l d be c o n v e r t e d
t o b i n a r y //
120 d = d *2
121 q = floor ( d )
122 s = s + q /(10^ i ) ;
123 if d >=1 then
124 d =d -1
125 end
126 end
127 k=b+s;
128 disp ( ’ t h e s u b t r a c t i o n o f two numbers g i v e : ’ )
129 disp ( k ) ; // d i s p l a y i n g t h e r e s u l t //
1 // m u l t i p l i c a t i o n o f two b i n a r y numbers //
2 // e x a m p l e 1 6 . a //
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s / / ; / / d e c i m a l e q u i v a l e n t o f t h e first
b i n a r y number //
7 p =1;
38
8 // i n i t i a l i s i n g //
9 q =1;
10 z =0;
11 b =0;
12 w =0;
13 f =0;
14 // b i n =1 s t i n p u t ( E n t e r t h e b i n a r y no t o be
converted to i t s decimal e q u i v a l e n t : )
15 // a c c e p t i n g t h e b i n a r y i n p u t from u s e r //
16 bin =1.01;
17 d = modulo ( bin ,1) ;
18 // s e p a r a t i n g t h e d e c i m a l p a r t and t h e i n t e g e r p a r t //
19 d = d *10^10;
20 a = floor ( bin ) ;
21 // r e m o v i n g t h e d e c i m a l p a r t //
22 while ( a >0)
23 // Loop t o t a k e t h e b i n a r y b i t s o f i n t e g e r i n t o a
m a t r i x //
24 r = modulo ( a ,10) ;
25 b (1 , q ) = r ;
26 a = a /10;
27 a = floor ( a ) ;
28 q = q +1;
29 end
30 for m =1: q -1
31 // m u l t i p l i y i n g t h e b i t s o f i n t e g e r p o s i t i o n v a l u e s
and a d d i n g //
32 c = m -1;
33 f = f + b (1 , m ) *(2^ c ) ;
34 end
35 while ( d >0)
36 // Loop t o t a k e t h e b i n a r y b i t s o f d e c i m a l i n t o a
m a t r i x //
37 e = modulo ( d ,2)
38 w (1 ,p ) = e
39 d = d /10;
40 d = floor ( d )
41 p = p +1;
39
42 end
43 for n =1: p -1
44 // m u l t i p l i y i n g t h e b i t s o f d e c i m a l w i t h t h e i r
p o s i t i o n v a l u e s and a d d i n g //
45 z = z + w (1 ,n ) *(0.5) ^(11 -n ) ;
46 end
47 z = z *10000;
48 // r o u n d i n g o f t o 4 d e c i m a l v a l u e s //
49 z = round ( z ) ;
50 z = z /10000;
51 x1 = f + z ;
52 disp ( ’ The D e c i m a l e q u i v a l e n t o f t h e f i r s t b i n a r y
number g i v e n i s ’ ) ;
53 disp ( x1 ) ;
54 // t o f i n d d e c i m a l e q u i v a l e n t o f s e c o n d b i n a r y number
//
55 // b i n =2nd i n p u t ( E n t e r t h e b i n a r y no t o be
converted to i t s decimal e q u i v a l e n t : )
56 p =1;
57 // i n i t i a l i s i n g //
58 q =1;
59 z =0;
60 b =0;
61 w =0;
62 f =0;
63 // a c c e p t i n g t h e b i n a r y i n p u t from u s e r //
64 bin =10.1;
65 d = modulo ( bin ,1) ;
66 // s e p a r a t i n g t h e d e c i m a l p a r t and t h e i n t e g e r p a r t //
67 d = d *10^10;
68 a = floor ( bin ) ;
69 // r e m o v i n g t h e d e c i m a l p a r t //
70 while ( a >0)
71 // Loop t o t a k e t h e b i n a r y b i t s o f i n t e g e r i n t o a
m a t r i x //
72 r = modulo ( a ,10) ;
73 b (1 , q ) = r ;
74 a = a /10;
40
75 a = floor ( a ) ;
76 q = q +1;
77 end
78 for m =1: q -1
79 // m u l t i p l i y i n g t h e b i t s o f i n t e g e r p o s i t i o n v a l u e s
and a d d i n g //
80 c = m -1;
81 f = f + b (1 , m ) *(2^ c ) ;
82 end
83 while ( d >0)
84 // Loop t o t a k e t h e b i n a r y b i t s o f d e c i m a l i n t o a
m a t r i x //
85 e = modulo ( d ,2)
86 w (1 ,p ) = e
87 d = d /10;
88 d = floor ( d )
89 p = p +1;
90 end
91 for n =1: p -1
92 // m u l t i p l i y i n g t h e b i t s o f d e c i m a l w i t h t h e i r
p o s i t i o n v a l u e s and a d d i n g //
93 z = z + w (1 ,n ) *(0.5) ^(11 -n ) ;
94 end
95 z = z *10000;
96 // r o u n d i n g o f t o 4 d e c i m a l v a l u e s //
97 z = round ( z ) ;
98 z = z /10000;
99 x2 = f + z ;
100 disp ( ’ The D e c i m a l e q u i v a l e n t o f t h e s e c o n d b i n a r y
number g i v e n i s ’ ) ;
101 disp ( x2 ) ;
102 res = x1 * x2 ; // m u l t i p l i c a t i o n //
103 q =0; // b i n a r y c o n v e r s i o n o f t h e d e c i m a l a n s w e r b e g i n s
h e r e //
104 b =0;
105 s =0;
106 // e n t e r t h e d e c i m a l number t o be c o n v e r t e d //
107 a = res ;
41
108 d = modulo (a ,1) ; // s e p a r a t i n g d e c i m a l and i n t e g e r part
//
109 a = floor ( a ) ; // r e m o v i n g d e c i m a l p a r t //
110 while (a >0)
111 // t a k i n g i n t e g e r p a r t i n t o a m a t r i x and c o n v e r t i n g
t o e q u i v a l e n t b i n a r y //
112 x = modulo (a ,2)
113 b = b +(10^ q ) * x
114 a = a /2
115 a = floor ( a )
116 q = q +1
117 end
118 for i =1:10
119 // f o r v a l u e s a f t e r d e c i m a l t h a t s h o u l d be c o n v e r t e d
t o b i n a r y //
120 d = d *2
121 q = floor ( d )
122 s = s + q /(10^ i ) ;
123 if d >=1 then
124 d =d -1
125 end
126 end
127 k=b+s;
128 disp ( ’ t h e m u l t i p l i c a t i o n o f two numbers g i v e : ’ )
129 disp ( k ) ; // d i s p l a y i n g t h e r e s u l t //
1 // m u l t i p l i c a t i o n o f two b i n a r y numbers //
2 // e x a m p l e 1 6 . b //
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s / / ;
7 // d e c i m a l e q u i v a l e n t o f t h e f i r s t b i n a r y number //
42
8 p =1;
9 // i n i t i a l i s i n g //
10 q =1;
11 z =0;
12 b =0;
13 w =0;
14 f =0;
15 // b i n =1 s t i n p u t ( E n t e r t h e b i n a r y no t o be
converted to i t s decimal e q u i v a l e n t : )
16 // a c c e p t i n g t h e b i n a r y i n p u t from u s e r //
17 bin =101.01;
18 d = modulo ( bin ,1) ;
19 // s e p a r a t i n g t h e d e c i m a l p a r t and t h e i n t e g e r p a r t //
20 d = d *10^10;
21 a = floor ( bin ) ;
22 // r e m o v i n g t h e d e c i m a l p a r t //
23 while ( a >0)
24 // Loop t o t a k e t h e b i n a r y b i t s o f i n t e g e r i n t o a
m a t r i x //
25 r = modulo ( a ,10) ;
26 b (1 , q ) = r ;
27 a = a /10;
28 a = floor ( a ) ;
29 q = q +1;
30 end
31 for m =1: q -1
32 // m u l t i p l i y i n g t h e b i t s o f i n t e g e r p o s i t i o n v a l u e s
and a d d i n g //
33 c = m -1;
34 f = f + b (1 , m ) *(2^ c ) ;
35 end
36 while ( d >0)
37 // Loop t o t a k e t h e b i n a r y b i t s o f d e c i m a l i n t o a
m a t r i x //
38 e = modulo ( d ,2)
39 w (1 ,p ) = e
40 d = d /10;
41 d = floor ( d )
43
42 p = p +1;
43 end
44 for n =1: p -1
45 // m u l t i p l i y i n g t h e b i t s o f d e c i m a l w i t h t h e i r
p o s i t i o n v a l u e s and a d d i n g //
46 z = z + w (1 ,n ) *(0.5) ^(11 -n ) ;
47 end
48 z = z *10000;
49 // r o u n d i n g o f t o 4 d e c i m a l v a l u e s //
50 z = round ( z ) ;
51 z = z /10000;
52 x1 = f + z ;
53 disp ( ’ The D e c i m a l e q u i v a l e n t o f t h e f i r s t b i n a r y
number g i v e n i s ’ ) ;
54 disp ( x1 ) ;
55 // t o f i n d d e c i m a l e q u i v a l e n t o f s e c o n d b i n a r y number
//
56 // b i n =2nd i n p u t ( E n t e r t h e b i n a r y no t o be
converted to i t s decimal e q u i v a l e n t : )
57 // a c c e p t i n g t h e b i n a r y i n p u t from u s e r //
58 bin = ’ 11 ’ ;
59 x2 = bin2dec ( bin )
60 disp ( ’ The D e c i m a l e q u i v a l e n t o f t h e s e c o n d b i n a r y
number g i v e n i s ’ ) ;
61 disp ( x2 ) ;
62 res = x1 * x2 ; // m u l t i p l i c a t i o n //
63 q =0; // b i n a r y c o n v e r s i o n o f t h e d e c i m a l a n s w e r b e g i n s
h e r e //
64 b =0;
65 s =0;
66 // e n t e r t h e d e c i m a l number t o be c o n v e r t e d //
67 a = res ;
68 d = modulo (a ,1) ; // s e p a r a t i n g d e c i m a l and i n t e g e r p a r t
//
69 a = floor ( a ) ; // r e m o v i n g d e c i m a l p a r t //
70 while (a >0)
71 // t a k i n g i n t e g e r p a r t i n t o a m a t r i x and c o n v e r t i n g
t o e q u i v a l e n t b i n a r y //
44
72 x = modulo (a ,2)
73 b = b +(10^ q ) * x
74 a = a /2
75 a = floor ( a )
76 q = q +1
77 end
78 for i =1:10
79 // f o r v a l u e s a f t e r d e c i m a l t h a t s h o u l d be c o n v e r t e d
t o b i n a r y //
80 d = d *2
81 q = floor ( d )
82 s = s + q /(10^ i ) ;
83 if d >=1 then
84 d =d -1
85 end
86 end
87 k=b+s;
88 disp ( ’ t h e m u l t i p l i c a t i o n o f two numbers g i v e : ’ )
89 disp ( k ) ; // d i s p l a y i n g t h e r e s u l t //
1 // d i v i s i o n o f two b i n a r y numbers //
2 // e x a m p l e 1 7 . a //
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s / / ; / / d e c i m a l e q u i v a l e n t o f t h e first
b i n a r y number //
7 // b i n =1 s t i n p u t ( E n t e r t h e b i n a r y no t o be
converted to i t s decimal e q u i v a l e n t : )
8 // a c c e p t i n g t h e b i n a r y i n p u t from u s e r //
9 bin = ’ 1 1 1 1 0 0 1 ’ ; // 1 s t i n p u t //
10 x1 = bin2dec ( bin )
11 bin = ’ 1 0 0 1 ’ ; // 2 nd i n p u t //
45
12 x2 = bin2dec ( bin )
13 res = x1 / x2 ; // d i v i s i o n //
14 q =0;
15 b =0;
16 s =0;
17 // e n t e r t h e d e c i m a l number t o be c o n v e r t e d //
18 a = res ;
19 d = modulo (a ,1) ; // s e p a r a t i n g d e c i m a l and i n t e g e r p a r t
//
20 a = floor ( a ) ; // r e m o v i n g d e c i m a l p a r t //
21 while (a >0)
22 // t a k i n g i n t e g e r p a r t i n t o a m a t r i x and c o n v e r t i n g
t o e q u i v a l e n t b i n a r y //
23 x = modulo (a ,2)
24 b = b +(10^ q ) * x
25 a = a /2
26 a = floor ( a )
27 q = q +1
28 end
29 for i =1:10
30 // f o r v a l u e s a f t e r d e c i m a l t h a t s h o u l d be c o n v e r t e d
t o b i n a r y //
31 d = d *2
32 q = floor ( d )
33 s = s + q /(10^ i ) ;
34 if d >=1 then
35 d =d -1
36 end
37 end
38 k=b+s;
39 disp ( ’ t h e d i v i s i o n o f two numbers g i v e : ’ )
40 disp ( k ) ; // d i s p l a y i n g t h e r e s u l t //
46
1 // d i v i s i o n o f two b i n a r y numbers //
2 // e x a m p l e 1 7 . b //
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s / / ; / / d e c i m a l e q u i v a l e n t o f t h e first
b i n a r y number //
7 p =1;
8 // i n i t i a l i s i n g //
9 q =1;
10 z =0;
11 b =0;
12 w =0;
13 f =0;
14 // b i n= i n p u t ( E n t e r t h e b i n a r y no t o be c o n v e r t e d
to i t s decimal e q u i v a l e n t : )
15 // a c c e p t i n g t h e b i n a r y i n p u t from u s e r //
16 bin =11.11;
17 d = modulo ( bin ,1) ;
18 // s e p a r a t i n g t h e d e c i m a l p a r t and t h e i n t e g e r p a r t //
19 d = d *10^10;
20 a = floor ( bin ) ;
21 // r e m o v i n g t h e d e c i m a l p a r t //
22 while ( a >0)
23 // Loop t o t a k e t h e b i n a r y b i t s o f i n t e g e r i n t o a
m a t r i x //
24 r = modulo ( a ,10) ;
25 b (1 , q ) = r ;
26 a = a /10;
27 a = floor ( a ) ;
28 q = q +1;
29 end
30 for m =1: q -1
31 // m u l t i p l i y i n g t h e b i t s o f i n t e g e r p o s i t i o n v a l u e s
and a d d i n g //
32 c = m -1;
33 f = f + b (1 , m ) *(2^ c ) ;
34 end
47
35 while ( d >0)
36 // Loop t o t a k e t h e b i n a r y b i t s o f d e c i m a l i n t o a
m a t r i x //
37 e = modulo ( d ,2)
38 w (1 ,p ) = e
39 d = d /10;
40 d = floor ( d )
41 p = p +1;
42 end
43 for n =1: p -1
44 // m u l t i p l i y i n g t h e b i t s o f d e c i m a l w i t h t h e i r
p o s i t i o n v a l u e s and a d d i n g //
45 z = z + w (1 ,n ) *(0.5) ^(11 -n ) ;
46 end
47 z = z *10000;
48 // r o u n d i n g o f t o 4 d e c i m a l v a l u e s //
49 z = round ( z ) ;
50 z = z /10000;
51 x1 = f + z ;
52 p =1;
53 // i n i t i a l i s i n g //
54 q =1;
55 z =0;
56 b =0;
57 w =0;
58 f =0;
59 // b i n =2nd i n p u t ( E n t e r t h e b i n a r y no t o be
converted to i t s decimal e q u i v a l e n t : )
60 // a c c e p t i n g t h e b i n a r y i n p u t from u s e r //
61 bin =.101;
62 d = modulo ( bin ,1) ;
63 // s e p a r a t i n g t h e d e c i m a l p a r t and t h e i n t e g e r p a r t //
64 d = d *10^10;
65 a = floor ( bin ) ;
66 // r e m o v i n g t h e d e c i m a l p a r t //
67 while ( a >0)
68 // Loop t o t a k e t h e b i n a r y b i t s o f i n t e g e r i n t o a
m a t r i x //
48
69 r = modulo ( a ,10) ;
70 b (1 , q ) = r ;
71 a = a /10;
72 a = floor ( a ) ;
73 q = q +1;
74 end
75 for m =1: q -1
76 // m u l t i p l i y i n g t h e b i t s o f i n t e g e r p o s i t i o n v a l u e s
and a d d i n g //
77 c = m -1;
78 f = f + b (1 , m ) *(2^ c ) ;
79 end
80 while ( d >0)
81 // Loop t o t a k e t h e b i n a r y b i t s o f d e c i m a l i n t o a
m a t r i x //
82 e = modulo ( d ,2)
83 w (1 ,p ) = e
84 d = d /10;
85 d = floor ( d )
86 p = p +1;
87 end
88 for n =1: p -1
89 // m u l t i p l i y i n g t h e b i t s o f d e c i m a l w i t h t h e i r
p o s i t i o n v a l u e s and a d d i n g //
90 z = z + w (1 ,n ) *(0.5) ^(11 -n ) ;
91 end
92 z = z *10000;
93 // r o u n d i n g o f t o 4 d e c i m a l v a l u e s //
94 z = round ( z ) ;
95 z = z /10000;
96 x2 = f + z ;
97 res = x1 / x2 ; // d i v i s i o n //
98 q =0;
99 b =0;
100 s =0;
101 // e n t e r t h e d e c i m a l number t o be c o n v e r t e d //
102 a = res ;
103 d = modulo (a ,1) ; // s e p a r a t i n g d e c i m a l and i n t e g e r p a r t
49
//
104 a = floor ( a ) ; // r e m o v i n g d e c i m a l p a r t //
105 while (a >0)
106 // t a k i n g i n t e g e r p a r t i n t o a m a t r i x and c o n v e r t i n g
t o e q u i v a l e n t b i n a r y //
107 x = modulo (a ,2)
108 b = b +(10^ q ) * x
109 a = a /2
110 a = floor ( a )
111 q = q +1
112 end
113 for i =1:10
114 // f o r v a l u e s a f t e r d e c i m a l t h a t s h o u l d be c o n v e r t e d
t o b i n a r y //
115 d = d *2
116 q = floor ( d )
117 s = s + q /(10^ i ) ;
118 if d >=1 then
119 d =d -1
120 end
121 end
122 k=b+s;
123 disp ( ’ t h e d i v i s i o n o f two numbers g i v e : ’ )
124 disp ( k ) ; // d i s p l a y i n g t h e r e s u l t //
1 // b i n a r y t o hex c o n v e r s i o n //
2 // e x a m p l e 1 8 . a //
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s //
7 x= ’ 10100110 ’
8 a = bin2dec ( x ) ; // b i n a r y t o d e c i m a l c o n v e r s i o n //
50
9 res = dec2hex ( a ) ; // d e c i m a l t o hex c o n v e r s i o n //
10 disp ( ’ t h e number i n h e x a d e c i m a l form : ’ )
11 disp ( res ) ; // a n s w e r d i s p l a y e d //
1 // d e c i m a l t o o c t a l c o n v e r s i o n //
2 // e x a m p l e 1 8 . b //
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s //
7 x =6800
8 res = dec2oct ( x ) ; // d e c i m a l t o o c t a l c o n v e r s i o n //
9 disp ( res ) ; // a n s w e r i n o c t a l form //
1 // d e c i m a l t o b i n a r y c o n v e r s i o n //
2 // e x a m p l e 1 8 . c //
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s //
7 // c o n v e r t i n g f i r s t b i n a r y i n p u t t o d e c i m a l //
8 p =1;
9 // i n i t i a l i s i n g //
10 q =1;
11 z =0;
12 b =0;
13 w =0;
14 f =0;
51
15 // b i n= i n p u t ( E n t e r t h e b i n a r y no t o be c o n v e r t e d
to i t s decimal e q u i v a l e n t : )
16 bin =.0111;
17 d = modulo ( bin ,1) ;
18 // s e p a r a t i n g t h e d e c i m a l p a r t and t h e i n t e g e r p a r t //
19 d = d *10^10;
20 a = floor ( bin ) ;
21 // r e m o v i n g t h e d e c i m a l p a r t //
22 while ( a >0)
23 // Loop t o t a k e t h e b i n a r y b i t s o f i n t e g e r i n t o a
m a t r i x //
24 r = modulo ( a ,10) ;
25 b (1 , q ) = r ;
26 a = a /10;
27 a = floor ( a ) ;
28 q = q +1;
29 end
30 for m =1: q -1
31 // m u l t i p l i y i n g t h e b i t s o f i n t e g e r p o s i t i o n v a l u e s
and a d d i n g //
32 c = m -1;
33 f = f + b (1 , m ) *(2^ c ) ;
34 end
35 while ( d >0)
36 // Loop t o t a k e t h e b i n a r y b i t s o f d e c i m a l i n t o a
m a t r i x //
37 e = modulo ( d ,2)
38 w (1 ,p ) = e
39 d = d /10;
40 d = floor ( d )
41 p = p +1;
42 end
43 for n =1: p -1
44 // m u l t i p l i y i n g t h e b i t s o f d e c i m a l w i t h t h e i r
p o s i t i o n v a l u e s and a d d i n g //
45 z = z + w (1 ,n ) *(0.5) ^(11 -n ) ;
46 end
47 z = z *10000;
52
48 // r o u n d i n g o f t o 4 d e c i m a l v a l u e s //
49 z = round ( z ) ;
50 z = z /10000;
51 d1 = f + z ;
52 disp ( ’ t h e number i n b i n a r y form : ’ )
53 disp ( d1 ) ; // d i s p l a y i n g t h e r e s u l t //
1 // d e c i m a l t o o c t a l c o n v e r s i o n //
2 // e x a m p l e 1 9 . a //
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s //
7 // d e c i m a l t o o c t a l c o n v e r s i o n //
8 q =0;
9 b =0;
10 s =0;
11 // e n t e r t h e d e c i m a l number t o be c o n v e r t e d //
12 a =4429.625;
13 d = modulo (a ,1) ; // s e p a r a t i n g d e c i m a l and i n t e g e r p a r t
//
14 a = floor ( a ) ; // r e m o v i n g d e c i m a l p a r t //
15 while (a >0)
16 // t a k i n g i n t e g e r p a r t i n t o a m a t r i x and c o n v e r t i n g
t o e q u i v a l e n t o c t a l //
17 x = modulo (a ,8)
18 b = b +(10^ q ) * x
19 a = a /8
20 a = floor ( a )
21 q = q +1
22 end
23 for i =1:10
24 // f o r v a l u e s a f t e r d e c i m a l t h a t s h o u l d be
53
c o n v e r t e d t o o c t a l //
25 d = d *8
26 q = floor ( d )
27 s = s + q /(10^ i ) ;
28 d =d - q
29 end
30 k = b + s ;
31 disp ( ’ t h e o c t a l e q u i v a l e n t of the given decimal
number i s ’ )
32 disp ( k ) ; // d i s p l a y i n g t h e r e s u l t //
1 // d e c i m a l t o o c t a l c o n v e r s i o n //
2 // e x a m p l e 1 9 . b //
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s //
7 // d e c i m a l t o o c t a l c o n v e r s i o n //
8 q =0;
9 b =0;
10 s =0;
11 // e n t e r t h e d e c i m a l number t o be c o n v e r t e d //
12 a =791.125;
13 d = modulo (a ,1) ; // s e p a r a t i n g d e c i m a l and i n t e g e r p a r t
//
14 a = floor ( a ) ; // r e m o v i n g d e c i m a l p a r t //
15 while (a >0)
16 // t a k i n g i n t e g e r p a r t i n t o a m a t r i x and c o n v e r t i n g
t o e q u i v a l e n t o c t a l //
17 x = modulo (a ,8)
18 b = b +(10^ q ) * x
19 a = a /8
20 a = floor ( a )
54
21 q = q +1
22 end
23 for i =1:10
24 // f o r v a l u e s a f t e r d e c i m a l t h a t s h o u l d be
c o n v e r t e d t o o c t a l //
25 d = d *8
26 q = floor ( d )
27 s = s + q /(10^ i ) ;
28 d =d - q
29 end
30 k=b+s;
31 disp ( ’ t h e o c t a l e q u i v a l e n t o f t h e g i v e n d e c i m a l
number i s ’ )
32 disp ( k ) ; // d i s p l a y i n g t h e r e s u l t //
1 // d e c i m a l t o o c t a l c o n v e r s i o n //
2 // e x a m p l e 1 9 . c //
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s //
7 // d e c i m a l t o o c t a l c o n v e r s i o n //
8 q =0;
9 b =0;
10 s =0;
11 // e n t e r t h e d e c i m a l number t o be c o n v e r t e d //
12 a =11.9375;
13 d = modulo (a ,1) ; // s e p a r a t i n g d e c i m a l and i n t e g e r p a r t
//
14 a = floor ( a ) ; // r e m o v i n g d e c i m a l p a r t //
15 while (a >0)
16 // t a k i n g i n t e g e r p a r t i n t o a m a t r i x and c o n v e r t i n g
t o e q u i v a l e n t o c t a l //
55
17 x = modulo (a ,8)
18 b = b +(10^ q ) * x
19 a = a /8
20 a = floor ( a )
21 q = q +1
22 end
23 for i =1:10
24 // f o r v a l u e s a f t e r d e c i m a l t h a t s h o u l d be
c o n v e r t e d t o o c t a l //
25 d = d *8
26 q = floor ( d )
27 s = s + q /(10^ i ) ;
28 d =d - q
29 end
30 k=b+s;
31 disp ( ’ t h e o c t a l e q u i v a l e n t o f t h e g i v e n d e c i m a l
number i s ’ )
32 disp ( k ) ; // d i s p l a y i n g t h e r e s u l t //
1 // d e c i m a l t o s i g n e d b i n a r y number //
2 // e x a m p l e 2 0 . a //
3 // d e c i m a l t o s i g n e d b i n a r y number s y s t e m //
4 clc
5 // c l e a r s t h e command window //
6 clear
7 // c l e a r s //
8 x =+29
9 a = dec2bin (x ,6)
10 // d e c i m a l t o b i n a r y c o n v e r s i o n //
11 disp ( ’ t h e a n s w e r i s ’ )
12 disp ( a )
13 // s i n c e t h e number i s p o s i t i v e i t s t a r t s w i t h a 0//
56
Scilab code Exa 1.20.b decimal to signed binary system conversion
1 // d e c i m a l t o s i g n e d b i n a r y number //
2 // e x a m p l e 2 0 . b //
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s //
7 // d e c i m a l t o s i g n e d b i n a r y number s y s t e m //
8 x = -29
9 a = dec2bin ( - x )
10 // d e c i m a l t o b i n a r y c o n v e r s i o n //
11 a = dec2bin ( - x + bin2dec ( ’ 1 0 0 0 0 0 ’ ) )
12 disp ( ’ t h e a n s w e r i s : ’ )
13 disp ( a )
14 // s i n c e t h e number i s n e g a t i v e i t s t a r t s w i t h a 1//
1 // d e c i m a l t o s i g n e d b i n a r y number //
2 // e x a m p l e 2 0 . c //
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s //
7 // d e c i m a l t o s i g n e d b i n a r y number s y s t e m //
8 x = -19
9 a = dec2bin ( - x )
10 // d e c i m a l t o b i n a r y c o n v e r s i o n //
11 a = dec2bin ( - x + bin2dec ( ’ 1 0 0 0 0 0 ’ ) )
12 disp ( ’ t h e a n s w e r i s : ’ )
13 disp ( a )
57
14 // s i n c e t h e number i s n e g a t i v e i t s t a r t s w i t h a 1 ,
msb i n d i c a t e s t h e s i g n //
1 // s u b t r a c t i o n u s i n g 1 ’ s complement //
2 // Example 2 1 . a //
3 // s u b t r a c t i o n i n one ’ s complement u s i n g 8 b i t s //
4 clc
5 // c l e a r s t h e window //
6 clear
7 // c l e a r s a l l t h e e x i s t i n g v a r i a b l e s //
8 x= ’ 11001 ’
9 y = bin2dec ( ’ 1 1 0 0 1 ’ )
10 z = bin2dec ( ’ 1 0 1 1 0 ’ )
11 c =( bitcmp (z ,5) ) ; // f i n d i n g 1 ’ s complement //
12 a = y + c +1
13 a =a - bin2dec ( ’ 1 0 0 0 0 0 ’ )
14 a = dec2bin (a ,5) ; // c o n v e r t i n g t h e r e s u l t t o b i n a r y
e q u i v a l e n t //
15 disp ( ’ t h e s u b t r a c t i o n o f two numbers g i v e : ’ )
16 disp ( a ) ; // d i s p l a y i n g t h e r e s u l t //
1 // s u b t r a c t i o n u s i n g 1 ’ s complement //
2 // Example 2 1 . b //
3 // s u b t r a c t i o n i n one ’ s complement u s i n g 8 b i t s //
4 clc
5 // c l e a r s t h e window //
6 clear
7 // c l e a r s a l l t h e e x i s t i n g v a r i a b l e s //
8 x= ’ 1011 ’
58
9 y = bin2dec ( ’ 1 0 1 1 ’ )
10 z = bin2dec ( ’ 0 1 0 1 ’ )
11 c =( bitcmp (z ,4) ) ; // f i n d i n g 1 ’ s complement //
12 a = y + c +1
13 a =a - bin2dec ( ’ 1 0 0 0 0 ’ ) ;
14 a = dec2bin (a ,4) ; // c o n v e r t i n g t h e r e s u l t t o b i n a r y
e q u i v a l e n t //
15 disp ( ’ t h e s u b t r a c t i o n o f two b i n a r y numbers g i v e : ’ )
16 disp ( a ) ; // d i s p l a y i n g t h e r e s u l t //
1 // s u b t r a c t i o n u s i n g 1 ’ s complement //
2 // Example 2 1 . c //
3 // s u b t r a c t i o n i n one ’ s complement u s i n g 8 b i t s //
4 clc
5 // c l e a r s t h e window //
6 clear
7 // c l e a r s a l l t h e e x i s t i n g v a r i a b l e s //
8 x= ’ 11011 ’
9 y = bin2dec ( ’ 1 1 0 1 1 ’ )
10 z = bin2dec ( ’ 1 1 0 0 1 ’ )
11 c =( bitcmp (z ,5) ) ; // f i n d i n g 1 ’ s complement //
12 a = y + c +1
13 a =a - bin2dec ( ’ 1 0 0 0 0 0 ’ ) ;
14 a = dec2bin ( a ) ; // c o n v e r t i n g t h e r e s u l t t o b i n a r y
e q u i v a l e n t //
15 disp ( ’ t h e s u b t r a c t i o n o f two b i n a r y numbers g i v e : ’ )
16 disp ( a ) ; // d i s p l a y i n g t h e r e s u l t //
1 // s u b t r a c t i o n u s i n g 1 ’ s complement //
59
2 // e x a m p l e 2 1 . d //
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s //
7 // c o n v e r t i n g f i r s t b i n a r y i n p u t t o d e c i m a l //
8 p =1;
9 // i n i t i a l i s i n g //
10 q =1;
11 z =0;
12 b =0;
13 w =0;
14 f =0;
15 // b i n= i n p u t ( E n t e r t h e b i n a r y no t o be c o n v e r t e d
to i t s decimal e q u i v a l e n t : )
16 // a c c e p t i n g t h e f i r s t b i n a r y i n p u t from u s e r //
17 bin =10111.1;
18 d = modulo ( bin ,1) ;
19 // s e p a r a t i n g t h e d e c i m a l p a r t and t h e i n t e g e r p a r t //
20 d = d *10^10;
21 a = floor ( bin ) ;
22 // r e m o v i n g t h e d e c i m a l p a r t //
23 while ( a >0)
24 // Loop t o t a k e t h e b i n a r y b i t s o f i n t e g e r i n t o a
m a t r i x //
25 r = modulo ( a ,10) ;
26 b (1 , q ) = r ;
27 a = a /10;
28 a = floor ( a ) ;
29 q = q +1;
30 end
31 for m =1: q -1
32 // m u l t i p l i y i n g t h e b i t s o f i n t e g e r p o s i t i o n v a l u e s
and a d d i n g //
33 c = m -1;
34 f = f + b (1 , m ) *(2^ c ) ;
35 end
36 while ( d >0)
60
37 // Loop t o t a k e t h e b i n a r y b i t s o f d e c i m a l i n t o a
m a t r i x //
38 e = modulo ( d ,2)
39 w (1 ,p ) = e
40 d = d /10;
41 d = floor ( d )
42 p = p +1;
43 end
44 for n =1: p -1
45 // m u l t i p l i y i n g t h e b i t s o f d e c i m a l w i t h t h e i r
p o s i t i o n v a l u e s and a d d i n g //
46 z = z + w (1 ,n ) *(0.5) ^(11 -n ) ;
47 end
48 z = z *10000;
49 // r o u n d i n g o f t o 4 d e c i m a l v a l u e s //
50 z = round ( z ) ;
51 z = z /10000;
52 d1 = f + z ;
53 // c o n v e r t i n g s e c o n d b i n a r y i n p u t t o i t s complement
d e c i m a l number //
54 p =1;
55 // i n i t i a l i s i n g //
56 q =1;
57 z =0;
58 b =0;
59 w =0;
60 f =0;
61 // a c c e p t i n g 2 nd b i n a r y i n p u t //
62 a =10011.1
63 s =0;
64 d = modulo (a ,1) ; // s e p a r a t i n g i n t e g e r and d e c i m a l p a r t
//
65 // s i n c e complement o f d e c i m a l p a r t i s 0 , we do n o t
c o n s i d e r i t f u r t h e r //
66 a = floor ( a ) ; // g e t t i n g t h e i n t e g e r p a r t //
67 for i =1:5
68 x ( i ) = modulo (a ,10)
69 if x ( i ) ==1 then
61
70 x ( i ) =0
71 else
72 x ( i ) =1
73 end
74 s = s +( x ( i ) *(2^( i -1) ) )
75 a = a /10
76 a = floor ( a )
77 end
78 d2 = s ; // a s s i g n i n g t h e e q u i v a l e n t d e c i m a l v a l u e t o d2
//
79 r = d1 + d2 +.5; // a d d i n g 1 t o t h e d e c i m a l e q u i v a l e n t s t o
f i n d t h e r e s u l t //
80 r =r - bin2dec ( ’ 1 0 0 0 0 0 ’ ) ;
81 r = dec2bin ( r ) ; // c o n v e r t i n g t h e r e s u l t t o b i n a r y
e q u i v a l e n t //
82 disp ( ’ t h e s u b t r a c t i o n o f two b i n a r y numbers g i v e : ’ )
83 disp ( r ) ; // d i s p l a y i n g t h e r e s u l t //
1 // s u b t r a c t i o n u s i n g 1 ’ s complement //
2 // e x a m p l e 2 1 . e //
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s //
7 // c o n v e r t i n g f i r s t b i n a r y i n p u t t o d e c i m a l //
8 p =1;
9 // i n i t i a l i s i n g //
10 q =1;
11 z =0;
12 b =0;
13 w =0;
14 f =0;
15 // b i n= i n p u t ( E n t e r t h e b i n a r y no t o be c o n v e r t e d
62
to i t s decimal e q u i v a l e n t : )
16 // a c c e p t i n g t h e f i r s t b i n a r y i n p u t from u s e r //
17 bin =11011.00;
18 d = modulo ( bin ,1) ;
19 // s e p a r a t i n g t h e d e c i m a l p a r t and t h e i n t e g e r p a r t //
20 d = d *10^10;
21 a = floor ( bin ) ;
22 // r e m o v i n g t h e d e c i m a l p a r t //
23 while ( a >0)
24 // Loop t o t a k e t h e b i n a r y b i t s o f i n t e g e r i n t o a
m a t r i x //
25 r = modulo ( a ,10) ;
26 b (1 , q ) = r ;
27 a = a /10;
28 a = floor ( a ) ;
29 q = q +1;
30 end
31 for m =1: q -1
32 // m u l t i p l i y i n g t h e b i t s o f i n t e g e r p o s i t i o n v a l u e s
and a d d i n g //
33 c = m -1;
34 f = f + b (1 , m ) *(2^ c ) ;
35 end
36 while ( d >0)
37 // Loop t o t a k e t h e b i n a r y b i t s o f d e c i m a l i n t o a
m a t r i x //
38 e = modulo ( d ,2)
39 w (1 ,p ) = e
40 d = d /10;
41 d = floor ( d )
42 p = p +1;
43 end
44 for n =1: p -1
45 // m u l t i p l i y i n g t h e b i t s o f d e c i m a l w i t h t h e i r
p o s i t i o n v a l u e s and a d d i n g //
46 z = z + w (1 ,n ) *(0.5) ^(11 -n ) ;
47 end
48 z = z *10000;
63
49 // r o u n d i n g o f t o 4 d e c i m a l v a l u e s //
50 z = round ( z ) ;
51 z = z /10000;
52 d1 = f + z ;
53 // c o n v e r t i n g s e c o n d b i n a r y i n p u t t o i t s complement
d e c i m a l number //
54 p =1;
55 // i n i t i a l i s i n g //
56 q =1;
57 z =0;
58 b =0;
59 w =0;
60 f =0;
61 a =10011.1
62 s =0;
63 d = modulo (a ,1) ; // s e p a r a t i n g i n t e g e r and d e c i m a l p a r t
//
64 // s i n c e complement o f d e c i m a l p a r t i s 0 , we do n o t
c o n s i d e r i t f u r t h e r //
65 a = floor ( a ) ; // g e t t i n g t h e i n t e g e r p a r t //
66 for i =1:5
67 x ( i ) = modulo (a ,10)
68 if x ( i ) ==1 then
69 x ( i ) =0
70 else
71 x ( i ) =1
72 end
73 s = s +( x ( i ) *(2^( i -1) ) )
74 a = a /10
75 a = floor ( a )
76 end
77 d2 = s ; // a s s i g n i n g t h e e q u i v a l e n t d e c i m a l v a l u e t o d2
//
78 r = d1 + d2 +.25; // a d d i n g 1 t o t h e d e c i m a l e q u i v a l e n t s t o
f i n d t h e r e s u l t //
79 disp ( r )
80 r =r - bin2dec ( ’ 1 0 0 0 0 0 ’ )
81 q =0;
64
82 b =0;
83 s =0;
84 // e n t e r t h e d e c i m a l number t o be c o n v e r t e d //
85 a=r;
86 d = modulo (a ,1) ; // s e p a r a t i n g d e c i m a l and i n t e g e r p a r t
//
87 a = floor ( a ) ; // r e m o v i n g d e c i m a l p a r t //
88 while (a >0)
89 // t a k i n g i n t e g e r p a r t i n t o a m a t r i x and c o n v e r t i n g
t o e q u i v a l e n t b i n a r y //
90 x = modulo (a ,2)
91 b = b +(10^ q ) * x
92 a = a /2
93 a = floor ( a )
94 q = q +1
95 end
96 for i =1:10
97 // f o r v a l u e s a f t e r d e c i m a l t h a t s h o u l d be c o n v e r t e d
t o b i n a r y //
98 d = d *2
99 q = floor ( d )
100 s = s + q /(10^ i ) ;
101 if d >=1 then
102 d =d -1
103 end
104 end
105 k=b+s;
106 disp ( ’ t h e s u b t r a c t i o n o f two numbers g i v e : ’ )
107 disp ( k ) ; // d i s p l a y i n g t h e r e s u l t //
1 // s u b t r a c t i o n u s i n g 2 ’ s complement //
2 // Example 2 2 . a //
3 // s u b t r a c t i o n u s i n g 2 ’ s complement //
65
4 clc
5 // c l e a r s t h e c o n s o l e //
6 clear
7 // c l e a r s a l l e x i s i t i n g v a r i a b l e s //
8 a = bin2dec ( ’ 1 1 0 1 1 ’ )
9 b = bin2dec ( ’ 1 1 0 0 1 ’ )
10 x = bitcmp (b ,5)
11 // complement o f t h e numbers //
12 z =1
13 u=x+z
14 // 1 i s added t o t h e c o m p l e m e n t s //
15 w=u+a
16 w =w - bin2dec ( ’ 1 0 0 0 0 0 ’ )
17 w = dec2bin ( w ) ; // c o v e r t i n g t o b i n a r y form //
18 disp ( ’ t h e s u b t r a c t i o n o f two b i n a r y numbers g i v e : ’ )
19 disp ( w ) ; // d i s p l a y i n g t h e r e s u l t //
1 // s u b t r a c t i o n u s i n g 2 ’ s complement //
2 // Example 2 2 . b //
3 // s u b t r a c t i o n u s i n g 2 ’ s complement //
4 clc
5 // c l e a r s t h e c o n s o l e //
6 clear
7 // c l e a r s a l l e x i s i t i n g v a r i a b l e s //
8 a = bin2dec ( ’ 1 1 0 1 1 ’ )
9 b = bin2dec ( ’ 0 1 1 0 0 ’ )
10 x = bitcmp (b ,5)
11 // complement o f t h e numbers //
12 z =1
13 u=x+z
14 // 1 i s added t o t h e c o m p l e m e n t s //
15 w=u+a
16 w =w - bin2dec ( ’ 1 0 0 0 0 0 ’ )
66
17 w = dec2bin ( w ) ; // c o v e r t i n g t o b i n a r y form //
18 disp ( ’ t h e s u b t r a c t i o n o f two b i n a r y numbers g i v e : ’ )
19 disp ( w ) ; // d i s p l a y i n g t h e r e s u l t //
1 // s u b t r a c t i o n u s i n g 2 ’ s complement //
2 // e x a m p l e 2 2 . c //
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s //
7 // c o n v e r t i n g f i r s t b i n a r y i n p u t t o d e c i m a l //
8 p =1;
9 // i n i t i a l i s i n g //
10 q =1;
11 z =0;
12 b =0;
13 w =0;
14 f =0;
15 // b i n= i n p u t ( E n t e r t h e b i n a r y no t o be c o n v e r t e d
to i t s decimal e q u i v a l e n t : )
16 // a c c e p t i n g t h e f i r s t b i n a r y i n p u t from u s e r //
17 bin =0.01111;
18 bin = bin *100000
19 y =0;
20 for i =1:4
21 x ( i ) = modulo ( bin ,10)
22 y = y +( x ( i ) /(2^(6 - i ) ) )
23 bin = bin /10
24 bin = floor ( bin )
25 end
26 d1 = y
27 // c o n v e r t i n g s e c o n d b i n a r y i n p u t t o i t s complement
d e c i m a l number //
67
28 a =.01001
29 a = a *100000
30 s =0;
31 for i =1:4
32 x ( i ) = modulo (a ,10)
33 if x ( i ) ==1 then
34 x ( i ) =0
35 else
36 x ( i ) =1
37 end
38 s = s +( x ( i ) /(2^(6 - i ) ) )
39 a = a /10
40 a = floor ( a )
41 end
42 s = s +.5
43 d2 = s ; // a s s i g n i n g t h e e q u i v a l e n t d e c i m a l v a l u e t o d2
//
44 d2 = d2 +.03125;
45 r = d1 + d2 ; // r e s u l t i n d e c i m a l form //
46 r =r - bin2dec ( ’ 1 ’ )
47 // c o n v e r t i n g t o b i n a r y form //
48 q =0;
49 b =0;
50 s =0;
51 // e n t e r t h e d e c i m a l number t o be c o n v e r t e d //
52 a=r;
53 d = modulo (a ,1) ; // s e p a r a t i n g d e c i m a l and i n t e g e r p a r t
//
54 a = floor ( a ) ; // r e m o v i n g d e c i m a l p a r t //
55 while (a >0)
56 // t a k i n g i n t e g e r p a r t i n t o a m a t r i x and c o n v e r t i n g
t o e q u i v a l e n t b i n a r y //
57 x = modulo (a ,2)
58 b = b +(10^ q ) * x
59 a = a /2
60 a = floor ( a )
61 q = q +1
62 end
68
63 for i =1:10
64 // f o r v a l u e s a f t e r d e c i m a l t h a t s h o u l d be c o n v e r t e d
t o b i n a r y //
65 d = d *2
66 q = floor ( d )
67 s = s + q /(10^ i ) ;
68 if d >=1 then
69 d =d -1
70 end
71 end
72 k=b+s;
73 disp ( ’ t h e s u b t r a c t i o n o f two b i n a r y numbers g i v e : ’ )
74 disp ( k ) ; // t h e r e s u l t i n b i n a r y form //
1 // s u b t r a c t i o n u s i n g 2 ’ s complement //
2 // e x a m p l e 2 2 . d //
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s //
7 // c o n v e r t i n g f i r s t b i n a r y i n p u t t o d e c i m a l //
8 p =1;
9 // i n i t i a l i s i n g //
10 q =1;
11 z =0;
12 b =0;
13 w =0;
14 f =0;
15 // b i n= i n p u t ( E n t e r t h e b i n a r y no t o be c o n v e r t e d
to i t s decimal e q u i v a l e n t : )
16 // a c c e p t i n g t h e f i r s t b i n a r y i n p u t from u s e r //
17 bin =111.010;
18 d = modulo ( bin ,1) ;
69
19 // s e p a r a t i n g t h e d e c i m a l p a r t and t h e i n t e g e r p a r t //
20 d = d *10^10;
21 a = floor ( bin ) ;
22 // r e m o v i n g t h e d e c i m a l p a r t //
23 while ( a >0)
24 // Loop t o t a k e t h e b i n a r y b i t s o f i n t e g e r i n t o a
m a t r i x //
25 r = modulo ( a ,10) ;
26 b (1 , q ) = r ;
27 a = a /10;
28 a = floor ( a ) ;
29 q = q +1;
30 end
31 for m =1: q -1
32 // m u l t i p l i y i n g t h e b i t s o f i n t e g e r p o s i t i o n v a l u e s
and a d d i n g //
33 c = m -1;
34 f = f + b (1 , m ) *(2^ c ) ;
35 end
36 while ( d >0)
37 // Loop t o t a k e t h e b i n a r y b i t s o f d e c i m a l i n t o a
m a t r i x //
38 e = modulo ( d ,2)
39 w (1 ,p ) = e
40 d = d /10;
41 d = floor ( d )
42 p = p +1;
43 end
44 for n =1: p -1
45 // m u l t i p l i y i n g t h e b i t s o f d e c i m a l w i t h t h e i r
p o s i t i o n v a l u e s and a d d i n g //
46 z = z + w (1 ,n ) *(0.5) ^(11 -n ) ;
47 end
48 z = z *10000;
49 // r o u n d i n g o f t o 4 d e c i m a l v a l u e s //
50 z = round ( z ) ;
51 z = z /10000;
52 d1 = f + z ;
70
53 // c o n v e r t i n g s e c o n d b i n a r y i n p u t t o i t s complement
d e c i m a l number //
54 a =010.111;
55 s =0;
56 d = modulo (a ,1) ; // s e p a r a t i n g i n t e g e r and d e c i m a l p a r t
//
57 // s i n c e complement o f d e c i m a l p a r t i s 0 , we do n o t
c o n s i d e r i t f u r t h e r //
58 a = floor ( a ) ; // g e t t i n g t h e i n t e g e r p a r t //
59 for i =1:3
60 x ( i ) = modulo (a ,10)
61 if x ( i ) ==1 then
62 x ( i ) =0
63 else
64 x ( i ) =1
65 end
66 s = s +( x ( i ) *(2^( i -1) ) )
67 a = a /10
68 a = floor ( a )
69 end
70 d2 = s ; // a s s i g n i n g t h e e q u i v a l e n t d e c i m a l v a l u e t o d2
//
71 d2 = d2 +.125; // s i n c e d e c i m a l i s u p t o t h r e e p l a c e s //
72 r = d1 + d2 ;
73 r =r - bin2dec ( ’ 1 0 0 0 ’ ) ; // s t e p t o i g n o r e t h e msb //
74 // c o n v e r t i n g t h e r e s u l t t o i t s b i n a r y e q u i v a l e n t //
75 q =0;
76 b =0;
77 s =0;
78 // e n t e r t h e d e c i m a l number t o be c o n v e r t e d //
79 a=r;
80 d = modulo (a ,1) ; // s e p a r a t i n g d e c i m a l and i n t e g e r p a r t
//
81 a = floor ( a ) ; // r e m o v i n g d e c i m a l p a r t //
82 while (a >0)
83 // t a k i n g i n t e g e r p a r t i n t o a m a t r i x and c o n v e r t i n g
t o e q u i v a l e n t b i n a r y //
84 x = modulo (a ,2)
71
85 b = b +(10^ q ) * x
86 a = a /2
87 a = floor ( a )
88 q = q +1
89 end
90 for i =1:10
91 // f o r v a l u e s a f t e r d e c i m a l t h a t s h o u l d be c o n v e r t e d
t o b i n a r y //
92 d = d *2
93 q = floor ( d )
94 s = s + q /(10^ i ) ;
95 if d >=1 then
96 d =d -1
97 end
98 end
99 k=b+s;
100 disp ( ’ t h e s u b t r a c t i o n o f two b i n a r y numbers g i v e : ’ )
101 disp ( k ) ; // r e q u i r e d r e s u l t i n b i n a r y form //
1 // s u b t r a c t i o n u s i n g 2 ’ s complement //
2 // e x a m p l e 2 2 . e //
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s //
7 // c o n v e r t i n g f i r s t b i n a r y i n p u t t o d e c i m a l //
8 p =1;
9 // i n i t i a l i s i n g //
10 q =1;
11 z =0;
12 b =0;
13 w =0;
14 f =0;
72
15 // b i n= i n p u t ( E n t e r t h e b i n a r y no t o be c o n v e r t e d
to i t s decimal e q u i v a l e n t : )
16 // a c c e p t i n g t h e f i r s t b i n a r y i n p u t from u s e r //
17 bin =111.01;
18 d = modulo ( bin ,1) ;
19 // s e p a r a t i n g t h e d e c i m a l p a r t and t h e i n t e g e r p a r t //
20 d = d *10^10;
21 a = floor ( bin ) ;
22 // r e m o v i n g t h e d e c i m a l p a r t //
23 while ( a >0)
24 // Loop t o t a k e t h e b i n a r y b i t s o f i n t e g e r i n t o a
m a t r i x //
25 r = modulo ( a ,10) ;
26 b (1 , q ) = r ;
27 a = a /10;
28 a = floor ( a ) ;
29 q = q +1;
30 end
31 for m =1: q -1
32 // m u l t i p l i y i n g t h e b i t s o f i n t e g e r p o s i t i o n v a l u e s
and a d d i n g //
33 c = m -1;
34 f = f + b (1 , m ) *(2^ c ) ;
35 end
36 while ( d >0)
37 // Loop t o t a k e t h e b i n a r y b i t s o f d e c i m a l i n t o a
m a t r i x //
38 e = modulo ( d ,2)
39 w (1 ,p ) = e
40 d = d /10;
41 d = floor ( d )
42 p = p +1;
43 end
44 for n =1: p -1
45 // m u l t i p l i y i n g t h e b i t s o f d e c i m a l w i t h t h e i r
p o s i t i o n v a l u e s and a d d i n g //
46 z = z + w (1 ,n ) *(0.5) ^(11 -n ) ;
47 end
73
48 z = z *10000;
49 // r o u n d i n g o f t o 4 d e c i m a l v a l u e s //
50 z = round ( z ) ;
51 z = z /10000;
52 d1 = f + z ;
53 // c o n v e r t i n g s e c o n d b i n a r y i n p u t t o i t s complement
d e c i m a l number //
54 a =110.11;
55 s =0;
56 d = modulo (a ,1) ; // s e p a r a t i n g i n t e g e r and d e c i m a l p a r t
//
57 // s i n c e complement o f d e c i m a l p a r t i s 0 , we do n o t
c o n s i d e r i t f u r t h e r //
58 a = floor ( a ) ; // g e t t i n g t h e i n t e g e r p a r t //
59 for i =1:3
60 x ( i ) = modulo (a ,10)
61 if x ( i ) ==1 then
62 x ( i ) =0
63 else
64 x ( i ) =1
65 end
66 s = s +( x ( i ) *(2^( i -1) ) )
67 a = a /10
68 a = floor ( a )
69 end
70 d2 = s ; // a s s i g n i n g t h e e q u i v a l e n t d e c i m a l v a l u e t o d2
//
71 d2 = d2 +.25; // s i n c e d e c i m a l i s u p t o t h r e e p l a c e s //
72 r = d1 + d2 ;
73 r =r - bin2dec ( ’ 1 0 0 0 ’ ) ; // s t e p t o i g n o r e t h e msb //
74 // c o n v e r t i n g t h e r e s u l t t o i t s b i n a r y e q u i v a l e n t //
75 q =0;
76 b =0;
77 s =0;
78 // e n t e r t h e d e c i m a l number t o be c o n v e r t e d //
79 a=r;
80 d = modulo (a ,1) ; // s e p a r a t i n g d e c i m a l and i n t e g e r p a r t
//
74
81 a = floor ( a ) ; // r e m o v i n g d e c i m a l p a r t //
82 while (a >0)
83 // t a k i n g i n t e g e r p a r t i n t o a m a t r i x and c o n v e r t i n g
t o e q u i v a l e n t b i n a r y //
84 x = modulo (a ,2)
85 b = b +(10^ q ) * x
86 a = a /2
87 a = floor ( a )
88 q = q +1
89 end
90 for i =1:10
91 // f o r v a l u e s a f t e r d e c i m a l t h a t s h o u l d be c o n v e r t e d
t o b i n a r y //
92 d = d *2
93 q = floor ( d )
94 s = s + q /(10^ i ) ;
95 if d >=1 then
96 d =d -1
97 end
98 end
99 k=b+s;
100 disp ( ’ t h e s u b t r a c t i o n o f two b i n a r y numbers g i v e : ’ )
101 disp ( k ) ; // r e q u i r e d r e s u l t i n b i n a r y form //
1 // s u b t r a c t i o n u s i n g 2 ’ s complement //
2 // e x a m p l e 2 2 . f //
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s //
7 // c o n v e r t i n g f i r s t b i n a r y i n p u t t o d e c i m a l //
8 p =1;
9 // i n i t i a l i s i n g //
75
10 q =1;
11 z =0;
12 b =0;
13 w =0;
14 f =0;
15 // b i n= i n p u t ( E n t e r t h e b i n a r y no t o be c o n v e r t e d
to i t s decimal e q u i v a l e n t : )
16 // a c c e p t i n g t h e f i r s t b i n a r y i n p u t from u s e r //
17 bin =10111.1;
18 d = modulo ( bin ,1) ;
19 // s e p a r a t i n g t h e d e c i m a l p a r t and t h e i n t e g e r p a r t //
20 d = d *10^10;
21 a = floor ( bin ) ;
22 // r e m o v i n g t h e d e c i m a l p a r t //
23 while ( a >0)
24 // Loop t o t a k e t h e b i n a r y b i t s o f i n t e g e r i n t o a
m a t r i x //
25 r = modulo ( a ,10) ;
26 b (1 , q ) = r ;
27 a = a /10;
28 a = floor ( a ) ;
29 q = q +1;
30 end
31 for m =1: q -1
32 // m u l t i p l i y i n g t h e b i t s o f i n t e g e r p o s i t i o n v a l u e s
and a d d i n g //
33 c = m -1;
34 f = f + b (1 , m ) *(2^ c ) ;
35 end
36 while ( d >0)
37 // Loop t o t a k e t h e b i n a r y b i t s o f d e c i m a l i n t o a
m a t r i x //
38 e = modulo ( d ,2)
39 w (1 ,p ) = e
40 d = d /10;
41 d = floor ( d )
42 p = p +1;
43 end
76
44 for n =1: p -1
45 // m u l t i p l i y i n g t h e b i t s o f d e c i m a l w i t h t h e i r
p o s i t i o n v a l u e s and a d d i n g //
46 z = z + w (1 ,n ) *(0.5) ^(11 -n ) ;
47 end
48 z = z *10000;
49 // r o u n d i n g o f t o 4 d e c i m a l v a l u e s //
50 z = round ( z ) ;
51 z = z /10000;
52 d1 = f + z ;
53 // c o n v e r t i n g s e c o n d b i n a r y i n p u t t o i t s complement
d e c i m a l number //
54 a =10011.1;
55 s =0;
56 d = modulo (a ,1) ; // s e p a r a t i n g i n t e g e r and d e c i m a l p a r t
//
57 // s i n c e complement o f d e c i m a l p a r t i s 0 , we do n o t
c o n s i d e r i t f u r t h e r //
58 a = floor ( a ) ; // g e t t i n g t h e i n t e g e r p a r t //
59 for i =1:5
60 x ( i ) = modulo (a ,10)
61 if x ( i ) ==1 then
62 x ( i ) =0
63 else
64 x ( i ) =1
65 end
66 s = s +( x ( i ) *(2^( i -1) ) )
67 a = a /10
68 a = floor ( a )
69 end
70 d2 = s ; // a s s i g n i n g t h e e q u i v a l e n t d e c i m a l v a l u e t o d2
//
71 d2 = d2 +.5; // s i n c e d e c i m a l i s u p t o t h r e e p l a c e s //
72 r = d1 + d2 ;
73 r =r - bin2dec ( ’ 1 0 0 0 0 0 ’ ) ; // s t e p t o i g n o r e t h e msb //
74 // c o n v e r t i n g t h e r e s u l t t o i t s b i n a r y e q u i v a l e n t //
75 q =0;
76 b =0;
77
77 s =0;
78 // e n t e r t h e d e c i m a l number t o be c o n v e r t e d //
79 a = r ;
80 d = modulo (a ,1) ; // s e p a r a t i n g d e c i m a l and i n t e g e r part
//
81 a = floor ( a ) ; // r e m o v i n g d e c i m a l p a r t //
82 while (a >0)
83 // t a k i n g i n t e g e r p a r t i n t o a m a t r i x and c o n v e r t i n g
t o e q u i v a l e n t b i n a r y //
84 x = modulo (a ,2)
85 b = b +(10^ q ) * x
86 a = a /2
87 a = floor ( a )
88 q = q +1
89 end
90 for i =1:10
91 // f o r v a l u e s a f t e r d e c i m a l t h a t s h o u l d be c o n v e r t e d
t o b i n a r y //
92 d = d *2
93 q = floor ( d )
94 s = s + q /(10^ i ) ;
95 if d >=1 then
96 d =d -1
97 end
98 end
99 k=b+s;
100 disp ( ’ t h e s u b t r a c t i o n o f two b i n a r y numbers g i v e : ’ )
101 disp ( k ) ; // r e q u i r e d r e s u l t i n b i n a r y form //
1 //BCD t o d e c i m a l c o n v e r s i o n //
2 // Example 2 3 . a //
3 // s u b t r a c t i o n u s i n g 2 ’ s complement //
4 clc
78
5 // c l e a r s t h e c o n s o l e //
6 clear
7 // c l e a r s a l l e x i s i t i n g v a r i a b l e s //
8 a =010000111001; // t a k i n g t h e BCD i n p u t //
9 z =0;
10 d = modulo (a ,10000)
11 for j =1:4
12 y ( j ) = modulo (d ,10)
13 z = z +( y ( j ) *(2^( j -1) ) )
14 d = d /10
15 d = floor ( d )
16 end
17 b = a /10000
18 b = floor ( b )
19 c = modulo (b ,10000)
20 z1 =0
21 for j =1:2
22 y ( j ) = modulo (c ,10)
23 z1 = z1 +( y ( j ) *(2^( j -1) ) )
24 c = c /10
25 c = floor ( c )
26 end
27 e = b /10000
28 e = floor ( e )
29 z2 =0
30 for j =1:3
31 y ( j ) = modulo (e ,10)
32 z2 = z2 +( y ( j ) *(2^( j -1) ) )
33 e = e /10
34 e = floor ( e )
35 end
36 r = z2 *100+ z1 *10+ z
37 printf ( ’ t h e d e c i m a l e q u i v a l e n t o f BCD c o d e i s =%d ’ ,r )
79
1 //BCD t o d e c i m a l c o n v e r s i o n //
2 // Example 2 3 . b //
3 // s u b t r a c t i o n u s i n g 2 ’ s complement //
4 clc
5 // c l e a r s t h e c o n s o l e //
6 clear
7 // c l e a r s a l l e x i s i t i n g v a r i a b l e s //
8 a =100001001111000; // t a k i n g t h e BCD i n p u t //
9 z =0;
10 d = modulo (a ,10000)
11 for j =1:4
12 y ( j ) = modulo (d ,10)
13 z = z +( y ( j ) *(2^( j -1) ) )
14 d = d /10
15 d = floor ( d )
16 end
17 b = a /10000
18 b = floor ( b )
19 z1 =0
20 c = modulo (b ,10000)
21 for i =1:3
22 y ( i ) = modulo (c ,10)
23 z1 = z1 +( y ( i ) *(2^( i -1) ) )
24 c = c /10
25 c = floor ( c )
26 end
27 z2 =0
28 e = b /10000
29 e = floor ( e )
30 f = modulo (e ,10000)
31 for i =1:2
32 y ( i ) = modulo (f ,10)
33 z2 = z2 +( y ( i ) *(2^( i -1) ) )
34 f = f /10
35 f = floor ( f )
36 end
37 g = e /10000
38 g = floor ( g )
80
39 z3 =0
40 for i =1:3
41 y ( i ) = modulo (g ,10)
42 z3 = z3 +( y ( i ) *(2^( i -1) ) )
43 g = g /10
44 g = floor ( g )
45 end
46 r = z3 *1000+ z2 *100+ z1 *10+ z
47 printf ( ’ t h e e q u i v a l e n t d e c i m a l number i s =%d ’ ,r )
1 // b i n a r y t o g r a y c o d e //
2 // Example 2 4 . a //
3 // b i n a r y t o g r a y c o d e //
4 clc
5 // c l e a r s t h e c o n s o l e //
6 clear
7 // c l e a r s a l l e x i s t i n g v a r i a b l e s //
8 a =110100
9 for i =1:6
10 x ( i ) = modulo (a ,10)
11 a = a /10
12 a = floor ( a )
13 end
14 y (6) = x (6)
15 k =5
16 while (k >0)
17 if ( bitand ( x ( k +1) ,x ( k ) ) ==1) then
18 a = bitand ( x ( k +1) ,x ( k ) )
19 y ( k ) = bitcmp (a ,1)
20 else
21 y ( k ) = bitor ( x ( k +1) ,x ( k ) )
22 end
23 k =k -1
81
24 end
25 // d i s p l a y //
26 z =0
27 for i =1:6
28 z = z + y ( i ) *(10^( i -1) )
29 end
30 disp ( z )
31 disp ( ’ e q u i v a l e n t g r a y c o d e i s d i s p l a y e d ’ )
1 // b i n a r y t o g r a y c o d e //
2 // Example 2 4 . b //
3 // b i n a r y t o g r a y c o d e //
4 clc
5 // c l e a r s t h e c o n s o l e //
6 clear
7 // c l e a r s a l l e x i s t i n g v a r i a b l e s //
8 a =101101
9 for i =1:6
10 x ( i ) = modulo (a ,10)
11 a = a /10
12 a = floor ( a )
13 end
14 y (6) = x (6)
15 k =5
16 while (k >0)
17 if ( bitand ( x ( k +1) ,x ( k ) ) ==1) then
18 a = bitand ( x ( k +1) ,x ( k ) )
19 y ( k ) = bitcmp (a ,1)
20 else
21 y ( k ) = bitor ( x ( k +1) ,x ( k ) )
22 end
23 k =k -1
24 end
82
25 // d i s p l a y //
26 z =0
27 for i =1:6
28 z = z + y ( i ) *(10^( i -1) )
29 end
30 disp ( z )
31 disp ( ’ e q u i v a l e n t g r a y c o d e is displayed ’)
1 // b i n a r y t o g r a y c o d e //
2 // Example 2 4 . c //
3 // b i n a r y t o g r a y c o d e //
4 clc
5 // c l e a r s t h e c o n s o l e //
6 clear
7 // c l e a r s a l l e x i s t i n g v a r i a b l e s //
8 a =1110010
9 for i =1:7
10 x ( i ) = modulo (a ,10)
11 a = a /10
12 a = floor ( a )
13 end
14 y (7) = x (7)
15 k =6
16 while (k >0)
17 if ( bitand ( x ( k +1) ,x ( k ) ) ==1) then
18 a = bitand ( x ( k +1) ,x ( k ) )
19 y ( k ) = bitcmp (a ,1)
20 else
21 y ( k ) = bitor ( x ( k +1) ,x ( k ) )
22 end
23 k =k -1
24 end
25 // d i s p l a y //
83
26 z =0
27 for i =1:7
28 z = z + y ( i ) *(10^( i -1) )
29 end
30 disp ( z )
31 disp ( ’ e q u i v a l e n t g r a y c o d e is displayed ’)
1 // s u b t r a c t i o n u s i n g 1 ’ s complement //
2 // Example 2 5 . a //
3 // s u b t r a c t i o n i n one ’ s complement //
4 clc
5 // c l e a r s t h e window //
6 clear
7 // c l e a r s a l l t h e e x i s t i n g v a r i a b l e s //
8 x= ’ 1011101 ’
9 y = bin2dec ( ’ 1 0 1 1 1 0 1 ’ )
10 z = bin2dec ( ’ 1 1 0 1 1 0 0 ’ )
11 c = bitcmp (z ,7) ; // f i n d i n g 1 ’ s complement //
12 a = y + c +1
13 a = dec2bin (a -( bin2dec ( ’ 1 0 0 0 0 0 0 ’ ) ) )
14 // b i n a r y c o n v e r s i o n //
15 disp ( ’ b i n a r y form o f t h e number o b t a i n e d by a d d i n g
1 0 1 1 1 0 1 t o −1101100 ’ )
16 disp ( a )
17 // r e s u l t i s d i s p l a y e d //
1 // g r a y c o d e t o b i n a r y c o n v e r s i o n //
2 // Example 2 5 . b //
3 // g r a y c o d e t o b i n a r y //
84
4 clc
5 // c l e a r s t h e c o n s o l e //
6 clear
7 // c l e a r s a l l e x i s t i n g v a r i a b l e s //
8 a =1011010111001
9 for i =1:13
10 x ( i ) = modulo (a ,10)
11 a = a /10
12 a = floor ( a )
13 end
14 y (13) = x (13)
15 k =12
16 while (k >0)
17 if ( bitand ( y ( k +1) ,x ( k ) ) ==1) then
18 a = bitand ( y ( k +1) ,x ( k ) )
19 y ( k ) = bitcmp (a ,1)
20 else
21 y ( k ) = bitor ( y ( k +1) ,x ( k ) )
22 end
23 k =k -1
24 end
25 z =0
26 for i =1:13
27 z = z + y ( i ) *(10^( i -1) )
28 end
29 disp ( z )
30 disp ( ’ e q u i v a l e n t b i n a r y c o d e i s d i s p l a y e d ’ )
1 // d e c i m a l t o BCD c o n v e r s i o n //
2 // e x a m p l e 2 5 . c //
3 clc
4 // c l e a r s t h e command window //
5 clear
85
6 // c l e a r s //
7 a =63547; // i n p u t i n d e c i m a l //
8 for i =1:5
9 x ( i ) = modulo (a ,10)
10 a = a /10
11 a = floor ( a )
12 end
13 x = strcat ( dec2bin ( x (5) ,4) + dec2bin ( x (4) ,4) + dec2bin ( x
(3) ,4) + dec2bin ( x (2) ,4) + dec2bin ( x (1) ,4) )
14 disp ( ’ r e q u i r e d BCD c o d e i s d i s p l a y e d ’ )
15 disp ( x )
1 // s u b t r a c t i o n u s i n g 1 ’ s complement //
2 // Example 2 5 . d //
3 // s u b t r a c t i o n i n one ’ s complement //
4 clc
5 // c l e a r s t h e window //
6 clear
7 // c l e a r s a l l t h e e x i s t i n g v a r i a b l e s //
8 x =85
9 y =32
10 c = bitcmp (y ,7) ; // f i n d i n g 1 ’ s complement //
11 a = x + c +1
12 a =a - bin2dec ( ’ 1 0 0 0 0 0 0 0 ’ )
13 res = dec2bin (a ,7)
14 // b i n a r y c o n v e r s i o n //
15 disp ( ’ b i n a r y form o f t h e number o b t a i n e d by a d d i n g
85 t o −32 ’ )
16 disp ( res )
17 // r e s u l t i s d i s p l a y e d //
86
Scilab code Exa 1.26 finding equivalent in decimal for excess 3 code
1 // f i n d i n g e q u i v a l e n t i n d e c i m a l f o r f i v e e x c e s s 3
c o d e numbers //
2 // e x a m p l e 2 6 / /
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s //
7 // f i r s t number i n e x c e s s −3 c o d e //
8 x= ’ 1000 ’
9 a = bin2dec ( ’ 1 0 0 0 ’ ) ; // c o n v e r s i o n t o b i n a r y //
10 y =a -3; // c o n v e r s i o n t o e x c e s s 3 c o d e //
11 disp ( ’ e q u i v a l e n t d e c i m a l number f o r f i r s t e x c e s s 3
code : ’ )
12 disp ( y ) ; // d i s p l a y i n g r e s u l t //
13 // s e c o n d number i n e x c e s s −3 c o d e //
14 x= ’ 0011 ’
15 a = bin2dec ( ’ 0 0 1 1 ’ ) ; // c o n v e r s i o n t o b i n a r y //
16 y =a -3; // c o n v e r s i o n t o e x c e s s 3 c o d e //
17 disp ( ’ e q u i v a l e n t d e c i m a l number f o r s e c o n d e x c e s s 3
code : ’ )
18 disp ( y ) ; // d i s p l a y i n g r e s u l t //
19 // t h i r d number i n e x c e s s 3− c o d e //
20 x= ’ 0100 ’
21 a = bin2dec ( ’ 0 1 0 0 ’ ) ; // c o n v e r s i o n t o b i n a r y //
22 y =a -3; // c o n v e r s i o n t o e x c e s s 3 c o d e //
23 disp ( ’ e q u i v a l e n t d e c i m a l number f o r t h i r d e x c e s s 3
code : ’ )
24 disp ( y ) ; // d i s p l a y i n g r e s u l t //
25 // f o u r t h number i n e x c e s s 3− c o d e //
26 x= ’ 0101 ’
27 a = bin2dec ( ’ 0 1 0 1 ’ ) ; // c o n v e r s i o n t o b i n a r y //
28 y =a -3; // c o n v e r s i o n t o e x c e s s 3 c o d e //
29 disp ( ’ e q u i v a l e n t d e c i m a l number f o r f o u r t h e x c e s s 3
code : ’ )
30 disp ( y ) ; // d i s p l a y i n g r e s u l t //
31 // f i f t h number i n e x c e s s 3− c o d e //
87
32 x = ’ 0 1 1 1 ’
33 a = bin2dec ( ’ 0 1 1 1 ’ ) ; // c o n v e r s i o n t o b i n a r y //
34 y =a -3; // c o n v e r s i o n t o e x c e s s 3 c o d e //
35 disp ( ’ e q u i v a l e n t d e c i m a l number f o r f i f t h e x c e s s 3
code : ’ )
36 disp ( y ) ; // d i s p l a y i n g r e s u l t //
1 // g r a y c o d e t o b i n a r y c o n v e r s i o n //
2 // Example 2 7 . a //
3 // g r a y c o d e t o b i n a r y //
4 clc
5 // c l e a r s t h e c o n s o l e //
6 clear
7 // c l e a r s a l l e x i s t i n g v a r i a b l e s //
8 a =101110
9 for i =1:6
10 x ( i ) = modulo (a ,10)
11 a = a /10
12 a = floor ( a )
13 end
14 y (6) = x (6)
15 k =5
16 while (k >0)
17 if ( bitand ( y ( k +1) ,x ( k ) ) ==1) then
18 a = bitand ( y ( k +1) ,x ( k ) )
19 y ( k ) = bitcmp (a ,1)
20 else
21 y ( k ) = bitor ( y ( k +1) ,x ( k ) )
22 end
23 k =k -1
24 end
25 z =0
26 for i =1:6
88
27 z = z + y ( i ) *(10^( i -1) )
28 end
29 disp ( z )
30 disp ( ’ e q u i v a l e n t b i n a r y c o d e is displayed ’)
1 // g r a y c o d e t o b i n a r y c o n v e r s i o n //
2 // Example 2 7 . b //
3 // g r a y c o d e t o b i n a r y //
4 clc
5 // c l e a r s t h e c o n s o l e //
6 clear
7 // c l e a r s a l l e x i s t i n g v a r i a b l e s //
8 a =111011
9 for i =1:6
10 x ( i ) = modulo (a ,10)
11 a = a /10
12 a = floor ( a )
13 end
14 y (6) = x (6)
15 k =5
16 while (k >0)
17 if ( bitand ( y ( k +1) ,x ( k ) ) ==1) then
18 a = bitand ( y ( k +1) ,x ( k ) )
19 y ( k ) = bitcmp (a ,1)
20 else
21 y ( k ) = bitor ( y ( k +1) ,x ( k ) )
22 end
23 k =k -1
24 end
25 z =0
26 for i =1:6
27 z = z + y ( i ) *(10^( i -1) )
28 end
89
29 disp ( z )
30 disp ( ’ e q u i v a l e n t b i n a r y c o d e i s d i s p l a y e d ’ )
1 // g r a y c o d e t o b i n a r y c o n v e r s i o n //
2 // Example 2 7 . c //
3 // g r a y c o d e t o b i n a r y //
4 clc
5 // c l e a r s t h e c o n s o l e //
6 clear
7 // c l e a r s a l l e x i s t i n g v a r i a b l e s //
8 a =1101110
9 for i =1:7
10 x ( i ) = modulo (a ,10)
11 a = a /10
12 a = floor ( a )
13 end
14 y (7) = x (7)
15 k =6
16 while (k >0)
17 if ( bitand ( y ( k +1) ,x ( k ) ) ==1) then
18 a = bitand ( y ( k +1) ,x ( k ) )
19 y ( k ) = bitcmp (a ,1)
20 else
21 y ( k ) = bitor ( y ( k +1) ,x ( k ) )
22 end
23 k =k -1
24 end
25 z =0
26 for i =1:7
27 z = z + y ( i ) *(10^( i -1) )
28 end
29 disp ( z )
30 disp ( ’ e q u i v a l e n t b i n a r y c o d e i s d i s p l a y e d ’ )
90
Scilab code Exa 1.28.a constructing an even parity 7 bit hamming code
1 // c o n s t r u c t i n g an e v e n p a r i t y 7 b i t hamming c o d e //
2 // e x a m p l e 2 8 . a //
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s //
7 z =0100; // i n p u t //
8 a =0; b =0; c =0; d =0;
9 // t a k i n g t h e i n p u t //
10 for i =1:7
11 x ( i ) =0
12 if ( i ==5) then
13 x ( i ) =1
14 end
15 end
16 // 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 / /
17 for i =3:7
18 if ( x ( i ) ==1) then
19 a = a +1
20 end
21 end
22 d = modulo (a ,2)
23 if ( d ==1) then
24 x (1) =1
25 end
26 // 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 / /
27 for i =3:7
28 if ( i ==5) then
29 continue
30 end
31 if ( x ( i ) ==1) then
32 b = b +1
91
33 end
34 end
35 d = modulo (b ,2)
36 if ( d ==1) then
37 x (2) =1
38 end
39 // 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 / /
40 for i =5:7
41 if ( x ( i ) ==1) then
42 c = c +1
43 end
44 end
45 d = modulo (c ,2)
46 if ( d ==1) then
47 x (4) =1
48 end
49 // d i s p l a y i n g t h e r e s u l t //
50 disp ( ’ t h e r e q u i r e d hamming c o d e i s : ’ )
51 for i =1:7
52 printf ( ’%d ’ ,x ( i ) )
53 end
Scilab code Exa 1.28.b constructing an even parity 7 bit hamming code
1 // c o n s t r u c t i n g an e v e n p a r i t y 7 b i t hamming c o d e //
2 // e x a m p l e 2 8 . b //
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s //
7 z =1110; // i n p u t //
8 a =0; b =0; c =0; d =0;
9 // t a k i n g t h e i n p u t //
10 for i =1:7
11 x ( i ) =0
92
12 if ( i ==3) then
13 x ( i ) =1
14 end
15 if ( i ==5) then
16 x ( i ) =1
17 end
18 if ( i ==6) then
19 x ( i ) =1
20 end
21 end
22 // 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 / /
23 for i =3:7
24 if ( i ==6) then
25 continue
26 end
27 if ( x ( i ) ==1) then
28 a = a +1
29 end
30 end
31 d = modulo (a ,2)
32 if ( d ==1) then
33 x (1) =1
34 end
35 // 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 / /
36 for i =3:7
37 if ( i ==5) then
38 continue
39 end
40 if ( x ( i ) ==1) then
41 b = b +1
42 end
43 end
44 d = modulo (b ,2)
45 if ( d ==1) then
46 x (2) =1
47 end
48 // 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 / /
49 for i =5:7
93
50 if ( x ( i ) ==1) then
51 c = c +1
52 end
53 end
54 d = modulo (c ,2)
55 if ( d ==1) then
56 x (4) =1
57 end
58 // d i s p l a y i n g t h e r e s u l t //
59 disp ( ’ t h e r e q u i r e d hamming c o d e i s : ’ )
60 for i =1:7
61 printf ( ’%d ’ ,x ( i ) )
62 end
Scilab code Exa 1.29 locating error in hamming code and correcting it
1 // l o c a t i n g e r r o r i n hamming c o d e and c o r r e c t i n g i t //
2 // e x a m p l e 2 9 / /
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s //
7 a =1111001
8 b =0; c =0; d =0; e =0;
9 for i =1:7
10 x (8 - i ) = modulo (a ,10)
11 a = a /10
12 a = floor ( a )
13 end
14 // c h e c k 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 / /
15 for i =4:7
16 if ( x ( i ) ==1) then
17 b = b +1
18 end
19 end
94
20 d = modulo (b ,2)
21 if ( d ==0) then
22 r1 =0
23 else
24 r1 =1
25 end
26 // c h e c k 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 / /
27 for i =2:7
28 if ( i ==4) then
29 continue
30 end
31 if ( i ==5) then
32 continue
33 end
34 if ( x ( i ) ==1) then
35 c = c +1
36 end
37 end
38 d = modulo (c ,2)
39 if ( d ==0) then
40 r2 =0
41 else
42 r2 =1
43 end
44 // c h e c k 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 / /
45 for i =1:7
46 if ( i ==2) then
47 continue
48 end
49 if ( i ==4) then
50 continue
51 end
52 if ( i ==6) then
53 continue
54 end
55 if ( x ( i ) ==1) then
56 e = e +1
57 end
95
58 end
59 d = modulo (e ,2)
60 if ( d ==0) then
61 r3 =0
62 else
63 r3 =1
64 end
65 r = r1 *(2^2) + r2 *(2^1) + r3 ; // c o n v e r t i n g t o e q u i v a l e n t
d e c i m a l //
66 printf ( ’ e r r o r o c c u r s a t p o s i t i o n =%d\n ’ ,r )
67 // b i t p o s i t i o n a t which e r r o r o c c u r s h a s t o be
c h a n g e d //
68 if ( x ( r ) ==1) then
69 x ( r ) =0
70 else
71 x ( r ) =1
72 end
73 // d i s p l a y i n g t h e c o r r e c t hamming c o d e //
74 disp ( ’ t h e c o r r e c t hamming c o d e i s : ’ )
75 for i =1:7
76 printf ( ’%d ’ ,x ( i ) )
77 end
96
Chapter 2
1 // s i m p l i f i c a t i o n o f b o o l e a n e x p r e s s i o n //
2 // e x a m p l e 8 . a //
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s //
7 disp ( ’ g i v e n Y=AB+AB ’ ’C(B ’ ’C ’ ’+C)+A ’ ’C ’ ’ ’ )
8 disp ( ’Y=AB+AB ’ ’B ’ ’CC ’ ’+AB ’ ’CC+A ’ ’C ’ ’ ’ ) ; //ON
MULTIPLICATION//
9 disp ( ’Y=AB+AB ’ ’C+A ’ ’+C ’ ’ ’ ) ; //CC’ ’ = 0 / /
10 disp ( ’Y=C ’ ’+AB+(A+A ’ ’ ) (A ’ ’+B ’ ’C) ’ )
11 disp ( ’Y=C ’ ’+AB+ 1 . (A ’ ’+B ’ ’C) ’ )
12 disp ( ’Y=A ’ ’+A . B+C ’ ’+B ’ ’C ’ )
13 disp ( ’Y=(A ’ ’+A) . ( A ’ ’+B) +(C ’ ’+B ’ ’ ) . ( C ’ ’+C) ’ )
14 disp ( ’Y= 1 . (A ’ ’+B) +(C ’ ’+B ’ ’ ) . 1 ’ )
15 disp ( ’ t h e s i m p l i f i e d e x p r e s s i o n i s : ’ )
16 disp ( ’Y=A ’ ’+B+C ’ ’+B ’ ’ ’ ) ; // r e q u i r e d s i m p l i f i e d
97
e x p r e s s i o n //
1 // s i m p l i f i c a t i o n o f b o o l e a n e x p r e s s i o n //
2 // e x a m p l e 8 . b //
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s //
7 disp ( ’ g i v e n Y=A ’ ’BC ’ ’+A ’ ’BC+AB ’ ’C ’ ’+ABC ’ )
8 disp ( ’Y=A ’ ’BC ’ ’+A ’ ’BC+A ’ ’BC+AB ’ ’C ’ ’+ABC ’ )
9 disp ( ’Y=A ’ ’B(C+C ’ ’ )+A ’ ’BC+AB ’ ’C+AB ’ ’C ’ ’ ’ )
10 disp ( ’Y=A ’ ’B.1+BC(A+A ’ ’ )+AB ’ ’C ’ ’ ’ ) ; //C+C’ ’ = 1 / /
11 disp ( ’Y=A ’ ’B+BC+AB ’ ’C ’ ’ ’ ) ; //A+A’ ’ = 1 / /
12 disp ( ’ t h e s i m p l i f i e d e x p r e s s i o n i s : ’ )
13 disp ( ’Y=B . ( A ’ ’+C)+AB ’ ’C ’ ’ ’ ) ; // r e q u i r e d s i m p l i f i e d
e x p r e s s i o n //
1 // s i m p l i f i c a t i o n o f b o o l e a n e x p r e s s i o n //
2 // e x a m p l e 9 . a //
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s //
7 // s i m p l i f i c a t i o n o f b o o l e a n e x p r e s s i o n //
8 disp ( ’ g i v e n Y=ABC ’ ’D ’ ’+ABC ’ ’D+ABCD ’ ’+ABCD ’ )
9 disp ( ’ s i n c e D+D ’ ’ =1 ,we g e t ’ )
10 disp ( ’Y=ABC ’ ’ (D+D ’ ’ )+ABC(D ’ ’+D) ’ )
11 disp ( ’Y=ABC ’ ’+ABC ’ )
12 disp ( ’Y=AB(C+C ’ ’ ) ’ )
98
13 disp ( ’ t h e s i m p l i f i e d e x p r e s s i o n i s : ’ )
14 disp ( ’Y=AB ’ ) ; //C+C’=1//
15 // a n s w e r a f t e r s i m p l i f i c a t i o n //
1 // s i m p l i f i c a t i o n o f b o o l e a n e x p r e s s i o n //
2 // e x a m p l e 9 . b //
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s //
7 // s i m p l i f i c a t i o n o f b o o l e a n e x p r e s s i o n //
8 disp ( ’ g i v e n Y=AB+ABC ’ ’+A ’ ’BC+ABC ’ )
9 disp ( ’ on s i m p l i f i c a t i o n we g e t ’ )
10 disp ( ’Y=AB+ABC ’ ’+A ’ ’BC+ABC+A ’ ’BC ’ )
11 disp ( ’Y=AB+AB(C+C ’ ’ )+BC(A+A ’ ’ ) ’ )
12 disp ( ’ s i n c e x+x ’ ’ =1 ,we g e t ’ )
13 disp ( ’Y=AB+AB+BC ’ )
14 disp ( ’Y=AB+BC ’ )
15 disp ( ’ t h e s i m p l i f i e d e x p r e s s i o n i s : ’ )
16 disp ( ’Y=B(A+C) ’ )
17 // a n s w e r a f t e r s i m p l i f i c a t i o n //
1 // s i m p l i f i c a t i o n o f b o o l e a n e x p r e s s i o n s //
2 // e x a m p l e 9 . c //
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s //
7 // s i m p l i f i c a t i o n o f b o o l e a n e x p r e s s i o n //
99
8 disp ( ’ g i v e n Y=AB(A ’ ’BC ’ ’+AB ’ ’C ’ ’+A ’ ’BC) ’ )
9 disp ( ’ on m u l t i p l i c a t i o n ’ )
10 disp ( ’Y=ABA ’ ’BC ’ ’+ABAB ’ ’C ’ ’+ABA ’ ’BC ’ )
11 disp ( ’Y=AA ’ ’ . BC ’ ’+AB ’ ’ . B ’ ’C ’ ’+AA ’ ’ . BC ’ )
12 disp ( ’ s i n c e x . x ’ ’ =0 ’ )
13 disp ( ’Y=0.BC ’ ’+A . 0 . C ’ ’ +0.BC ’ )
14 disp ( ’Y=0 ’ )
15 // a n s w e r a f t e r s i m p l i f i c a t i o n //
1 // s i m p l i f i c a t i o n o f b o o l e a n e x p r e s s i o n s //
2 // e x a m p l e 1 0 . a //
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s //
7 // s i m p l i f i c a t i o n o f b o o l e a n e x p r e s s i o n //
8 disp ( ’ g i v e n Y=(A+B) ’ ’ . ( A ’ ’+B ’ ’ ) ’ ’ ’ )
9 disp ( ’Y=(A ’ ’ . B ’ ’ ) . ( A . B) ’ ) ; // a p p l y i n g De Morgan ’ s law
//
10 disp ( ’Y=AA ’ ’ . BB ’ ’ ’ )
11 disp ( ’ t h e s i m p l i f i e d e x p r e s s i o n i s : ’ )
12 disp ( ’Y=0 ’ ) ; // s i n c e x . x ’ ’ = 0 / /
13 // s i m p l i f i e d b o o l e a n e x p r e s s i o n //
1 // s i m p l i f i c a t i o n o f b o o l e a n e x p r e s s i o n s //
2 // e x a m p l e 1 0 . b //
3 clc
4 // c l e a r s t h e command window //
5 clear
100
6 // c l e a r s //
7 // s i m p l i f i c a t i o n o f b o o l e a n e x p r e s s i o n //
8 disp ( ’Y=ABC+A ’ ’B+ABC ’ ’ ’ )
9 disp ( ’Y=AB(C+C ’ ’ )+A ’ ’B ’ )
10 disp ( ’Y=B(A+A ’ ’ ) ’ ) ; //C+C’ ’ = 1 / /
11 disp ( ’ t h e s i m p l i f i e d e x p r e s s i o n i s : ’ )
12 disp ( ’Y=B ’ ) ; // s i m p l i f i e d e x p r e s s i o n //
1 // s i m p l i f i c a t i o n o f b o o l e a n e x p r e s s i o n s //
2 // e x a m p l e 1 0 . c //
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s //
7 // s i m p l i f i c a t i o n o f b o o l e a n e x p r e s s i o n //
8 disp ( ’ g i v e n Y=C(AB ’ ’+AB)+BC ’ )
9 disp ( ’Y=CA(B ’ ’+B)+BC ’ )
10 disp ( ’Y=CA+BC ’ ) ; //B+B’ ’ = 1 / /
11 disp ( ’ t h e s i m p l i f i e d e x p r e s s i o n i s : ’ )
12 disp ( ’Y=C(A+B) ’ )
13 // s i m p l i f i e d b o o l e a n e x p r e s s i o n //
1 // s i m p l i f i c a t i o n o f b o o l e a n e x p r e s s i o n s //
2 // e x a m p l e 1 1 / /
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s //
7 // s i m p l i f i c a t i o n o f b o o l e a n e x p r e s s i o n //
101
8 disp ( ’ g i v e n Y=ABCD+ABCD ’ ’+A ’ ’BCD+A ’ ’BCD ’ ’ ’ )
9 disp ( ’Y=BC(AD+AD ’ ’+A ’ ’D+A ’ ’D ’ ’ ) ’ )
10 disp ( ’Y=BC{A(D+D ’ ’ )+A ’ ’ (D+D ’ ’ ) } ’ )
11 disp ( ’Y=BC(A.1+A ’ ’ . 1 ) ’ )
12 disp ( ’BC(A+A ’ ’ ) ’ )
13 disp ( ’ t h e s i m p l i f i e d e x p r e s s i o n i s : ’ )
14 disp ( ’Y=BC ’ ) ; // s i m p l i f i e d b o o l e a n e x p r e s s i o n //
1 // s i m p l i f i c a t i o n o f b o o l e a n e x p r e s s i o n s //
2 // e x a m p l e 1 3 . a //
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s //
7 disp ( ’ g i v e n Z=(AB ’ ’C ’ ’+AB ’ ’C+ABC+ABC ’ ’ ) (A+B) ’ )
8 disp ( ’ Z={}AB ’ ’ (C+C ’ ’ )+AB(C+C ’ ’ ) } (A+B) ’ )
9 disp ( ’ Z=(AB ’ ’+AB) (A+B) ’ )
10 disp ( ’ Z=A(B ’ ’+B) (A+B) ’ )
11 disp ( ’ Z=A . 1 . ( A+B) ’ )
12 disp ( ’ Z=AA+AB ’ )
13 disp ( ’ Z=A+AB ’ )
14 disp ( ’ Z=A(1+B) ’ )
15 disp ( ’ t h e s i m p l i f i e d e x p r e s s i o n i s : ’ )
16 disp ( ’A ’ ) ; // s i m p l i f i e d b o o l e a n e x p r e s s i o n //
1 // s i m p l i f i c a t i o n o f b o o l e a n e x p r e s s i o n s //
2 // e x a m p l e 1 3 . b //
3 clc
4 // c l e a r s t h e command window //
102
5 clear
6 // c l e a r s //
7 disp ( ’ g i v e n A=P+P ’ ’QR ’ ’ +(Q+R) ’ ’ ’ )
8 disp ( ’A=P+P ’ ’QR ’ ’+Q ’ ’ . R ’ ’ ’ ) ; //DE MORGAN’ S LAW//
9 disp ( ’A=P+R ’ ’ (P ’ ’Q+Q ’ ’ ) ’ )
10 disp ( ’A=P+R ’ ’ [ P ’ ’Q+Q ’ ’ (P+P ’ ’ ) ] ’ )
11 disp ( ’A=P+R ’ ’ [ P ’ ’ (Q+Q ’ ’ )+PQ ’ ’ ] ’ )
12 disp ( ’A=P+R ’ ’ [ P ’ ’+PQ ] ’ )
13 disp ( ’A=P+R ’ ’ { (P ’ ’+P) . ( P ’ ’+Q ’ ’ ) } ’ )
14 disp ( ’A=P+R ’ ’ (P ’ ’+Q ’ ’ ) ’ )
15 disp ( ’A=P+P ’ ’R ’ ’+Q ’ ’R ’ ’ ’ )
16 disp ( ’A=(P+P ’ ’ ) . ( P ’ ’+R ’ ’ )+Q ’ ’R ’ ’ ’ )
17 disp ( ’A= 1 . (P+R ’ ’ )+Q ’ ’R ’ ’ ’ )
18 disp ( ’A=P+R ’ ’+Q ’ ’R ’ ’ ’ )
19 disp ( ’A=P+R ’ ’+Q ’ ’R ’ ’ ’ )
20 disp ( ’A=P+R ’ ’ (1+Q ’ ’ ) ’ )
21 disp ( ’ t h e s i m p l i f i e d e x p r e s s i o n i s : ’ )
22 disp ( ’A=P+R ’ ’ ’ )
1 // f i n d i n g SOP//
2 // e x a m p l e 2 9 / /
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s //
7 disp ( ’ f=summation ( 1 , 2 , 4 , 7 ) ’ )
8 disp ( ’ f =001+0101+100+111 ’ )
9 disp ( ’ r e q u i r e d SOP form : ’ )
10 disp ( ’ f=A ’ ’B ’ ’C+A ’ ’BC ’ ’+AB ’ ’C ’ ’+ABC ’ ) ;
103
1 // f i n d i n g POS//
2 // e x a m p l e 3 0 / /
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s //
7 disp ( ’ g i v e n f=p r o d u c t ( 0 , 3 , 5 , 6 ) ’ )
8 disp ( ’ t h i s i n d i c a t e s ( 0 0 0 ) ( 0 1 1 ) ( 1 0 1 ) ( 1 1 0 ) ’ )
9 disp ( ’ f =(A+B+C) (A+B ’ ’+C ’ ’ ) (A ’ ’+B+C ’ ’ ) (A ’ ’+B ’ ’+C) ’ ) ;
// r e q u i r e d POS e x p r e s s i o n //
1 // c o n v e r s i o n t o c a n o n i c a l SOP//
2 // e x a m p l e 3 1 . a //
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s //
7 // c o n v e r s i o n t o c a n o n i c a l SOP//
8 disp ( ’ g i v e n Y=AB+A ’ ’B ’ ’+AC+A ’ ’C ’ ’ ’ )
9 disp ( ’ s i n c e x+x ’ ’ =1 ,we g e t ’ )
10 disp ( ’Y=AB(C+C ’ ’ )+A ’ ’B ’ ’ (C+C ’ ’ )+AC(B+B ’ ’ )+A ’ ’C ’ ’ (B+B
’ ’ ) ’)
11 disp ( ’Y=ABC+ABC ’ ’+A ’ ’B ’ ’C+A ’ ’B ’ ’C ’ ’+ACB ’ ’+ACB ’ ’+A ’ ’C
’ ’B+A ’ ’C ’ ’B ’ ’ ’ )
12 disp ( ’ s i n c e ABC and A ’ ’B ’ ’C ’ ’ a p p e a r t w i c e , we w r i t e
them o n c e ’ )
13 disp ( ’Y=ABC+ABC ’ ’+A ’ ’B ’ ’C+A ’ ’B ’ ’C ’ ’+AB ’ ’C+A ’ ’BC ’ ’ ’ )
14 disp ( ’ t h e r e q u i r e d SOP e x p r e s s i o n ’ )
15 disp ( ’Y=111+110+001+000+101+010 ’ )
16 disp ( ’ h e n c e t h e r e q u i r e d e n t r i e s w i l l be a t t h e
positions 0 ,1 ,2 ,5 ,6 ,7 ’)
17 disp ( ’ f=summation ( 0 , 1 , 2 , 5 , 6 , 7 ) ’ )
104
Scilab code Exa 2.31.b conversion to canonical POS
1 // c o n v e r s i o n t o c a n o n i c a l POS//
2 // e x a m p l e 3 1 . b //
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s //
7 // c o n v e r s i o n t o c a n o n i c a l POS//
8 disp ( ’ g i v e n Y=AB+A ’ ’B ’ ’+AC+A ’ ’C ’ ’ ’ )
9 disp ( ’ we u s e t h e complement o f t h e p r e v i o u s e x a m p l e ’
)
10 disp ( ’ t h e e n t r i e s w i l l be a t 3 , 4 i . e . 0 1 1 , 1 0 0
positions ’)
11 disp ( ’Y=A ’ ’BC+AB ’ ’C ’ ’ ’ )
12 disp ( ’Y=(A ’ ’BC . AB ’ ’C ’ ’ ) ’ ’ ’ )
13 // de morgan ’ s t h e o r e m //
14 disp ( ’Y=(A+B ’ ’+C ’ ’ ) . ( A ’ ’+B+C) ’ )
15 disp ( ’ t h e r e q u i r e d POS e x p r e s s i o n ’ )
105
10 disp ( ’ f =[C+(A ’ ’+B) (A ’ ’+B ’ ’ ) ] [ C ’ ’ +(A ’ ’+B) (A ’ ’+B) ] [ ( A+
B+C ’ ’ ) (A ’ ’+B+C ’ ’ ) ’ )
11 disp ( ’ f =(C+A ’ ’+B) (C+A ’ ’+B ’ ’ ) (C ’ ’+A ’ ’+B) (C ’ ’+A ’ ’+B ’ ’ )
(A+B+C ’ ’ ) (A ’ ’+B+C) ’ )
12 disp ( ’ f =(A ’ ’+B+C) (A ’ ’+B ’ ’+C ’ ’ ) (A ’ ’+B+C ’ ’ ) (A ’ ’+B ’ ’+C ’
’ ) (A+B+C ’ ’ ) ’ )
13 disp ( ’ f = ( 1 0 0 ) ( 1 1 0 ) ( 1 0 1 ) ( 1 1 1 ) ( 0 0 1 ) ’ )
14 disp ( ’ r e q u i r e d POS form i s : ’ )
15 disp ( ’ f=p r o d u c t ( 1 , 4 , 5 , 6 , 7 ) ’ ) ; // r e q u i r e d POS form //
16 disp ( ’ f=A ’ ’ (B+C ’ ’ ) ’ )
17 disp ( ’ f=A ’ ’B+A ’ ’C ’ ’ ) ’ )
18 disp ( ’ f=A ’ ’B(C+C ’ ’ )+A ’ ’C ’ ’ (B+B ’ ’ ) ’ )
19 disp ( ’ f=A ’ ’BC+A ’ ’BC ’ ’+A ’ ’BC ’ ’+A ’ ’B ’ ’C ’ ’ ’ )
20 disp ( ’ f =011+010+000 ’ )
21 disp ( ’ r e q u i r e d SOP form i s : ’ )
22 disp ( ’ f=summation ( 0 , 2 , 3 ) ’ ) ; // r e q u i r e d SOP form //
106
13 disp ( ’ r e q u i r e d POS form : ’ )
14 disp ( ’ f=p r o d u c t ( 0 , 2 , 3 , 4 , 6 , 7 ) ’ )
15 // f i n d i n g SOP//
16 disp ( ’ f =(A+A ’ ’ ) . B ’ ’C ’ )
17 disp ( ’ f=AB ’ ’C+A ’ ’BC ’ )
18 disp ( ’ f =101+001 ’ )
19 disp ( ’ r e q u i r e d SOP form : ’ )
20 disp ( ’ f=summation ( 5 , 1 ) ’ )
1 // f i n d i n g maxterms and m i n t e r m s //
2 // e x a m p l e 3 4 / /
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s //
7 // f i n d i n g m i n t e r m s and maxterms //
8 disp ( ’ g i v e n Y=AC ’ ’+A ’ ’C ’ )
9 disp ( ’Y=A ’ ’C(B+B ’ ’ )+AC ’ ’ (B+B ’ ’ ) ’ )
10 disp ( ’Y=A ’ ’CB+A ’ ’CB ’ ’+AC ’ ’B+AC ’ ’B ’ ’ ’ )
11 disp ( ’ t h e s e a r e t h e r e q u i r e d m i n t e r m s ’ )
12 disp ( ’ t h e m i n t e r m s i n d i c a t e t h e p o s i t i o n s 1 , 3 , 4 , 6 ’ )
13 disp ( ’ t h e maxterms i n d i c a t e t h e p o s i t i o n s 0 , 2 , 5 , 7 ’ )
14 // c o m p l e m e n t a r y o f m i n t e r m s //
15 disp ( ’Y=(A+B+C) (A+B ’ ’+C) +(A ’ ’+B+C ’ ’ ) (A ’ ’+B ’ ’+C ’ ’ ) ’ )
16 disp ( ’ t h e s e a r e t h e r e q u i r e d maxterms ’ )
1 // f i n d i n g maxterms //
2 // e x a m p l e 3 5 / /
3 clc
107
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s //
7 disp ( ’ g i v e n f=p r o d u c t ( 0 , 3 , 5 , 6 ) ’ )
8 disp ( ’ f = ( 0 0 0 ) ( 0 1 1 ) ( 1 0 1 ) ( 1 1 0 ) ’ )
9 disp ( ’ r e q u i r e d maxterms : ’ )
10 disp ( ’ f =(A+B+C) (A+B ’ ’+C ’ ’ ) (A ’ ’+B+C ’ ’ ) (A ’ ’+B ’ ’+C) ’ )
1 // f i n d i n g r e q u i r e d d a t a //
2 // e x a m p l e 3 6 / /
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s //
7 disp ( ’ f=A ’ ’B ’ ’C+A ’ ’BC ’ ’+AB ’ ’C+ABC ’ ’ ’ ) ; // s i n c e f is 1
a t p o s i t i o n s 1 , 2 , 5 , 6 ; t h i s i s t h e r e q u i r e d SOP
e x p r e s s i o n //
8 disp ( ’ f =(A+B+C) (A+B ’ ’+C ’ ’ ) (A ’ ’+B+C) (A ’ ’+B ’ ’+C ’ ’ ) ’ ) ;
// s i n c e f i s 0 a t 0 , 3 , 4 , 7 ; t h i s i s t h e r e q u i r e d
POS e x p r e s s i o n //
9 disp ( ’ sum o f m i n t e r m s=summation ( 1 , 2 , 5 , 6 ) ’ )
10 disp ( ’ p r o d u c t o f maxterms=p r o d u c t ( 0 , 3 , 4 , 7 ) ’ )
1 // f i n d i n g SOP//
2 // e x a m p l e 3 7 / /
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s //
108
7 disp ( ’ g i v e n f=AC ’ )
8 disp ( ’ f=AC(B+B ’ ’ ) (D+D ’ ’ ) ’ )
9 disp ( ’ f=ACBD+ACBD ’ ’+ACB ’ ’D+ACB ’ ’D ’ ’ ’ ) ; // r e q u i r e d sum
o f m i n t e r m s //
10 disp ( ’ f =1111+1110+1011+1010 ’ )
11 disp ( ’ r e q u i r e d SOP form : ’ )
12 disp ( ’ f=summation ( 1 0 , 1 1 , 1 4 , 1 5 ) ’ )
Scilab code Exa 2.38 finding sum of minterms and product of maxterms
109
Chapter 3
1 // f i n d i n g r e q u i r e d d a t a //
2 // e x a m p l e 1//
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s //
7 // f i n d i n g t i m e c o n s t a n t and t i m e f o r c a p a c i t o r to
c h a r g e 90% o f s u p p l i e d v o l t a g e //
8 R =10^6 ; // r e s i s t a n c e i n ohms //
9 C =10^ -5 ; // c a p a c i t a n c e i n f a r a d s //
10 T=R*C
11 printf ( ’ t h e t i m e c o n s t a n t=%f s e c o n d s \n ’ ,T )
12 // t i m e c o n s t a n t i s f o u n d o u t //
13 v =90/100*10; // v=v o l t a g e a t t i m e t //
14 V =10; // v o l t a g e i n v o l t s //
15 // t=r e q u i r e d t i m e //
16 disp ( ’ from t h e f o r m u l a v=V∗(1 − exp (− t / (R∗C) ) ) , we g e t
the r e q u i r e d time as : ’ )
17 t = -(( R * C ) * log (1 -( v / V ) ) )
110
18 printf ( ’ r e q u i r e d t i m e t o c h a r g e t o 90 p e r c e n t o f t h e
s u p p l i e d v o l t a g e=%f s e c o n d s ’ ,t )
19 // t h e r e s u l t i s t s e c o n d s //
1 // f i n d i n g minimum a m p l i f i c a t i o n //
2 // e x a m p l e 2//
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s //
7 r =.04*10^ -6
8 // r i s t h e r i s e t i m e //
9 disp ( ’ r i s e t i m e =(2∗ v ) / ( 2 ∗ %pi ∗ f ∗V) , h e n c e ’ )
10 // f=f r e q u e n c y i n h e t r z ; v=o u t p u t v o l t a g e i n v o l t s ; V=
r e q u i r e d maximum o u t p u t v o l t a g e i n v o l t s //
11 f =400*10^3
12 v =6.28
13 V =(2* v ) /( r *2* %pi * f )
14 Vin =12.5; // i n p u t v o l t a g e //
15 a = V / Vin
16 printf ( ’ minimum a m p l i f i c a t i o n r e q u i r e d=%f ’ ,a )
1 // f i n d i n g c l a m p i n g l e v e l v o l t a g e //
2 // e x a m p l e 3 . a //
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s //
7 // c h a r g e=v o l t a g e / t i m e //
111
8 disp ( ’ from e q u a t i o n ( (V∗4000∗10ˆ −6) / 2 0 ) =((5−V)
∗4∗10ˆ −6) / ( 2 0 ∗ 1 0 ˆ − 3 ) ) , we g e t ’ )
9 // r e s i s t a n c e =20kohm ; V=c l a m p i n g l e v e l v o l t a g e ; c h a r g e=
v o l t a g e ∗ c a p a c i t a n c e //
10 V =5*(10^ -6)
11 printf ( ’ c l a m p i n g l e v e l v o l t a g e=%f v o l t ’ ,V )
12 // t h e a n s w e r i s d i s p l a y e d //
1 // f i n d i n g c l a m p i n g l e v e l v o l t a g e //
2 // e x a m p l e 3 . b //
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s //
7 // c h a r g e=v o l t a g e / t i m e //
8 disp ( ’ from e q u a t i o n ( (V∗4∗10ˆ −6) / 2 0 ) =((5−V)
∗4000∗10ˆ −6) / ( 2 0 ∗ 1 0 ˆ − 3 ) ) , we g e t ’ )
9 // r e s i s t a n c e =20kohm ; V=c l a m p i n g l e v e l v o l t a g e ; c h a r g e=
v o l t a g e ∗ c a p a c i t a n c e //
10 V =20/8
11 printf ( ’ c l a m p i n g l e v e l v o l t a g e=%f v o l t ’ ,V )
12 // t h e a n s w e r i s d i s p l a y e d //
1 // f i n d i n g o u t p u t v o l t a g e //
2 // e x a m p l e 4//
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s //
112
7 V1 =10; // v o l t a g e s o u r c e a m p l i t u d e //
8 Rf =50; // f o r w a r d r s i s t a n c e //
9 r1 =50; // i n t e r n a l r e s i s t a n c e //
10 i = V1 * Rf /( Rf + r1 )
11 // c u r r e n t t h r o u g h d i o d e d u r i n g p o s i t i v e h a l f c y c l e //
12 C =10^ -6
13 T =( Rf + r1 ) * C ; // t i m e c o n s t a n t d u r i n g c o n d u c t i o n //
14 f =10*10^3
15 T1 =1/(2* f ) ; // t i m e d u r a t i o n d u r i n g which i n p u t
v o l t a g e i s p o s i t i v e //
16 t =50*10^ -6; // g i v e n t i m e //
17 v = V1 *(1 - exp ( -( t / T ) ) ) ; // v o l t a g e a c r o s s c a p a c i t o r
a f t e r 50 m i c r o s e c //
18 Vo =(( V1 - v ) * Rf ) /( Rf + r1 ) ; // o u t p u t v o l t a g e a c r o s s d i o d e
//
19 R =20*10^3; // r e s i s t a n c e i n s e r i e s //
20 T2 =( r1 + R ) * C ; // t i m e c o n s t a n t o f d i s c h a r g i n g c i r c u i t //
21 V2 = v * exp ( -( t / T2 ) ) ; // v o l t a g e a t t h e end o f 50 m i c r o
s e c //
22 printf ( ’ o u t p u t v o l t a g e=%f v o l t ’ , V2 )
23 // r e s u l t i s d i s p l a y e d //
1 // f i n d i n g l o a d r e s i s t a n c e and v a l u e o f c a p a c i t o r //
2 // e x a m p l e 6//
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s //
7 Vo =15; // o u t p u t v o l t a g e //
8 t =10*10^ -3; // t i m e d u r a t i o n //
9 tr =.05*10^ -6; // r i s e t i m e //
10 Rs =100*10^3; // s o u r c e r e s i s t a n c e //
11 C =50*10^ -12; // o u t p u t c a p a c i t a n c e //
113
12 S =.05; // s a g a l l o w e d //
13 // t r =.35/ f 2 where f 2=u p p e r c u t o f f f r e q u e n c y //
14 // a l s o t r =2.2∗R∗C where R=l o a d r e s i s t a n c e //
15 R = tr /(2.2* C )
16 printf ( ’ r e s i s t a n c e =%f ohms \n ’ ,R )
17 // r e s u l t i s d i s p l a y e d //
18 Cs = t /( Rs * S ) ; // Cs=s o u r c e c a p a c i t o r //
19 printf ( ’ c a p a c i t a n c e=%f f a r a d ’ , Cs ) ; // r e s u l t i s
d i s p l a y e d //
1 // f i n d i n g peak v o l t a g e //
2 // e x a m p l e 7//
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s //
7 T =250*10^ -12; // t i m e c o n s t a n t //
8 t =.05*10^ -6;
9 Vo =100; // peak o u t p u t v o l t a g e //
10 Vi =( Vo * t ) / T ; // peak v o l t a g e o f g e n e r a t o r //
11 printf ( ’ peak v o l t a g e=%f v o l t ’ , Vi )
12 // r e s u l t i s d i s p l a y e d //
114
Chapter 5
1 // f i n d i n g r e q u i r e d t i m e //
2 // e x a m p l e 1//
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s //
7 R =10*10^3; // e x t e r n a l r e s i s t a n c e //
8 C =0.1*10^ -6; // t i m i n g c a p a c i t o r //
9 T =1.1* R * C ; // t i m e f o r which o u t p u t r e m a i n s h i g h i n
m o n o s t a b l e m u l t i v i b r a t o r u s i n g IC 555 t i m e r //
10 printf ( ’ r e q u i r e d t i m e=%f s e c o n d s ’ ,T ) ; // r e s u l t i s
displayed
1 // c a l c u l a t i n g f r e q u e n c y and duty c y c l e //
115
2 // e x a m p l e 2//
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s //
7 Ra =10*10^3
8 Rb =10*10^3
9 C =.001*10^ -6
10 T1 =.693*( Ra + Rb ) * C ; // c h a r g i n g t i m e c o n s t a n t //
11 T2 =.693* Rb * C ; // d i s c h a r g i n g t i m e c o n s t a n t //
12 T = T1 + T2 ; // t o t a l t i m e //
13 f =1/ T ; // f r e q u e n c y //
14 printf ( ’ f r e q u e n c y=%f h e r t z \n ’ ,f ) ; // r e s u l t i s
d i s p l a y e d //
15 D = T2 / T ; // duty c y c l e //
16 D = D *100
17 printf ( ’ duty c y c l e=%f p e r c e n t ’ ,D )
18 // r e s u l t i s d i s p l a y e d //
1 // c a l c u l a t i n g f r e q u e n c y and duty c y c l e //
2 // e x a m p l e 3//
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s //
7 Ra =6.8*10^3
8 Rb =3.3*10^3
9 C =.1*10^ -6
10 T1 =.693*( Ra + Rb ) * C ; // c h a r g i n g t i m e c o n s t a n t //
11 T2 = .693* Rb * C ; // d i s c h a r g i n g t i m e c o n s t a n t //
12 T = T1 + T2 ; // t o t a l t i m e //
13 f =1/ T // f r e q u e n c y //
14 printf ( ’ f r e q u e n c y=%f h e r t z \n ’ ,f ) ; // r e s u l t i s
116
d i s p l a y e d //
15 D = T2 / T ; // duty c y c l e //
16 D = D *100
17 printf ( ’ duty c y c l e=%f p e r c e n t ’ ,D ) ; // r e s u l t i s
d i s p l a y e d //
117
Chapter 8
D to A and A to D converters
1 // f i n d i n g b i n a r y e q u i v a l e n t w e i g h t s //
2 // e x a m p l e 1//
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s //
7 n =6; // no . o f b i t s //
8 LSB1 =1/((2^6) -1)
9 printf ( ’ w e i g h t o f 1 s t l s b=%f\n ’ , LSB1 )
10 LSB2 =2* LSB1
11 printf ( ’ w e i g h t o f 2 nd l s b=%f\n ’ , LSB2 )
12 LSB3 =4* LSB1
13 printf ( ’ w e i g h t o f 3 r d l s b=%f\n ’ , LSB3 )
14 LSB4 =8* LSB1
15 printf ( ’ w e i g h t o f 4 t h l s b=%f\n ’ , LSB4 )
16 LSB5 =16* LSB1
17 printf ( ’ w e i g h t o f 5 t h l s b=%f\n ’ , LSB5 )
18 MSB =32* LSB1
19 printf ( ’ w e i g h t o f msb=%f ’ , MSB )
20 // r e s u l t s a r e d i s p l a y e d //
118
Scilab code Exa 8.2 calculating required data
1 // c a l c u l a t i n g t h e r e q u i r e d d a t a //
2 // e x a m p l e 2//
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s //
7 n =4;
8 WLSB1 =1/(2^4 -1) ; // w e i g h t a s s i g n e d t o LSB//
9 printf ( ’ w e i g h t a s s i g n e d t o LSB=%f\n ’ , WLSB1 )
10 WLSB2 =2* WLSB1 ; // w e i g h t a s s i g n e d t o 2 nd LSB//
11 printf ( ’ w e i g h t a s s i g n e d t o s e c o n d LSB=%f\n ’ , WLSB2 )
12 WLSB3 =4* WLSB1 ; // w e i g h t a s s i g n e d t o 3 r d LSB//
13 printf ( ’ w e i g h t a s s i g n e d t o 3 r d LSB=%f\n ’ , WLSB3 )
14 // h i g h l e v e l 1=10V//
15 V1 =10* WLSB1 ; // c h a n g e i n o u t p u t v o l t a g e due t o c h a n g e
i n LSB//
16 V2 =10*8/15; // c h a n g e i n o u t p u t v o l t a g e due t o c h a n g e
i n MSB//
17 printf ( ’ c h a n g e i n o u t p u t v o l t a g e due t o c h a n g e i n
LSB=%f v o l t \n ’ , V1 )
18 printf ( ’ c h a n g e i n o u t p u t v o l t a g e due t o c h a n g e i n
MSB=%f v o l t \n ’ , V2 )
19 // r e s u l t s a r e d i s p l a y e d //
20 disp ( ’ Vo1 =(20/R+40/R+80/R) / ( 1 /R+2/R+4/R+8/R) ’ )
21 Vo1 =140/15;
22 printf ( ’ o u t p u t v o l t a g e f o r i n p u t 1110= %f v o l t \n ’ , Vo1
)
23 disp ( ’ Vo2 =(10/R+40/R+80/R) / ( 1 /R+2/R+4/R+8/R) ’ )
24 Vo2 =130/15;
25 printf ( ’ o u t p u t v o l t a g e f o r i n p u t 1101= %f v o l t ’ , Vo2 )
26 // r e s u l t s a r e d i s p l a y e d //
119
Scilab code Exa 8.3 calculating required data
1 // c l a c u l a t i n g t h e r e q u i r e d d a t a //
2 // e x a m p l e 2//
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s //
7 disp ( ’ t h e f u l l s c a l e maximum v o l t a g e o c c u r s when a l l
input are at high l e v e l 1 ’ )
8 Vm =10; // f u l l s c a l e maximum o u t p u t v o l t a g e //
9 printf ( ’ f u l l s c a l e maximum o u t p u t v o l t a g e=%d v o l t \n ’
, Vm ) ; // r e s u l t i s d i s p l a y e d //
10 n =5
11 LSB =1/((2^ n ) -1) ; // w e i g h t o f LSB//
12 V1 =10* LSB ; // c h a n g e i n o u t p u t v o l t a g e due t o LSB//
13 printf ( ’ c h a n g e i n o u t p u t v o l t a g e due t o c h a n g e i n
l s b=%f v o l t \n ’ , V1 )
14 disp ( ’ a n a l o g o u t p u t v o l t a g e f o r 1 1 0 0 1 i s g i v e n by Vo
=(10/R+80/R+160/R) / ( 1 /R+2/R+4/R+8/R+16/R) ’ )
15 Vo =250/31;
16 printf ( ’ a n a l o g o u t p u t v o l t a g e f o r i n p u t 11001= %f
v o l t \n ’ , Vo )
1 // c a l c u l a t i n g r e q u i r e d d a t a //
2 // e x a m p l e 4//
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s //
120
7 a = bin2dec ( ’ 1 0 0 1 0 ’ )
8 Vo =9*10^ -3; // o u t p u t v o l t a g e //
9 k = Vo / a ; // p r o p o r t i o n a l i t y f a c t o r //
10 b = bin2dec ( ’ 1 1 0 1 1 ’ )
11 Vo1 = k * b ; // a n a l o g o u t p u t v o l t a g e //
12 printf ( ’ a n a l o g o u t p u t v o l t a g e=%f v o l t \n ’ , Vo1 ) ;
13 //maximum 5 b i t d i g i t a l i n p u t i s 1 1 1 1 1 i . e . 31 i n
d e c i m a l //
14 Vm = k *31; //maximum f u l l s c a l e o u t p u t v o l t a g e //
15 printf ( ’ maximum f u l l s c a l e o u t p u t v o l t a g e=%f v o l t ’ ,
Vm ) ;
1 // c a l c u l a t i n g r e q u i r e d d a t a //
2 // e x a m p l e 5//
3 clc
4 // c l e a r s t h e command window / / ;
5 clear
6 // c l e a r s //
7 LSB1 =10; // c h a n g e i n o u t p u t v o l t a g e due t o LSB//
8 LSB2 =2* LSB1 ; // c h a n g e i n o u t p u t v o l t a g e due t o s e c o n d
LSB//
9 LSB3 =4* LSB1 ; // c h a n g e i n o u t p u t v o l t a g e due t o t h i r d
LSB//
10 LSB4 =8* LSB1 ; // c h a n g e i n o u t p u t v o l t a g e due t o f o u r t h
LSB//
11 LSB5 =16* LSB1 ; // c h a n g e i n o u t p u t v o l t a g e due t o f i f t h
LSB//
12 Vmax = LSB1 + LSB2 + LSB3 + LSB4 + LSB5 ; //maximum f u l l s c a l e
o u t p u t which o c c u r s a t i n p u t 1 1 1 1 1 / /
13 // a l l t h e v o l t a g e u n i t s a r e i n mV//
14 printf ( ’ maximum f u l l s c a l e o u t p u t i n mV a t 1111=%d
v o l t \n ’ , Vmax ) ; // d i s p l a y i n g t h e r e s u l t //
15 R =10; // r e s o l u t i o n i n mV s i n c e s m a l l e s t i n c r e m e n t due
121
t o c h a n g e i n LSB i s 10mV//
16 printf ( ’ r e s o l u t i o n =%d v o l t \n ’ ,R ) ; // d i a p l a y i n g t h e
r e s u l t //
17 S =10; // s t e p s i z e = r e s o l u t i o n //
18 R1 = S / Vmax *100; // p e r c e n t a g e r e s o l u t i o n //
19 printf ( ’ p e r c e n t a g e r e s o l u t i o n =%f p e r c e n t ’ , R1 ) ; //
d i s p l a y i n g t h e r e s u l t //
1 // c a l c u l a t i n g r e q u i r e d d a t a //
2 // e x a m p l e 6//
3 clc
4 // c l e a r s t h e command window / / ;
5 clear
6 // c l e a r s //
7 N =(2^8) -1; // no . o f s t e p s //
8 M =1000/ N ; // no . o f s t e p s i n which motor s p e e d can
v a r i e d ; motor s p e e d v a r i e s from 0 t o 1 0 0 0 rpm //
9 N1 =450/ M ; // no . o f s t e p s r e q u i r e d t o r e a c h 450 rpm //
10 N2 = round ( N1 ) ; // r o u n d i n g t h e no . o f s t e p s //
11 disp ( ’ For 115 t h s t e p , s p e e d o f motor w i l l be
a p p r o x i m a t e l y 451 rpm which g i v e s an a c c u r a c y o f
. 2 2% ’ )
12 // d i s p l a y i n g t h e a n s w e r //
1 // c a l c u l a t i n g v a l u e s o f r e s i s t o r s //
2 // e x a m p l e 7//
3 clc
4 // c l e a r s t h e command window / / ;
5 clear
122
6 // c l e a r s //
7 R1 =10*10^3; // r e s i s t o r i n ohm f o r msb //
8 R =16* R1 ; // r e s i s t o r f o r LSB//
9 R2 = R /2; // r e s i s t o r f o r 2 nd LSB//
10 printf ( ’ r e s i s t o r f o r l s b=%d ohm\n ’ ,R )
11 printf ( ’ r e s i s t o r f o r 2 nd l s b=%d ohm ’ , R2 )
1 // c a l c u l a t i n g v a l u e o f r e s i s t o r //
2 // e x a m p l e 8//
3 clc
4 // c l e a r s t h e command window / / ;
5 clear
6 // c l e a r s //
7 R =640*(10^3) ; // r e s i s t o r f o r LSB//
8 R1 = R /32; // r e s i s t o r f o r MSB ; s i n c e 6 b i t , h e n c e LSB i s
d i v i d e d by 3 2 / /
9 printf ( ’ r e s i s t o r f o r msb=%d ohm ’ , R1 ) ; // d i s p l a y i n g
r e s u l t //
1 // c a l c u l a t i n g r e q u i r e d d a t a //
2 // e x a m p l e 9//
3 clc
4 // c l e a r s t h e command window / / ;
5 clear
6 // c l e a r s //
7 N =2^10 -1; // no . o f s t e p s //
8 Vmax =10*10^ -3* N ; //maximum f u l l s c a l e o u t p u t //
9 R =(10*10^ -3) / Vmax *100; // p e r c e n t a g e r e s o l u t i o n //
10 printf ( ’ maximum f u l l s c a l e o u t p u t=%f ampere \n ’ , Vmax )
123
11 printf ( ’ p e r c e n t a g e r e s o l u t i o n =%f p e r c e n t ’ ,R )
1 // c a l c u l a t i n g r e q u i r e d d a t a //
2 // e x a m p l e 1 0 / /
3 clc
4 // c l e a r s t h e command window / / ;
5 clear
6 // c l e a r s //
7 S =10/10^3; // s t e p s i z e =w e i g h t o f Ao i n V//
8 printf ( ’ s t e p s i z e =%f v o l t \n ’ ,S ) ; // d i s p l a y i n g step
s i z e //
9 n =99; // no . o f s t e p s //
10 Vmax = S * n ; //maximum f u l l s c a l e o u t p u t //
11 printf ( ’ maximum f u l l s c a l e o u t p u t=%f v o l t \n ’ , Vmax ) ;
// d i s p l a y i n g f u l l s c a l e o u t p u t //
12 R = S / Vmax *100; // p e r c e n t a g e r e s o l u t i o n //
13 printf ( ’ p e r c e n t a g e r e s o l u t i o n =%f p e r c e n t \n ’ ,R ) ; //
p e r c e n t a g e r e s o l u t i o n d i s p l a y e d //
14 Ao =10/10^3
15 Bo =20/10^3
16 Co =40/10^3
17 Do =80/10^3
18 A1 =100/10^3
19 B1 =200/10^3
20 C1 =400/10^3
21 D1 =800/10^3
22 // w e i g h t i n V o f d i f f e r e n t i n p u t s //
23 // i n p u t f o r D1 , C1 , B1 , A1 i s 1 1 0 1 and f o r Do , Co , Bo , Ao
i s 1011//
24 V = D1 + C1 + A1 + Do + Bo + Ao ; // a n a l o g o u t p u t v o l t a g e //
25 printf ( ’ a n a l o g o u t p u t v o l t a g e=%f v o l t ’ ,V ) ; // r e s u l t
d i s p l a y e d //
124
Scilab code Exa 8.11 calculating output voltages
1 // c a l c u l a t i n g o u t p u t v o l t a g e s //
2 // e x a m p l e 1 1 / /
3 clc
4 // c l e a r s t h e command window //
5 clear
6 // c l e a r s //
7 V =10; // h i g h i n p u t v o l t a g e //
8 VMSB = V /2; // o u t p u t c a u s e d by MSB//
9 VMSB2 = V /4; // o u t p u t c a u s e d by s e c o n d MSB//
10 VMSB3 = V /8; // o u t p u t c a u s e d by t h i r d MSB//
11 VMSB4 = V /16; // o u t p u t c a u s e d by f o u r t h MSB//
12 VMSB5 = V /32; // o u t p u t c a u s e d by f i f t h MSB//
13 VMSB6 = V /64; // o u t p u t c a u s e d by s i x t h MSB//
14 printf ( ’ o u t p u t c a u s e d by MSB=%f v o l t \n ’ , VMSB )
15 printf ( ’ o u t p u t c a u s e d by s e c o n d MSB=%f v o l t \n ’ , VMSB2
)
16 printf ( ’ o u t p u t c a u s e d by t h i r d MSB=%f v o l t \n ’ , VMSB3 )
17 printf ( ’ o u t p u t c a u s e d by f o u r t h MSB=%f v o l t \n ’ , VMSB4
)
18 printf ( ’ o u t p u t c a u s e d by f i f t h MSB=%f v o l t \n ’ , VMSB5 )
19 printf ( ’ o u t p u t c a u s e d by s i x t h MSB=%f v o l t \n ’ , VMSB6 )
20 // d i s p l a y i n g t h e r e s u l t s //
1 // c a l c u l a t i n g r e q u i r e d d a t a //
2 // e x a m p l e 1 2 / /
3 clc
4 // c l e a r s t h e command window //
5 clear
125
6 // c l e a r s //
7 V =10; // h i g h i n p u t v o l t a g e //
8 // f i r s t p a r t //
9 // d i g i t a l i n p u t i s 1 0 1 0 0 1 / /
10 Vo =(10*2^0+0+0+10*2^3+0+10*2^5) /2^6; // o u t p u t v o l t a g e
//
11 printf ( ’ o u t p u t v o l t a g e f o r d i g i t a l i n p u t 101001= %f
v o l t \n ’ , Vo ) ; // d i s p l a y i n g t h e r e s u l t //
12 // s e c o n d p a r t //
13 // d i g i t a l i n p u t i s 1 1 0 0 0 1 / /
14 Vo1 =(10*1+0+0+0+10*16+10*32) /64; // o u t p u t v o l t a g e //
15 printf ( ’ o u t p u t v o l t a g e f o r i n p u t 110001= %f v o l t ’ , Vo1
) ; // d i s p l a y i n g t h e r e s u l t //
1 // c a l c u l a t i n g maximum f u l l s c a l e v o l t a g e //
2 // e x a m p l e 1 3 / /
3 clc
4 // c l e a r s t h e command window / / ;
5 clear
6 // c l e a r s //
7 // d i g i t a l i n p u t i s 1 1 1 1 1 1 1 1 / /
8 // h i g h i n p u t l e v e l =10V//
9 Vo =10*(1+2+4+8+16+32+64+128) /256; // o u t p u t v o l t a g e //
10 printf ( ’ maximum f u l l s c a l e v o l t a g e=%f v o l t ’ , Vo ) ; //
r e s u l t d i s p l a y e d //
1 // c a l c u l a t i n g r e q u i r e d d a t a //
2 // e x a m p l e 1 4 / /
3 clc
126
4 // c l e a r s t h e command window / / ;
5 clear
6 // c l e a r s //
7 // p a r t i //
8 // o u t p u t h i g h v o l t a g e =10V//
9 VMSB1 =1/2*10; // o u t p u t c a u s e d by MSB//
10 VMSB2 =1/4*10; // o u t p u t c a u s e d by s e c o n d MSB//
11 VMSB3 =1/8*10; // o u t p u t c a u s e d by t h i r d MSB//
12 VLSB =1/16*10; // o u t p u t c a u s e d by LSB//
13 printf ( ’ o u t p u t c a u s e d by msb=%f v o l t \n ’ , VMSB1 )
14 printf ( ’ o u t p u t c a u s e d by s e c o n d msb=%f v o l t \n ’ , VMSB2
)
15 printf ( ’ o u t p u t c a u s e d by t h i r d msb=%f\n ’ , VMSB3 )
16 printf ( ’ o u t p u t c a u s e d by l s b=%f v o l t \n ’ , VLSB )
17 // d i s p l a y i n g t h e r e s u l t s //
18 // p a r t i i //
19 // i n p u t i s 1 0 1 1 / /
20 V =1* VMSB1 +0+1* VMSB3 +1* VLSB ; // o u t p u t v o l t a g e //
21 printf ( ’ o u t p u t v o l t a g e a t i n p u t 1011= %f v o l t \n ’ ,V ) ;
// d i s p l a y i n g t h e r e s u l t //
22 // 3 r d p a r t //
23 Vo = VMSB1 + VMSB2 + VMSB3 + VLSB ; // f u l l s c a l e o u t p u t
v o l t a g e which o c c u r s a t 1 1 1 1 / /
24 printf ( ’ f u l l s c a l e o u t p u t v o l t a g e=%f v o l t ’ , Vo ) ; //
d i s p l a y i n g t h e r e s u l t //
1 // c a l c u l a t i n g no o f b i t s //
2 // e x a m p l e 1 5 / /
3 clc
4 // c l e a r s t h e command window / / ;
5 clear
6 // c l e a r s //
7 disp ( ’ r e s o l u t i o n =v o l t a g e c o r r e s p o n d i n g t o LSB ’ )
127
8 // w e i g h t o f LSB=1/2ˆn , where n=no . o f b i t s //
9 disp ( ’ V o l t a g e c o r r e s p o n d i n g t o LSB=1/2ˆn ∗ 1 0 ; which
s h o u l d be e q u a l t o r e s o l u t i o n which i s 10mv ’ )
10 disp ( ’ t h e r e f o r e 2ˆ n=1000 ’ )
11 disp ( ’ n=10 ’ ) ; // n e a r e s t v a l u e o f n which s a t i s f i e s
the e q u a t i o n i s 10//
12 // r e s u l t i s d i s p l a y e d //
1 // c a l c u l a t i n g r e q u i r e d d a t a //
2 // e x a m p l e 1 6 / /
3 clc
4 // c l e a r s t h e command window / / ;
5 clear
6 // c l e a r s //
7 V =10; // f u l l s c a l e v o l t a g e
8 W =1/2^12; // w e i g h t o f LSB//
9 R = V * W ; // r e s o l u t i o n //
10 printf ( ’ r e s o l u t i o n =%f v o l t \n ’ ,R ) ; // r e s u l t i s
d i s p l a y e d //
11 S = R ; // s t e p s i z e //
12 R1 = S / V *100; // p e r c e n t a g e r e s o l u t i o n //
13 printf ( ’ p e r c e n t a g e r e s o l u t i o n =%f p e r c e n t ’ , R1 )
14 // r e s u l t d i s p l a y e d //
1 // c a l c u l a t i n g r e q u i r e d d a t a //
2 // e x a m p l e 1 7 / /
3 clc
4 // c l e a r s t h e command window / / ;
5 clear
128
6 // c l e a r s //
7 // n=no . o f b i t s //
8 // r e s o l u t i o n =1/2ˆn ∗ 5//
9 R =5*10^ -3; // r e s o l u t i o n //
10 V =5; // f u l l s c a l e o u t p u t //
11 n = log ((5*10^ -3) /5) / log (.5)
12 // no . o f b i t s //
13 n = round ( n )
14 disp ( ’ no o f b i t s : ’ )
15 disp ( n ) ; // r e s u l t i s d i s p l a y e d //
16 S =1/2^ n *5; // s t e p s i z e //
17 R1 = S / V *100; // p e r c e n t a g e r e s o l u t i o n //
18 disp ( ’ p e r c e n t a g e r e s o l u t i o n : ’ )
19 disp ( R1 )
20 // r e s u l t i s d i s p l a y e d //
1 // c a l c u l a t i n g c o n v e r s i o n t i m e //
2 // e x a m p l e 1 8 / /
3 clc
4 // c l e a r s t h e command window / / ;
5 clear
6 // c l e a r s //
7 T =1/(1000*10^3) ; // t i m e f o r one c l o c k c y c l e //
8 disp ( T )
9 n =10; // no . o f b i t s //
10 t =(2^ n -1) * T ; // c o n v e r s i o n t i m e f o r 10 b i t A/D
c o n v e r t e r //
11 disp ( ’ c o n v e r s i o n t i m e f o r d i g i t a l ramp A/D c o n v e r t e r
: ’)
12 disp ( t ) ; // d i s p l a y i n g r e s u l t //
13 t1 = T *10;
14 disp ( ’ c o n v e r s i o n t i m e f o r 10 b i t s u c c e s s i v e
a p p r o x i m a t i o n A/D c o n v e r t e r : ’ )
129
15 disp ( t1 )
1 // c a l c u l a t i n g r e q u i r e d d a t a //
2 // e x a m p l e 1 9 / /
3 clc
4 // c l e a r s t h e command window / / ;
5 clear
6 // c l e a r s //
7 E =10; // c h a n g e i n i n p u t v o l t a g e //
8 n =8; // no . o f b i t s //
9 R = E /((2^ n ) -1) ; // r e s o l u t i o n //
10 disp ( ’ r e s o l u t i o n : ’ )
11 disp ( R ) ; // d i s p l a y i n g r e s u l t //
12 R1 =(1/((2^ n ) -1) ) *100; // p e r c e n t a g e r e s o l u t i o n //
13 disp ( ’ p e r c e n t a g e r e s o l u t i o n : ’ )
14 disp ( R1 ) ; // d i s p l a y i n g r e s u l t //
1 // f i n d i n g no . o f b i t s //
2 // e x a m p l e 2 0 / /
3 clc
4 // c l e a r s t h e command window / / ;
5 clear
6 // c l e a r s //
7 E =10; // c h a n g e i n i n p u t v o l t a g e //
8 R =10^ -3; // r e s o l u t i o n //
9 n = log ( E / R +1) / log (2) ; // no o f b i t s //
10 n = n +1;
11 n = round ( n )
12 disp ( ’ no o f b i t s : ’ )
130
13 disp ( n ) ; // r e s u l t d i s p l a y e d //
1 // f i n d i n g maximum c o n v e r s i o n t i m e //
2 // e x a m p l e 2 1 / /
3 clc
4 // c l e a r s t h e command window / / ;
5 clear
6 // c l e a r s //
7 c =2^10; //maximum no o f c o u n t s //
8 f =2*10^ -6; // c o u n t e r a d v a n c e r a t e o f 1 c o u n t p e r
s e c o n d //
9 T = c * f ; // c o n v e r s i o n t i m e //
10 disp ( ’ maximum c o n v e r s i o n t i m e : ’ )
11 disp ( T ) ; // d i s p l a y i n g r e s u l t //
1 // f i n d i n g r e q u i r e d d a t a //
2 // e x a m p l e 2 2 / /
3 clc
4 // c l e a r s t h e command window / / ;
5 clear
6 // c l e a r s //
7 f =500; // f r e q u e n c y //
8 F =2* f ; // a p e r t u r e r a t e //
9 t =5*10^ -6; // a p e r t u r e t i m e //
10 v =2* %pi * F * t ; // e r r o r i n v o l t a g e //
11 disp ( ’ e r r o r i n v o l t a g e : ’ )
12 disp ( v ) ; // d i s p l a y i n g r e s u l t //
13 V = v *100; // p e r c e n t a g e e r r o r //
14 disp ( ’ p e r c e n t a g e e r r o r : ’ )
131
15 disp ( V ) ; // d i s p l a y i n g r e s u l t //
1 // f i n d i n g a p e r t u r e t i m e //
2 // e x a m p l e 2 3 / /
3 clc
4 // c l e a r s t h e command window / / ;
5 clear
6 // c l e a r s //
7 a =.5; // p e r c e n t a g e a c c u r a c y //
8 f =1000; // a p e r t u r e r a t e //
9 t = a /(2* %pi ) *10^ -5; // a p e r t u r e t i m e //
10 disp ( ’ a p e r t u r e t i m e : ’ )
11 disp ( t ) ; // d i s p l a y i n g r e s u l t //
1 // c a l c u l a t i n g r e q u i r e d d a t a //
2 // e x a m p l e 2 4 / /
3 clc
4 // c l e a r s t h e command window / / ;
5 clear
6 // c l e a r s //
7 n =10; // no . o f b i t s //
8 q =1/(2^ n ) *100; // q u a n t i z a t i o n e r r o r expressed as
p e r c e n t a g e //
9 disp ( ’ q u a n t i z a t i o n p e r c e n t a g e e r r o r : ’ )
10 disp ( q ) ; // d i s p l a y i n g r e s u l t //
132
Scilab code Exa 8.25 finding digital output in binary form
1 // f i n d i n g d i g i t a l o u t p u t i n b i n a r y form //
2 // e x a m p l e 2 5 / /
3 clc
4 // c l e a r s t h e command window / / ;
5 clear
6 // c l e a r s //
7 V =1.085; // a n a l o g i n p u t v o l t a g e //
8 R =20*10^ -3; // r e s o l u t i o n //
9 n = V / R ; // no o f s t e p s //
10 n = round ( n ) ; // s o t h a t o u t p u t v o l t a g e p r o d u c e d i s less
t h a n a n a l o g i n p u t v o l t a g e //
11 num = dec2bin (n ,8) ; // c o n v e r t i n g t o b i n a r y //
12 disp ( ’ d i g i t a l o u t p u t : ’ )
13 disp ( num ) ; // d i s p l a y i n g r e s u l t i n b i n a r y form //
133