Chapter 13 Number Systems

Ex 13_2 PG-13.3

In [66]:
print "Representation of the binary number 1101.101 in power of 2"
print "N=(1*2**3)+(1*2**2)+(0*2**1)+(1*2**0)+(1*2**(-1))+(0*2**(-2))+(1*2**(-3))=13.625"
N=(1*2**3)+(1*2**2)+(0*2**1)+(1*2**0)+(1*2**(-1))+(0*2**(-2))+(1*2**(-3))
print "\n The decimal equivalent of binary no 1101.101 is: %.3f"%(N)
Representation of the binary number 1101.101 in power of 2
N=(1*2**3)+(1*2**2)+(0*2**1)+(1*2**0)+(1*2**(-1))+(0*2**(-2))+(1*2**(-3))=13.625

 The decimal equivalent of binary no 1101.101 is: 13.625

Ex 13_3 PG-13.3

In [67]:
## print "representation of the number 567 in power of 8"
print "N=(5*8**2)+(6*8**1)+(7*8**0)=375"
print " Therefore decimal equivalent of 567 is: "
N=(5*8**2)+(6*8**1)+(7*8**0)
print "%.0f"%(N)
N=(5*8**2)+(6*8**1)+(7*8**0)=375
 Therefore decimal equivalent of 567 is: 
375

Ex 13_4 PG-13.4

In [68]:
print "Representation of the hexadecimalnumber 3FD in power of 16"
print "N=(3*16**2)+(F*16**1)+(D*16**0)=1021"
print ' The Decimal equivalent of the Binary number 3FD is:'#
N=(3*16**2)+(15*16**1)+(13*16**0)
print "%.0f"%(N)
Representation of the hexadecimalnumber 3FD in power of 16
N=(3*16**2)+(F*16**1)+(D*16**0)=1021
 The Decimal equivalent of the Binary number 3FD is:
1021

Ex 13_5 PG-13.5

In [69]:
print 'The Decimal equivalent of the number 231.23 with base 4 is: '
x=(2*4**2)+(3*4**1)+(1*4**0)+(2*4**(-1))+(3*4**(-2))
print "%.4f"%(x)
The Decimal equivalent of the number 231.23 with base 4 is: 
45.6875

Ex 13_6 PG-13.5

In [70]:
print "decimal from 0 to 9 in radix 5: "
for i in range(0,10):
    a=i/5#
    b=(i%5)#
    print "     \t\t\t\t%d=%d%d"%(i,a,b)##conversion from decimal to radix 5
 
decimal from 0 to 9 in radix 5: 
     				0=00
     				1=01
     				2=02
     				3=03
     				4=04
     				5=10
     				6=11
     				7=12
     				8=13
     				9=14

Ex 13_7 PG-13.7

In [71]:
print "Conversion of binary no 111101100 to octal equivalent is :"
a='111101100'#
x=int(a,2)# decimal
y=oct(x)# octal
print "%s"%(y)
Conversion of binary no 111101100 to octal equivalent is :
0754

Ex 13_8 PG-13.7

In [72]:
print "conversion of octal no 634 to binary equivalent is :"
a='634'
x=int(a,8)#first we convert to decimal
y=bin(x)
print y[2:]
conversion of octal no 634 to binary equivalent is :
110011100

Ex 13_9 PG-13.7

In [73]:
from math import floor
print "conversion of octal no 725.63 to binary equivalent"
a=725.63#
#first we convert the number 725.63(octal)to decimal
x=(7*8**2)+(2*8**1)+(5*8**0)+(6*8**(-1))+(3*8**(-2))#
#then we convert the decimal to binary
z=(x%1)
x=floor(x)##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=floor(x)#
    c=c+1# 

for i in range(0,10):##converting the values after the decimal point into binary
    z=z*2#
    q=floor(z)#
    d=d+q/(10**i)#
    if z>=1:
        z=z-1#
    

s=b+d#
print "\n The binary equivalent of the given octal number 725.63 is =%.6f"%(s)#
conversion of octal no 725.63 to binary equivalent

 The binary equivalent of the given octal number 725.63 is =111010102.100110

Ex 13_10 PG-13.7

In [74]:
print "hexadecimal equivalent of 1101100010011011 binary is : "
a='1101100010011011'#
x=int(a,2)#
y=hex(x)#
print y[2:]
hexadecimal equivalent of 1101100010011011 binary is : 
d89b

