Chapter 19 - Digital Electronics

Example E1 - Pg 957

In [1]:
#Ex19_1 Pg-957 
#calculate decimal equivqlent of 101
a="101"
b=int(a,base=2)
print'%s %.f'%("The decimal equivqlent of 101 is",b)
The decimal equivqlent of 101 is 5

Example E2 - Pg 958

In [2]:
#Ex19_2 Pg-958
#decimal equivqlent of 11101
bina='11101'; #binary input
dec=int(bina,base=2) #decimal output
print("The decimal equivqlent of 11101 is")
print(dec)
The decimal equivqlent of 11101 is
29

Example E3 - Pg 958

In [3]:
#Ex19_3 Pg-958
#decimal equivqlent of 0.101
a=1
b=0
c=1
dec=a*2**(-1)+b*2**(-2)+c*2**(-3) #decimal output
print("The decimal equivqlent of 0.101 is")
print(dec)
The decimal equivqlent of 0.101 is
0.625

Example E4 - Pg 958

In [4]:
#Ex19_4 Pg-958
#The decimal equivqlent of 0.1101
a=1
b=1
c=0
d=1
dec=a*2**(-1)+b*2**(-2)+c*2**(-3)+d*2**(-4) #decimal output
print("The decimal equivqlent of 0.1101 is")
print(dec)
The decimal equivqlent of 0.1101 is
0.8125

Example E5 - Pg 958

In [5]:
#Ex19_5 Pg-958 
# decimal equivqlent of 10101.101
#Integer part
bina='10101'; #binary input
dec_I=int(bina,base=2) #decimal output

#Decimal part
a=1
b=0
c=1
dec_D=a*2**(-1)+b*2**(-2)+c*2**(-3)
dec=dec_I+dec_D #decimal output
print("The decimal equivqlent of 10101.101 is")
print(dec)
The decimal equivqlent of 10101.101 is
21.625

Example E6 - Pg 959

In [6]:
#Ex19_6 Pg-959
#binary equivalent of 9
dec=9 #decimal input
bina=bin(dec) #binary output
print("The binary equivalent of 9 is")
print(bina)
The binary equivalent of 9 is
0b1001

Example E7 - Pg 959

In [7]:
#Ex19_7 Pg-959 
#binary equivalent of 31
dec=31 #decimal input
bina=bin(dec) #binary output
print("The binary equivalent of 31 is")
print(bina)
The binary equivalent of 31 is
0b11111

Example E8 - Pg 959

In [8]:
#Ex19_8 Pg-959 
#binary equivalent of 13
dec=13 #decimal input
bina=bin(dec) #binary output
print("The binary equivalent of 13 is")
print(bina)
The binary equivalent of 13 is
0b1101

Example E9 - Pg 960

In [9]:
#Ex19_9 Pg-960
#Conversion of decimal number 31.65 base to its binary equivalent
import math
print("Conversion of decimal number 31.65 base to its binary equivalent ")
a=31.65;
z=a%1
x=math.floor(a);#separating the decimal from the integer part
b=0;
c=0;
d=0;
while(x>0):  #taking integer part into a matrix and convert to equivalent binary
	y=x%2;
	b=b+(10**c)*y;
	x=x/2.;
	x=math.floor(x);
	c=c+1;

for i in range(1,10):#converting the values after the decimal point into binary
    z=z*2;
    q=math.floor(z);
    d=d+q/(10**i);
    if z>=1:
    	z=z-1;

s=b+d;
print s
Conversion of decimal number 31.65 base to its binary equivalent 
11111.1010011

Example E10 - Pg 961

In [10]:
#Ex19_10 Pg-961
#Calculate the decimal equivqlent of 1111 and 1111111
a1="1111"
b1=int(a1,base=2)
print'%s %.f'%("(a)The decimal equivqlent of 1111 is",b1)
a2="1111111"
b2=int(a2,base=2)
print'%s %.f'%("(b)The decimal equivqlent of 1111111 is",b2)
(a)The decimal equivqlent of 1111 is 15
(b)The decimal equivqlent of 1111111 is 127

Example E11 - Pg 962

In [11]:
#Ex19_11 Pg-962 
#calculate The decimal equivqlent of 23
a="23"
b=int(a,base=8)
print'%s %.f'%("The decimal equivqlent of 23 is =",b)
The decimal equivqlent of 23 is = 19

Example E12 - Pg 962

In [12]:
#Ex19_12 Pg-962 
#calculate The decimal equivqlent of 23
a="257"
b=int(a,base=8)
print'%s %.f'%("The decimal equivqlent of 257 is =",b)
The decimal equivqlent of 257 is = 175

