Chapter 17: Number Systems, Boolean Algebra and Digital Circuits

Example 1, Page 440

In [1]:
#Variable declaration
x=25

#Calculations&Results
s=int(bin(x)[2:])
print "1. Binary equivalent of 25 is ",s
y=576
s1=int(bin(y)[2:])
print "2. Binary equivalent of 576 is ",s1
1. Binary equivalent of 25 is  11001
2. Binary equivalent of 576 is  1001000000

Example 2, Page 440

In [2]:
#Variable declaration
s='1111'

#Calculations
x=int(s,2)

#Result
print "Decimal equivalent of 1111 is ",x
Decimal equivalent of 1111 is  15

Example 3, Page 440

In [63]:
import math

p = 1.;
#initialising variables
z = 0;
b = 0;
w = 0;
f = 0;
bin1 = 11.1101;
d = bin1%1;         #separating the decimal part and the integer part
d = d*10**10;
a = math.floor(bin1);#removing the decimal part
b = []
while(a>0):#loop to take the binary bits of integer into a matrix
    r = a%10;
    b.append(r);
    a = a/10;
    a =  math.floor(a);

for m in range(len(b)):#multiplying the bits of integer with their position values and adding
    c = m;
    f = f+b[m]*(2**c);

w = []
while(d>0):#loop to take the binary bits of decimal into a matrix
    e = d%2
    w.append(e)
    d = d /10;
    d = math.floor(d)

for n in range(len(w)):#multiplying the bits of decimal with their position values and adding
    z = z+w[n] *(0.5)**(11-n+1);

z = z*10000;
#rounding of to 4 decimal values
z = round(z);
z = z/10000;
print "The decimal equivalent of 11.1101 is  =  %.4f"%(f+z)
#answers differ due to usage of in-built functions
The decimal equivalent of 11.1101 is  =  3.2031

Example 4, Page 440

In [100]:
import math

#Variable declaration
q=0.;
b=0.;
s=0.;
a=4.625;

#Calculations
a=math.floor(a);#removing the decimal part
while(a>0):#taking integer part into a matrix and converting into equivalent binary
    x=(a%2);
    b=b+(10**q)*x;
    a=a/2;
    a=math.floor(a);
    q=q+1;

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

k=b+s;

#Result
print "The binary equivalent of 4.625 is =" ,k
The binary equivalent of 4.625 is = 100.0

Example 5, Page 440

In [29]:
#Variable declaration

def toStr(n,base):
   convertString = "0123456789ABCDEF"
   if n < base:
      return convertString[n]
   else:
      return toStr(n//base,base) + convertString[n%base]

dec=263
base=5

#Calculations
s=toStr(dec,base)

#Result
print "Equivalent of 263 in a code base 5 is ",s
Equivalent of 263 in a code base 5 is  2023

Example 6, Page 441

In [50]:
#Variable declaration
x=2

#Calculations
s=x+x
s1=bin(s)

#Result
print "Binary addition corresponding to decimal addition 2+2 is ",int(s1[2:])
Binary addition corresponding to decimal addition 2+2 is  100

Example 7, Page 441

In [51]:
#Variable declaration
x='11111'
y='1011'
z='101'
w='10'
v='1'

#Calculations
s1=int(x,2)
s2=int(y,2)
s3=int(z,2)
s4=int(w,2)
s5=int(v,2)
a=s1+s2+s3+s4+s5
b=int(bin(a)[2:])

#Results
print "Binary addition of 11111+1011+101+10+1 is ",b
print "Decimal equivalent corresponding to above binary addition is ",a
Binary addition of 11111+1011+101+10+1 is  110010
Decimal equivalent corresponding to above binary addition is  50

Example 8, Page 441

In [52]:
#Variable declaration
x='1101'
y='111'

#Calculations
s1=int(x,2)
s2=int(y,2)
a=s1-s2
s=int(bin(a)[2:])

#Result
print "Binary subtraction 1101-111 is =",s
Binary subtraction 1101-111 is = 110

Example 9, Page 442

In [56]:
#Variable declaration
hFE=30#hFE=dc current gain of given silicon transistor
VBE=0.8#VBE=base-emitter voltage drop at saturation
VCE=0.2#VCE=collector-emitter voltage drop at saturation
R1=15.*1000#resistance at the base side of the transistor in ohms
R2=100*1000#another resistance at the base side of the transistor in ohms
RL=2*1000#load resistance at the collector side of the transistor in ohms
VCC=10#VCC=collector supply voltage
VBB=-10#VBB=base supply voltage

#Calculations&Results
#If the input level is 0 volt i e vi=0,the open-circuited base voltage is given as
VB=VBB*(R1/(R1+R2))
print "For input level 0 V:"
print "As a bias of approximately 0V is sufficient to cut off a silicon emitter junction, it follows that transistor is cut off when vi=0"
print "When vi=0,the output voltage is vo=VCC=%.f V"%VCC
print "This indicates that the output is in state 1 when the input is in state 0"
#When the input level is 10 volt i e vi=10, we have to show that the transistor is in saturation
#The minimum base current for saturation is given by iB(min)=iC/hFE
iC=(VCC-VCE)/RL#collector current when the transistor saturates
iB=iC/hFE#iB=iB(min)=minimum base current for saturation in mA
i1=(10-VBE)/R1#i1=current through R1 resistor connected at the base side and here vi=10 is taken
i2=(VBE-VBB)/R2#i2=current through R2 resistor connected at the base side
iB1=i1-i2#iB1=actual base current
print "\nFor input level 10 V:"
if (iB1>iB):
    print "Since iB>iB(min),it is verified that the transistor is in saturation"#iB indicates actual base current & iB(min) indicates minimum base current for saturation
    print "When vi=10,the output voltage is vo=VCE(sat)=%.1f V"%VCE
    print "This indicates that the output is in state 0 when the input is in state 1"

print "Overall it has been thus verified that the circuit has performed the NOT operation"
For input level 0 V:
As a bias of approximately 0V is sufficient to cut off a silicon emitter junction, it follows that transistor is cut off when vi=0
When vi=0,the output voltage is vo=VCC=10 V
This indicates that the output is in state 1 when the input is in state 0

For input level 10 V:
Since iB>iB(min),it is verified that the transistor is in saturation
When vi=10,the output voltage is vo=VCE(sat)=0.2 V
This indicates that the output is in state 0 when the input is in state 1
Overall it has been thus verified that the circuit has performed the NOT operation

Example 10, Page 442

In [57]:
#Variable declaration
A=0
B=0

#Calculations&Results
C=(A|B)#bitwise OR operation is performed
print "Boolean expression C=A+B for inputs A=0 and B=0 is",C
A=1
B=0
C=(A|B)
print "Boolean expression C=A+B for inputs A=1 and B=0 is",C
A=1
B=1
C=(A|B)
print "Boolean expression C=A+B for inputs A=1 and B=1 is",C
Boolean expression C=A+B for inputs A=0 and B=0 is 0
Boolean expression C=A+B for inputs A=1 and B=0 is 1
Boolean expression C=A+B for inputs A=1 and B=1 is 1