Ex 13_11 PG-13.8

In [75]:
print "Binary equivalent of 3FD hexadecimal is :"
a='3FD'
y = bin(int(a, 16))[2:]
print '00%s'%y
Binary equivalent of 3FD hexadecimal is :
001111111101

Ex 13_13 PG-13.9

In [76]:
print "conversion of octal no 615 to its hexadecimal equivalent :",
a='615'#
x=int(a,8)
y=hex(x)
print y[2:]
conversion of octal no 615 to its hexadecimal equivalent : 18d

Ex 13_14 PG-13.9

In [77]:
print "conversion of hexadecimal no 25B to its octal equivalent : ",
a='25B'#
x=int(a,16)
y=oct(x)
print y[2:]
conversion of hexadecimal no 25B to its octal equivalent :  133

Ex 13_15 PG-13.10

In [78]:
print "conversion of binary no 1101.1 to its decimal equivalent =",
N=(1*2**3)+(1*2**2)+(0*2**1)+(1*2**0)+(1*2**(-1))#
print " %.1f"%(N)
conversion of binary no 1101.1 to its decimal equivalent =  13.5

Ex 13_16 PG-13.10

In [79]:
print "conversion of octal no 475.25 to its decimal equivalent ="
N=(4*8**2)+(7*8**1)+(5*8**0)+(2*8**(-1))+(5*8**(-2))#
print " %.5f"%(N)
conversion of octal no 475.25 to its decimal equivalent =
 317.32812

Ex 13_17 PG-13.10

In [80]:
print "conversion of hexadecimal no 9B2.1A to its decimal equivalent ="
N=(9*16**2)+(11*16**1)+(2*16**0)+(1*16**(-1))+(10*16**(-2))#
print " %.1f"%(N)
conversion of hexadecimal no 9B2.1A to its decimal equivalent =
 2482.1

Ex 13_18 PG-13.10

In [81]:
print "Conversion of the number 3102.12 \n with base 4 to its decimal equivalent ="
N=(3*4**3)+(1*4**2)+(0*4**1)+(2*4**0)+(1*4**(-1))+(2*4**(-2))#
print " %.3f"%(N)
Conversion of the number 3102.12 
 with base 4 to its decimal equivalent =
 210.375

Ex 13_19 PG-13.10

In [82]:
print "Conversion of the number 614.15 with base 7 to its decimal equivalent ="
N=(6*7**2)+(1*7**1)+(4*7**0)+(1*7**(-1))+(5*7**(-2))#
print " %.4f"%(N)
Conversion of the number 614.15 with base 7 to its decimal equivalent =
 305.2449

Ex 13_20 PG-13.11

In [83]:
print "Conversion of decimal number 37 to its binary equivalent =",
a=37#
x=bin(a)#
print "%s"%(x)[2:]
Conversion of decimal number 37 to its binary equivalent = 100101

Ex 13_21 PG-13.12

In [84]:
print "conversion of decimal number 214 to its octal equivalent =",
a=214#
x=oct(a)
print x[2:]
conversion of decimal number 214 to its octal equivalent = 26

Ex 13_22 PG-13.12

In [85]:
print "conversion of decimal number 3509 to its hexadecimal equivalent =",
a=3509#
x=hex(a)
print x[2:]
conversion of decimal number 3509 to its hexadecimal equivalent = db5

Ex 13_23 PG-13.13

In [86]:
print "conversion of decimal number 54 base  to a number with base 4 =",
a=54
base = 4
def base10toN(num, base):
    """Change ``num'' to given base
    Upto base 36 is supported."""

    converted_string, modstring = "", ""
    currentnum = num
    if not 1 < base < 37:
        raise ValueError("base must be between 2 and 36")
    if not num:
        return '0'
    while currentnum:
        mod = currentnum % base
        currentnum = currentnum // base
        converted_string = chr(48 + mod + 7*(mod > 10)) + converted_string
    return converted_string
print base10toN(a,base)
conversion of decimal number 54 base  to a number with base 4 = 312

Ex 13_24 PG-13.13

In [87]:
from math import floor
print "Conversion of decimal number 0.8125 base  to its binary equivalent "
a=0.8125
z=(a%1)#
d=0#
for i in range(0,10):##converting the values after the decimal point into binary
    z=z*2#
    q=floor(z)#
    d=d+q/(10**i)#
    if z>=1 :
        z=z-1#
    