Example E13 - Pg 962

In [13]:
#Ex19_13 Pg-962 
#calculate The octal equivqlent of 257
a="175"
b=oct(175)
print b
0257

Example E14 - Pg 963

In [14]:
#Ex19_14 Pg-963
#calculate the octal equivalent of 0.85
a=.85
b=.5
octal=int(a*8.) 
octal2=int(b*64.)
print 'In octal representation it is .',octal,octal2
In octal representation it is . 6 32

Example E15 - Pg 963

In [15]:
#Ex19_15 Pg-963
a='34' #octal input
b=int(a,base=8) #decimal output
print b
28

Example E16 - Pg 963

In [16]:
#Ex19_16 Pg-963
x='357'
decimal=int(x,base=8) #to convert octal to decimal
binary=bin(decimal) #to convert binary to decimal
print 'In decimal=',decimal
print'In binary = ',binary
In decimal= 239
In binary =  0b11101111

Example E17 - Pg 963

In [17]:
#Ex19_17 Pg-963
#Integer part
bina='1011'; #binary input
dec_I=int(bina,base=2) #decimal output
oct_I=oct(dec_I) #octal output

#Decimal part
bina2='11010'; #binary input
dec_D=int(bina2,base=2) #decimal output
oct_D=oct(dec_D) #octal output
octa=oct_I + oct_D #final octal output
b = ([oct_I, oct_D ]) # combining intger and decimal part
print("The octal equivqlent of 1011.01101 is")
print(b)
The octal equivqlent of 1011.01101 is
['013', '032']

Example E18 - Pg 965

In [19]:
#Ex19_18 Pg-965
hexa='9AF' #hexadecimal input
dec=int(hexa,base=16) #decimal output
bina=bin(dec) #binary output
print("The binary equivalent of 9AF is")
print(bina)
The binary equivalent of 9AF is
0b100110101111

Example E19 - Pg 965

In [20]:
#Ex19_19 Pg-965
hexa='C5E2' #hexadecimal input
dec=int(hexa,base=16) #decimal output
bina=bin(dec) #binary output
print("The binary equivalent of C5E2 is")
print(bina)
The binary equivalent of C5E2 is
0b1100010111100010

Example E20 - Pg 957

In [21]:
#Ex19_20 Pg-957 
bina='10001100'; #binary input
dec=int(bina,base=2) #decimal output
hexa=hex(dec) #hexadecimal output
print("The hexadecimal equivqlent of 10001100 is")
print(hexa)
The hexadecimal equivqlent of 10001100 is
0x8c

Example E21 - Pg 965

In [22]:
#Ex19_21 Pg-965
#Integer part
hexa='F8E6'; #binary input
dec_I=int(hexa,base=16) #decimal output

#Decimal part
a=3 
b=9
dec=dec_I+a*16**(-1)+b*16**(-2) #decimal output 
print("The decimal equivalent of F8E6.39 is")
print(dec)
The decimal equivalent of F8E6.39 is
63718.2226562

Example E22 - Pg 996

In [23]:
#Ex19_22 Pg-966
dec=2479 #decimal input
hexa=hex(dec) #hexadecimal output
print("The Hexadecimal equivalent of 2479 is")
print(hex)
The Hexadecimal equivalent of 2479 is
<built-in function hex>

Example E23 - Pg 966

In [24]:
#Ex19_23 Pg-966
dec=65535 #decimal input
hexa=hex(dec) #hexadecimal output
print("The Hexadecimal equivalent of 65535 is")
print(hexa)

bina=bin(dec) #binary output
print("The Binary equivalent of 65535 is ")
print(bina)
The Hexadecimal equivalent of 65535 is
0xffff
The Binary equivalent of 65535 is 
0b1111111111111111

Example E24 - Pg 969

In [25]:
#Ex19_24 Pg-969
x=int('11100',2) #1st input
y=int('11010',2) #2nd input
z=x+y #binary addition
add=bin(z)
print(" 11100 + 11010 = ")
print(add)
 11100 + 11010 = 
0b110110

Example E25 - Pg 970

In [26]:
#Ex19_25 Pg-970
x=int('1101',2) #1st input
y=int('1010',2) #2nd input
z=x-y #subtraction
sub=bin(z)
print("1101-1010 =")
print(sub)
1101-1010 =
0b11

Example E26 - Pg 970

In [27]:
#Ex19_26 Pg-970
x=200 #1st input
y=125 #2nd input
z=x-y #subtraction
print("For decimal system 200 - 125 = ")
print(z)

