Chapter 15: Digital Electronics

Example 15.1, Page 771

In [4]:
#Variable declaration
n = 25

#Calculations
b = bin(n)

#Result
print "The binary equivalent of 25 is",b
The binary equivalent of 25 is 0b11001

Example 15.2, Page 771

In [33]:
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
  

def binfrac_decifrac(nf): # Function to convert binary fraction to decimal fraction
    decf = 0;
    i = -1;
    while (i >= -3):
      nf = nf*10;
      rem = round(nf);    
      nf = nf-rem;
      decf = decf + rem*2**i;
      i = i - 1;
    return decf
   

n = 101.101;    # Initialize the binary number
n_int = int(n);     # Extract the integral part
n_frac = n-n_int;   # Extract the fractional part

#Result
print "Decimal equivalent of %7.3f = %5.3f"%(n, binary_decimal(n_int)+binfrac_decifrac(n_frac))
Decimal equivalent of 101.101 = 5.625

Example 15.3, Page 772

In [37]:
def octal_decimal(n): # Function to convert binary to decimal
    dec = 0;
    i = 0;
    while (n != 0):
      rem = n-int(n/10)*10; 
      n = int(n/10);
      dec = dec + rem*8**i;
      i = i + 1;
    return dec

n = 173;    # Initialize the octal number

#Result
print "Decimal equivalent of %d = %d"%(n, octal_decimal(n)); 
Decimal equivalent of 173 = 123

Example 15.4, Page 772

In [17]:
#Variable declaration
n = 278

#Calculations
o = oct(n)

#Result
print "The octal equivalent of 278 is",o
The octal equivalent of 278 is 0426

Example 15.5, Page 772

In [21]:
#Variable declaration
n1 = '10001100'
n2 = '1011010111'

#Calculations
x1 = hex(int(n1,2))
x2 = hex(int(n2,2))

#Results
print "The hexadecimal equivalent of 10001100 is",x1
print "The hexadecimal equivalent of 1011010111 is",x2
The hexadecimal equivalent of 10001100 is 0x8c
The hexadecimal equivalent of 1011010111 is 0x2d7

Example 15.6, Page 772

In [23]:
#Variable declaration
n = 72905

#Calculations
h = hex(n)

#Result
print "The hexadecimal equivalent of 278 is",h
The hexadecimal equivalent of 278 is 0x11cc9

Example 15.7, Page 773

In [27]:
#Variable declaration
x1 = 0b11101
x2 = 0b10111

#Calculations
x = bin(x1+x2)

#Result
print "The required result is",x
The required result is 0b110100

Example 15.8, Page 773

In [49]:
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

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

def binfrac_decifrac(nf): # Function to convert binary fraction to decimal fraction
    decf = 0;
    i = -1;
    while (i >= -3):
      nf = nf*10;
      rem = round(nf);    
      nf = nf-rem;
      decf = decf + rem*2.**i;
      i = i - 1;
    return decf 

bin1 = 1011.11;    # Initialize the first binary binber
bin2 = 1011.01;    # Initialize the second binary binber
bin1_int = int(bin1);     # Extract the integral part for first
bin1_frac = bin1-bin1_int;   # Extract the fractional part for second
bin2_int = int(bin2);     # Extract the integral part for first
bin2_frac = bin2-bin2_int;   # Extract the fractional part for second
dec1 = binary_decimal(bin1_int)+binfrac_decifrac(bin1_frac);
dec2 = binary_decimal(bin2_int)+binfrac_decifrac(bin2_frac);
dec = dec1+dec2;
dec_int = int(dec);
dec_frac = dec-dec_int;

#Result
print "%7.2f + %7.2f = %8.2f"%(bin1, bin2, decimal_binary(dec_int)+decifrac_binfrac(dec_frac))
1011.11 + 1011.01 = 10111.00

Example 15.9, Page 773

In [30]:
#Variable declaration
x1 = 0b0111
x2 = 0b1001

#Calculations
x = bin(x1-x2)

#Result
print "The required result is",x
The required result is -0b10

Example 15.10, Page 773

In [31]:
#Variable declaration
x1 = 0b1101
x2 = 0b1100

#Calculations
x = bin(x1*x2)

#Result
print "The required result is",x
#Incorrect solution in textbook
The required result is 0b10011100

Example 15.11, Page 774

In [32]:
#Variable declaration
x1 = 0b11001
x2 = 0b101

#Calculations
x = bin(x1/x2)

#Result
print "The required result is",x
The required result is 0b101