s=d#
print "\nThe binary equivalent of the given decimal number 0.8125 is = %.4f"%(s)#
Conversion of decimal number 0.8125 base  to its binary equivalent 

The binary equivalent of the given decimal number 0.8125 is = 1.1010

Ex 13_25 PG-13.14

In [88]:
from math import floor
print "Conversion of decimal number 0.95 base  to its binary equivalent "
a=0.95#
z=(a%1)#
d=0#
for i in range(0,10):##converting the values after the decimal point into binary
    z=z*2#
    q=floor(z)#
    d=d+q/(10**i)#
    if z>=1:
        z=z-1#
    

s=d#
print "\n The binary equivalent of the given decimal number 0.95 is = %.7f"%(s)#
Conversion of decimal number 0.95 base  to its binary equivalent 

 The binary equivalent of the given decimal number 0.95 is = 1.1110011

Ex 13_26 PG-13.14

In [89]:
print "Conversion of decimal number 0.640625 base  to its octal equivalent ="
a=0.640625
z=(a%1)#
d=0#
for i in range(0,10):##converting the values after the decimal point into octal
    z=z*8#
    q=floor(z)#
    d=d+q/(10**i)#
    if z>=1:
        z=z-q#
    

s=d#
print "\nThe octal equivalent of the given decimal number 0.640625 is = %.2f"%(s)#
Conversion of decimal number 0.640625 base  to its octal equivalent =

The octal equivalent of the given decimal number 0.640625 is = 5.10

Ex 13_27 PG-13.14

In [90]:
from math import floor
print "Conversion of decimal number 0.1289062 base  to its hexadecimal equivalent "
a=0.1289062
z=(a%1)#
d=0#
for i in range(0,10):##converting the values after the decimal point into octal
    z=z*16#
    q=floor(z)#
    d=d+q/(10**i)#
    if z>=1 :
        z=z-q#
    

s=d#
print "\nThe hexadecimal equivalent of the given decimal number 0.640625 is = %.3f"%(s)#
Conversion of decimal number 0.1289062 base  to its hexadecimal equivalent 

The hexadecimal equivalent of the given decimal number 0.640625 is = 2.167

Ex 13_28 PG-13.14

In [91]:
from math import floor
print "Conversion of decimal number 24.6 base  to its binary equivalent "
a=24.6#
z=(a%1)
x=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=floor(x)#
    c=c+1#

for i in range(0,10):##converting the values after the decimal point into binary
    z=z*2#
    q=floor(z)#
    d=d+q/(10**i)#
    
    if z>=1 :
        z=z-1#
    

s=b+d#
print "\nThe binary equivalent of the given decimal number 24.6 is =%.5f"%(s)#
Conversion of decimal number 24.6 base  to its binary equivalent 

The binary equivalent of the given decimal number 24.6 is =11001.00110

Ex 13_29 PG-13.15

In [92]:
from math import floor
print "Conversion of decimal number 35.45 to its octal equivalent "
a=35.45#
z=(a)
x=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 octal
    y=(x%8)#
    b=b+(10**c)*y#
    x=x/8#
    x=floor(x)#
    c=c+1#

for i in range(0,10):##converting the values after the decimal point into octal
    z=z*8#
    q=floor(z)#
    d=d+q/(10**i)#
    if z>=1 :
        z=z-q#


s=b+d#
print "\nThe octal equivalent of the given decimal number 35.45 is =%.5f"%(s)#
Conversion of decimal number 35.45 to its octal equivalent 

The octal equivalent of the given decimal number 35.45 is =326.46315

Ex 13_32 Pg-18

In [93]:
from math import floor
print "\n\n   Conversion of hexadecimal number 2AC5.D to \n\n"

#conversion into decimal form
N=(2*16**3)+(10*16**2)+(12*16**1)+(5*16**0)+(13*16**(-1))
print "   Decimal form = %.4f\n\n"%(N)

#conversion into octal form
#we take the value of the decimal form and convert it to octal form
z=(N%1)
x=floor(N)##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%8)#
    b=b+(10**c)*y#
    x=x/8#
    x=floor(x)#
    c=c+1#

for i in range(0,10):##converting the values after the decimal point intooctal
    z=z*8#
    q=floor(z)#
    d=d+q/(10**i)#
    if z>=1:
        z=z-q#
    