a=hex(z) #hexadeciaml output of 200-125
print("For hexadecimal system C8 - 7D is")
print(a)

b=bin(z) #binary output of 200-125
print("For binary system 11001000 - 01111101 is")
print(b)
For decimal system 200 - 125 = 
75
For hexadecimal system C8 - 7D is
0x4b
For binary system 11001000 - 01111101 is
0b1001011

Example E27 - Pg 997

In [28]:
#Ex19_27 Pg-997
print("(A+B)(A+C) = AA + AC + AB + BC ")
#by distributive law
print("           = A + AC + AB +BC")
#by theorem(6)
print("           = A(1 + C) + AB + BC")
#by distributive law
print("           = A.1 + AB + BC ")
#by theorem(3)
print("           = A(B + 1) + BC")
#by distributive law
print("           = A + BC")
print("Therefore (A+B)(A+C) = A + BC")
(A+B)(A+C) = AA + AC + AB + BC 
           = A + AC + AB +BC
           = A(1 + C) + AB + BC
           = A.1 + AB + BC 
           = A(B + 1) + BC
           = A + BC
Therefore (A+B)(A+C) = A + BC

Example E28 - Pg 998

In [29]:
#Ex19_28 Pg-998
print("AB + A(B + C) + B(B + C)  = AB + AB + AC + BB + BC")
#using distributive law
print("                          = AB + AC + B +BC      ")
#using law 6
print("                          = AB + AC + B(1 + C)  ")
#taking common B from B + BC 
print("                          = AB + AC + B")
#using law 7 
print("                          = B(A + 1) + AC")
#taking common B from AB + B
print("                          = B + AC")
#using law 7
print("Therefore AB + A(B + C) + B(B + C) = B + AC")
AB + A(B + C) + B(B + C)  = AB + AB + AC + BB + BC
                          = AB + AC + B +BC      
                          = AB + AC + B(1 + C)  
                          = AB + AC + B
                          = B(A + 1) + AC
                          = B + AC
Therefore AB + A(B + C) + B(B + C) = B + AC

Example E30 - Pg 998

In [30]:
#Ex19_30 Pg-998
# question in the textbook is wrong7 
print("LHS : (A + B + C)(A + B + C) ")
print("        = AA + AB + AC + BA + BB +BC + CA + CB + CC")
#using distributive law
print("        = A + AB + AC + BA + B +BC + CA + CB + C")
#using law 6
print("        = A + AB + AC +BC + CB + C")
#using law 5
print("        = A(B + 1) + AC + B + C(B + 1)")
#taking A common from A+AB and C from CB+C
print("        = A + AC + B + C")
#using law 3
print("        = A + B + C(A + 1)")
#taking common C from AC+C
print("        = A + B + C")
#using law 3
print("Therefore (A'' + B + C)(A'' + B'' + C) = A'' + C")
LHS : (A + B + C)(A + B + C) 
        = AA + AB + AC + BA + BB +BC + CA + CB + CC
        = A + AB + AC + BA + B +BC + CA + CB + C
        = A + AB + AC +BC + CB + C
        = A(B + 1) + AC + B + C(B + 1)
        = A + AC + B + C
        = A + B + C(A + 1)
        = A + B + C
Therefore (A'' + B + C)(A'' + B'' + C) = A'' + C

Example E31 - Pg 999

In [32]:
#Ex19_31 Pg-999
print("LHS : (A+ C)(A'' + B) ")
print("         = AA'' + AB + CA'' + BC") #using distributive law
print("         = 0 + AB + CA'' + BC") #using law 8
print("         = AB + (A + A'')BC + CA''") #using law 7
print("         = AB + ABC + A''BC + CA''") 
#using distributive law
print("         = AB + ABC + A''C(B + 1)") 
#taking common A'C from A'BC + CA'
print("         = AB + ABC + A''C") #using law 3
print("         = AB(C + 1)+ A''C") 
#taking common AB from AB + ABC
print("         = AB + A''C") #using law 3
print("Therefore (A+ C)(A'' + B) = AB + A''C")
LHS : (A+ C)(A'' + B) 
         = AA'' + AB + CA'' + BC
         = 0 + AB + CA'' + BC
         = AB + (A + A'')BC + CA''
         = AB + ABC + A''BC + CA''
         = AB + ABC + A''C(B + 1)
         = AB + ABC + A''C
         = AB(C + 1)+ A''C
         = AB + A''C
Therefore (A+ C)(A'' + B) = AB + A''C