CHAPTER 26 : DIGITAL ELECTRONICS

Example 26.1 : Page 732

In [4]:
#Given
d=37;         #Given decimal number

#Calculation
b=int(bin(d)[2:]);                #Equivalent Octal number 

#Result
print("The equivalent binary number=%s."%b);
The equivalent binary number=100101.

Example 26.2 : Page number 733

In [5]:
#Given
d=23;      #Given decimal number

#Calculation
b=int(bin(d)[2:]);                   #Equivalent Octal number

#Result
print("The equivalent binary number=%d."%b);
The equivalent binary number=10111.

Example 26.3 : Page number 733

In [6]:
#Given 
b=0b110001;               #Given binary number

#Calculation
d=int(b);                   #Equivalent decimal number

#Result
print("Equivalent decimal number=%d."%d);
Equivalent decimal number=49.

Example 26.4 : Page number 735

In [7]:
#Given 
d1=76;                #Given decimal number
d2=255;               #Given decimal number
d3=372;               #Given decimal number

#Calculation
o1=int(oct(d1)[1:]);                   #Equivalent octal number
o2=int(oct(d2)[1:]);                   #Equivalent octal number
o3=int(oct(d3)[1:]);                   #Equivalent octal number


#Result
print("(i)   Equivalent octal number=%d."%o1);
print("(ii)  Equivalent octal number=%d."%o2);
print("(iii) Equivalent octal number=%d."%o3);
(i)   Equivalent octal number=114.
(ii)  Equivalent octal number=377.
(iii) Equivalent octal number=564.

Example 26.5 : Page number 735

In [64]:
#Given 
o=24.6;               #Given octal number

#Calculation
o_f=o%1;                             #Floating part of octal number
o_i=(int)(o-(o%1));                  #Integer part of octal number
d=int(str(o_i),8);                   #Equivalent decimal number

s=str(o_f);                         #String value of floating  part 
i=2
while(i<len(s)):
    d=d+int(s[i])*8**-(i-1);
    i+=1;
#Result
print("Equivalent decimal number=%.2f."%d);
Equivalent decimal number=20.75.

Example 26.6 : Page number 735

In [63]:
#Given 
d=177;               #Given decimal number

#Calculation
o=oct(d)[1:];                   #Equivalent octal number

b="";
for i in o:
    bo=bin(int(i))[2:];                     #Binary of individual octal digit
    b=b+(("0" if len(bo)==2 else ("00" if len(bo)==1 else"")) +bo);                   #Equivalent binary number
    
#Result
print("Equivalent octal number=%s."%o);
print("Equivalent binary number=%s."%b);
Equivalent octal number=261.
Equivalent binary number=010110001.

Example 26.7 : Page number 737

In [23]:
#Given 
d=541;               #Given decimal number

#Calculation
h=hex(d)[2:];                   #Equivalent hexadecimal number

#Result
print("Equivalent hexadecimal number=%s."%h);
Equivalent hexadecimal number=21d.

Example 26.8 : Page number 737

In [56]:
hex_to_dec={'0':0,'1':1,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9,'a':10,'b':11,'c':12,'d':13,'e':14,'f':15};
        
#Given 
d=378;               #Given decimal number

#Calculation
h=hex(d)[2:];                   #Equivalent Hexadecimal number


b="";
for i in h:
    bh=bin(hex_to_dec[i])[2:];                 #Binary of individual hexadecimaldigit
    b=b+(("0" if len(bh)==3 else ("00" if len(bh)==2 else ("000" if len(bh)==1 else "")))+bh);    #Equivalent binary number

#Result
print("Equivalent hexadeciaml number=%s."%h);
print("Equivalent binary number=%s."%b);
Equivalent hexadeciaml number=17a.
Equivalent binary number=000101111010.

Example 26.9 : Page number 737

In [58]:
#Given
h=0xB2F;        #Given hexadecimal number

#Calculation
o=oct(h)[1:];       #Equivalent octal number

#Result
print("Equivalent octal number=%s."%o);
Equivalent octal number=5457.

Example 26.10 : Page number 738

In [7]:
#Given
BCD="0100 0000 0010"             #Given BCD string
BCD_split=BCD.split(" ");        #Splitting th binary string into individual BCD 
d=0;
for i in range(len(BCD_split),0,-1):
    d+=int(BCD_split[len(BCD_split)-i],2)*10**(i-1);

#Result
print("The equivalent decimal =%d."%d);
    
The equivalent decimal =402.

Example 26.11 : Page number 745

In [29]:
print("Boolean Expression obtained from the circuit: \n Y'=A+B \n Y=((A+B).A)");
print("Truth Table:");
print("a\tb\tY'=A+B\t  Y=Y'.A");
for b in range(0,2):
    for a in range(0,2):
        Y_dash=1 if a or b else 0;
        Y=1 if Y_dash and a else 0;
        print("%d\t%d\t%d\t  %d"%(a,b,Y_dash,Y));
Boolean Expression obtained from the circuit: 
 Y'=A+B 
 Y=((A+B).A)
Truth Table:
a	b	Y'=A+B	  Y=Y'.A
0	0	0	  0
1	0	1	  1
0	1	1	  0
1	1	1	  1

Example 26.12 : Page number 745-746

In [30]:
print("Boolean Expression obtained from the circuit: \n Y'=A'.B \n Y=Y'+B'");
print("Truth Table:");
print("A\tB\tA'\tY'=A'.B\t  B'\tY=Y'+B'");
for b in range(0,2):
    for a in range(0,2):
        a_dash=1 if not a else 0;
        b_dash=1 if not b else 0;
        Y_dash=1 if a_dash and b else 0;
        Y=1 if Y_dash or b_dash else 0;
        print("%d\t%d\t%d\t%d\t  %d\t%d"%(a,b,a_dash,Y_dash,b_dash,Y));
Boolean Expression obtained from the circuit: 
 Y'=A'.B 
 Y=Y'+B'
Truth Table:
A	B	A'	Y'=A'.B	  B'	Y=Y'+B'
0	0	1	0	  1	1
1	0	0	0	  1	1
0	1	1	1	  0	1
1	1	0	0	  0	0