s=b+d#
print "   Octal  form = %.2f\n\n"%(s)#

#conversion into binary form
#we take the value of the decimal form and convert it to octal form
z=(N%1)
x=floor(N)##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=floor(x)#
    c=c+1#

for i in range(0,10):##converting the values after the decimal point into binary
    z=z*2#
    q=floor(z)#
    d=d+q/(10**i)#
    
    if z>=1 :
        z=z-1#
    

s=b+d#
print "   Binary  form : "
print "\n       Integer part 2AC5 = 00%.0f"%(b)
print "\n       Decimal part 0.D = %.4f00"%(d)

   Conversion of hexadecimal number 2AC5.D to 


   Decimal form = 10949.8125


   Octal  form = 25311.40


   Binary  form : 

       Integer part 2AC5 = 0010101011000101

       Decimal part 0.D = 1.101000

Ex 13_33 Pg-18

In [94]:
from math import sqrt
print "\n    i)\n    Detemination of value of base x \n"
print "    (193)_x = (623)_8\n\n"
print "    First we convert (623)_8 octal into decimal\n\n"
N=(6*8**2)+(2*8**1)+(3*8**0)
print "          (623)_8 = (%.0f)_10\n\n"%(N)
print "   Therefore (193)_x = (%.0f)_10\n\n"%(N)
print "   (1*x**2)+(9*x**1)+(3*x**0) = %.0f\n\n"%(N)
print "    x**2 + 9x + 3 = %.0f\n\n"%(N)
print "    x**2 + 9x %.0f = 0\n\n"%(3-N)
a=1#
b=9#
c=-400
x1=(-b+sqrt(b**2-4*a*c))/(2*a)#
x2=(-b-sqrt(b**2-4*a*c))/(2*a)#
print "Therefore x1 = %.0f\n          x2 = %.0f"%(x1,x2)
print "\n   Negative is not applicable. Therefore x = %.0f"%(x1)
print "\n   Hence (193)_%.0f = (623)_8\n\n"%(x1)

print "\n    ii)\n    Detemination of value of base x \n"
print "    (225)_x = (341)_8\n\n"
print "    First we convert (341)_8 octal into decimal\n\n"
N=(3*8**2)+(4*8**1)+(1*8**0)
print "          (341)_8 = (%.0f)_10\n\n"%(N)
print "Therefore (225)_x = (%.0f)_10\n\n"%(N)
print "   (2*x**2)+(2*x**1)+(2*x**0) = %.0f\n\n"%(N)
print "    2x**2 + 2x +5 = %.0f\n\n"%(N)
print "    2x**2 + 2x %.0f= 0\n\n"%(5-N)
a=2#
b=2#
c=-200
x1=(-b+sqrt(b**2-4*a*c))/(2*a)#
x2=(-b-sqrt(b**2-4*a*c))/(2*a)#
print "Therefore x1 = %.0f\n          x2 = %.0f"%(x1,x2)
print "\n   Negative is not applicable. Therefore x = %.0f"%(x1)
print "\n   Hence (225)_%.0f = (341)_8\n\n"%(x1)

print "\n    iii)\n    Detemination of value of base x \n"
print "    (211)_x = (152)_8\n\n"
print "    First we convert (152)_8 octal into decimal\n\n"
N=(1*8**2)+(5*8**1)+(2*8**0)
print "          (152)_8 = (%.0f)_10\n\n"%(N)
print "Therefore (211)_x = (%.0f)_10\n\n"%(N)
print "   (2*x**2)+(1*x**1)+(1*x**0) = %.0f\n\n"%(N)
print "    2x**2 + 1x +1 = %.0f\n\n"%(N)
print "    2x**2 + 1x %.0f = 0\n\n"%(1-N)
a=2#
b=1#
c=-105
x1=(-b+sqrt(b**2-4*a*c))/(2*a)#
x2=(-b-sqrt(b**2-4*a*c))/(2*a)#
print "Therefore x1 = %.0f\n          x2 = %.0f"%(x1,x2)
print "\n   Negative is not applicable. Therefore x = %.0f"%(x1)
print "\n   Hence (211)_%.0f = (152)_8\n\n"%(x1)
    i)
    Detemination of value of base x 

    (193)_x = (623)_8


    First we convert (623)_8 octal into decimal


          (623)_8 = (403)_10


   Therefore (193)_x = (403)_10


   (1*x**2)+(9*x**1)+(3*x**0) = 403


    x**2 + 9x + 3 = 403


    x**2 + 9x -400 = 0


