Chapter 9: Digital Electronics

Example 9.8, Page 176

In [7]:
#Initialization
ni1=11010               #binary number

#Calculation
def binary_decimal(ni): # Function to convert binary to decimal
    deci = 0;
    i = 0;
    while (ni != 0):
      rem = ni-int(ni/10.)*10
      ni = int(ni/10.);
      deci = deci + rem*2**i;
      i = i + 1;
    return deci

w=binary_decimal(ni1)              #calling the function

#Declaration
print'Decimal Equivalent = %f'%w
Decimal Equivalent = 26.000000

Example 9.9, Page 176

In [1]:
#Initialization
ni1=26                #Decimal number

#Calculation
def decimal_binary(ni): # Function to convert decimal to binary
    bini = 0;
    i = 1;
    while (ni != 0):
      rem = ni-int(ni/2)*2; 
      ni = int(ni/2);
      bini = bini + rem*i;
      i = i * 10;
    return bini

w=decimal_binary(ni1)              #calling the function

#Declaration
print'Binary Equivalent = %d'%w
Binary Equivalent = 11010

Example 9.10, Page 177

In [17]:
#Initializaton

no=34.6875            #decimal number
n_int = int(no);     # Extract the integral part
n_frac = no-n_int;   # Extract the fractional part

#Calculation

def decimal_binary(ni): # Function to convert decimal to binary
    bini = 0;
    i = 1;
    while (ni != 0):
      rem = ni-int(ni/2)*2; 
      ni = int(ni/2);
      bini = bini + rem*i;
      i = i * 10;
    return bini

def decifrac_binfrac(nf): # Function to convert binary fraction to decimal fraction
    binf = 0; i = 0.1;
    while (nf != 0):
      nf = nf*2;
      rem = int(nf); 
      nf = nf-rem;
      binf = binf + rem*i;
      i = i/10;
    return binf



#Result
print "Decimal equivalent of 34.6875 = %.4f"%(decimal_binary(n_int)+decifrac_binfrac(n_frac))
Decimal equivalent of 34.6875 = 100010.1011

Example 9.11, Page 177

In [26]:
#initialization
n='A013'                 #Hex number 

#Calculation
w=int(n, 16)              #Hex to Decimal Coversion


#Result
print'W = %d'%w
W = 40979

Example 9.12, Page 178

In [34]:
#Variable declaration
n=7046                 #Hex number 

#Calculations
h = hex(n)                             #decimal to hex conversion

#Result
print "The hexadecimal equivalent of 7046 is",h
The hexadecimal equivalent of 7046 is 0x1b86

Example 9.13, Page 178

In [32]:
#Initializaton

n='f851'                   #Hex Number

#Calculation

w=int(n, 16)              #Hex to Decimal Coversion

def decimal_binary(ni): # Function to convert decimal to binary
    bini = 0;
    i = 1;
    while (ni != 0):
      rem = ni-int(ni/2)*2; 
      ni = int(ni/2);
      bini = bini + rem*i;
      i = i * 10;
    return bini


w1=decimal_binary(w)              #calling the function


#Result
print "Decimal equivalent of 34.6875 = %.d"%(w1)
Decimal equivalent of 34.6875 = 1111100001010001

Example 9.14, Page 179

In [36]:
#Initialiation
ni1=111011011000100               #binary number

#Calculation
def binary_decimal(ni): # Function to convert binary to decimal
    deci = 0;
    i = 0;
    while (ni != 0):
      rem = ni-int(ni/10.)*10
      ni = int(ni/10.);
      deci = deci + rem*2**i;
      i = i + 1;
    return deci

w=binary_decimal(ni1)              #calling the function
h = hex(w)                             #decimal to hex conversion

#Result
print "The hexadecimal equivalent of 111011011000100 is",h
The hexadecimal equivalent of 111011011000100 is 0x76c4

Example 9.15, Page 182

In [19]:
#initialisation
x='9450'                           #decimal number to be convert

#calculation
digits = [int(c) for c in x]
zero_padded_BCD_digits = [format(d, '04b') for d in digits]

#results
print "Eqivalent BCD of 72 = ",
print ''.join(zero_padded_BCD_digits)
Eqivalent BCD of 72 =  1001010001010000

Example 9.16, Page 182

In [21]:
#Initialisation
BCD="0011 1000 0111 0110"             #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 =3876.
In [ ]: