#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)
#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)
#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)
#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)
#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)
#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)
#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)
#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)
#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
#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)
#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)
#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)
#Ex19_13 Pg-962
#calculate The octal equivqlent of 257
a="175"
b=oct(175)
print b
#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
#Ex19_15 Pg-963
a='34' #octal input
b=int(a,base=8) #decimal output
print b
#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
#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)
#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)
#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)
#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)
#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)
#Ex19_22 Pg-966
dec=2479 #decimal input
hexa=hex(dec) #hexadecimal output
print("The Hexadecimal equivalent of 2479 is")
print(hex)
#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)
#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)
#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)
#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)
#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")
#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")
#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")
#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")