Therefore x1 = 16
          x2 = -25

   Negative is not applicable. Therefore x = 16

   Hence (193)_16 = (623)_8



    ii)
    Detemination of value of base x 

    (225)_x = (341)_8


    First we convert (341)_8 octal into decimal


          (341)_8 = (225)_10


Therefore (225)_x = (225)_10


   (2*x**2)+(2*x**1)+(2*x**0) = 225


    2x**2 + 2x +5 = 225


    2x**2 + 2x -220= 0


Therefore x1 = 10
          x2 = -11

   Negative is not applicable. Therefore x = 10

   Hence (225)_10 = (341)_8



    iii)
    Detemination of value of base x 

    (211)_x = (152)_8


    First we convert (152)_8 octal into decimal


          (152)_8 = (106)_10


Therefore (211)_x = (106)_10


   (2*x**2)+(1*x**1)+(1*x**0) = 106


    2x**2 + 1x +1 = 106


    2x**2 + 1x -105 = 0


Therefore x1 = 7
          x2 = -8

   Negative is not applicable. Therefore x = 7

   Hence (211)_7 = (152)_8


Ex 13_34 Pg-20

In [95]:
print "  1's complement of 1101 = ",
x='1101'

def bitcmp(a):
    a= str.replace(a,'0','x')
    a = str.replace(a,'1','0')
    a= str.replace(a,'x','1')
    return a
z = bitcmp(x)

print "%s"%(z)
  1's complement of 1101 =  0010

Ex 13_35 Pg-20

In [96]:
print "1's complement of 10111001 = ",
x='10111001'
def bitcmp(a):
    a= str.replace(a,'0','x')
    a = str.replace(a,'1','0')
    a= str.replace(a,'x','1')
    return a
z = bitcmp(x)

print "%s"%(z)
1's complement of 10111001 =  01000110

Ex 13_36 Pg-21

In [97]:
print "  2's complement 1001 is : ",
x='1001'#
def bitcmp(a):
    a= str.replace(a,'0','x')
    a = str.replace(a,'1','0')
    a= str.replace(a,'x','1')
    return a
z = bitcmp(x)
z = int(z,2)+1
z = bin(z)
print "0%s"%(z[2:])
  2's complement 1001 is :  0111

Ex 13_37 Pg-21

In [98]:
print "2's complement 10100011 is : ",
x='10100011'
z = bitcmp(x)
z = int(z,2)+1
z = bin(z)
print "0%s"%(z[2:])
2's complement 10100011 is :  01011101

Ex 13_38 Pg-22

In [99]:
print "7's complement of(612)_8 is : ",
x='612'
x=int(x,8)
x = bin(x)
x = bitcmp(x)[2:]
x = int(x,2)
x = oct(x)
print x
7's complement of(612)_8 is :  0165

Ex 13_40 Pg-22

In [100]:
print "15's complement (A9B)_16 is :",
x='A9B'#
def hex15cmp(n):
    nn = int(n,16)
    nn = bin(nn)
    nn = bitcmp(nn)[2:]
    nn = int(nn,2)
    nn = hex(nn)
    return nn[2:]

y=hex15cmp(x)##hexadecimal to decimal conversion#
print "%s"%y
15's complement (A9B)_16 is : 564

Ex 13_41 Pg-23

In [101]:
print "16's complement (A8C)_16 is : ",
x='A8C'#
y=hex15cmp(x)##hexadecimal 15's comp
y = hex(int(y,16)+1)[2:]
print "%s"%(y)
16's complement (A8C)_16 is :  574

Ex 13_42 Pg-23

In [102]:
x='1010'#
y='0011'#
#binary to decimal conversion#
x=int(x,2)
y=int(y,2)
z=x+y#
a=bin(z)[2:]#decimal to binary conversion#
print 'the addition of given numbers is: ',
print "%s"%(a)
the addition of given numbers is:  1101

Ex 13_43 Pg-23

In [103]:
x=28#
y=15#
# decimal to binary  conversion#
z=x+y#
a=bin(z)
print 'the addition of given numbers in binary is: ',
print "%s"%(a)[2:]
print '\n the addition of given numbers in decimal is: ',
print "%.0f"%(z)
the addition of given numbers in binary is:  101011

 the addition of given numbers in decimal is:  43