# 11: Digital principles¶

## Example number 11.1, Page number 408¶

In [136]:
#importing modules
import math
from __future__ import division

#Variable declaration
a='1001101';    #binary number

#Calculation
b=int(a,2);    #number in decimal form

#Result
print "number in decimal form is",b

number in decimal form is 77


## Example number 11.2, Page number 408¶

In [137]:
#importing modules
import math
from __future__ import division

#Variable declaration
a='1001.101';    #binary number

#Calculation
def parse_bin(s):
t = s.split('.')
return int(t[0], 2) + int(t[1], 2) / 2.**len(t[1])

n=parse_bin(a);    #number in decimal form

#Result
print "number in decimal form is",n

number in decimal form is 9.625


## Example number 11.3, Page number 409¶

In [138]:
#importing modules
import math
from __future__ import division

#Variable declaration
a='246';     #octal number

#Calculation
n=int(a,8);    #number in decimal form

#Result
print "number in decimal form is",n

number in decimal form is 166


## Example number 11.4, Page number 409¶

In [139]:
#importing modules
import math
from __future__ import division

#Variable declaration
a='136.24';     #octal number

#Calculation
def parse_bin(s):
t = s.split('.')
return int(t[0], 8) + int(t[1], 8) / 8.**len(t[1])

n=parse_bin(a);    #number in decimal form

#Result
print "number in decimal form is",n

number in decimal form is 94.3125


## Example number 11.5, Page number 410¶

In [140]:
#importing modules
import math
from __future__ import division

#Variable declaration

#Calculation
n=int(a,16);    #number in decimal form

#Result
print "number in decimal form is",n

number in decimal form is 1278


## Example number 11.6, Page number 411¶

In [141]:
#importing modules
import math
from __future__ import division

#Variable declaration
a='10';    #binary number

#Calculation
b=int(a,2);    #number in decimal form

#Result
print "number in decimal form is",b

number in decimal form is 2


## Example number 11.7, Page number 411¶

In [142]:
#importing modules
import math
from __future__ import division

#Variable declaration
a='11111';    #binary number

#Calculation
b=int(a,2);    #number in decimal form

#Result
print "number in decimal form is",b

number in decimal form is 31


## Example number 11.8, Page number 412¶

In [143]:
#importing modules
import math
from __future__ import division

#Variable declaration
a=41;     #decimal number

#Calculation
n=bin(a);    #binary equivalent

#Result
print "binary equivalent is",n

binary equivalent is 0b101001


## Example number 11.9, Page number 412¶

In [144]:
#importing modules
import math
from __future__ import division

#Variable declaration
a=0.65625;     #decimal number

#Calculation
x1=a*2;
n1=int(x1);   #carry
x2=(x1-n1)*2;
n2=int(x2);   #carry
x3=x2*2;
n3=int(x3);   #carry
x4=(x3-n3)*2;
n4=int(x4);   #carry
x5=x4*2;
n5=int(x5);   #carry

#Result
print "since the given decimal number is fractional the binary equivalent will be 0.",n1,n2,n3,n4,n5

since the given decimal number is fractional the binary equivalent will be 0. 1 0 1 0 1


## Example number 11.10, Page number 413¶

In [145]:
#importing modules
import math
from __future__ import division

#Variable declaration
a='110111.101';    #binary number

#Calculation
def parse_bin(s):
t = s.split('.')
return int(t[0], 2) + int(t[1], 2) / 2.**len(t[1])

n=parse_bin(a);    #number in decimal form

#Result
print "number in decimal form is",n

number in decimal form is 55.625


## Example number 11.11, Page number 413¶

In [146]:
#importing modules
import math
from __future__ import division

#Variable declaration
a='1001';    #binary number
b='1111';    #binary number

#Calculation
sum=int(a,2)+int(b,2);    #sum of two binary numbers
sum=bin(sum);

#Result
print "sum of two binary numbers is",sum

sum of two binary numbers is 0b11000


## Example number 11.12, Page number 414¶

In [147]:
#importing modules
import math
from __future__ import division

#Variable declaration
a='011';    #binary number
b='101';    #binary number

#Calculation
diff=int(b,2)-int(a,2);    #difference of two binary numbers
diff=bin(diff);

#Result
print "difference of two binary numbers is",diff

difference of two binary numbers is 0b10


## Example number 11.13, Page number 415¶

In [148]:
#importing modules
import math
from __future__ import division

#Variable declaration
a='1011';    #binary number
b='0110';    #binary number

#Calculation
diff=int(a,2)-int(b,2);    #difference of two binary numbers
diff=bin(diff);

#Result
print "difference of two binary numbers is",diff

difference of two binary numbers is 0b101


## Example number 11.14, Page number 415¶

In [149]:
#importing modules
import math
from __future__ import division

#Variable declaration
n='010'    #binary number

#Calculation
def complement(n):
size = len(format(n, 'b'))
comp = n ^ ((1 << size) - 1)
return '0b1{0:0{1}b}'.format(comp, size)

a=complement((0b010))

#Result
print "1's complement of the number is",a

1's complement of the number is 0b101


## Example number 11.15, Page number 415¶

In [150]:
#importing modules
import math
from __future__ import division

#Variable declaration
n='1110'    #binary number

#Calculation
def complement(n):
size = len(format(n, 'b'))
comp = n ^ ((1 << size) - 1)
return '0b{0:0{1}b}'.format(comp, size)

a=complement((0b1110))

#Result
print "1's complement of the number is",a

1's complement of the number is 0b0001


## Example number 11.16, Page number 416¶

In [152]:
#importing modules
import math
from __future__ import division

#Variable declaration
a='1011';    #binary number
b='1101';    #binary number

#Calculation
diff=int(b,2)-int(a,2);    #difference of two binary numbers
diff=bin(diff);

#Result
print "difference of two binary numbers is",diff

difference of two binary numbers is 0b10


## Example number 11.17, Page number 416¶

In [153]:
#importing modules
import math
from __future__ import division

#Variable declaration
a='1100';    #binary number
b='1000';    #binary number

#Calculation
diff=int(b,2)-int(a,2);    #difference of two binary numbers
diff=bin(diff);

#Result
print "difference of two binary numbers is",diff

difference of two binary numbers is -0b100


## Example number 11.18, Page number 416¶

In [154]:
#importing modules
import math
from __future__ import division

#Variable declaration
n='0101'     #binary number
b='1'

#Calculation
def complement(n):
size = len(format(n, 'b'))
comp = n ^ ((1 << size) - 1)
return '0b1{0:0{1}b}'.format(comp, size)

a=complement((0b0101))
ans=int(a,2)+int(b,2);     #2's complement of number

#Result
print "2's complement of number is",bin(ans)

2's complement of number is 0b1011


## Example number 11.19, Page number 417¶

In [155]:
#importing modules
import math
from __future__ import division

#Variable declaration
n='101100'     #binary number
b='1'

#Calculation
def complement(n):
size = len(format(n, 'b'))
comp = n ^ ((1 << size) - 1)
return '0b{0:0{1}b}'.format(comp, size)

a=complement((0b101100))
ans=int(a,2)+int(b,2);     #2's complement of number

#Result
print "2's complement of number is",bin(ans)

2's complement of number is 0b10100


## Example number 11.20, Page number 417¶

In [156]:
#importing modules
import math
from __future__ import division

#Variable declaration
a=bin(6);
b=bin(2);

#Calculation
c= int(a,2)-int(b,2);    #difference

#Result
print "difference is",bin(c)

difference is 0b100


## Example number 11.21, Page number 418¶

In [157]:
#importing modules
import math
from __future__ import division

#Variable declaration
a=bin(3);
b=bin(5);

#Calculation
c=int(a,2)-int(b,2);    #difference

#Result
print "difference is",bin(c)

difference is -0b10


## Example number 11.22, Page number 420¶

In [158]:
#importing modules
import math
from __future__ import division

#Variable declaration
a='01101110'    #binary number

#Calculation

#Result

equivalent hexadecimal number is 0x6e


## Example number 11.23, Page number 420¶

In [159]:
#importing modules
import math
from __future__ import division

#Variable declaration
a='10110101111011'    #binary number

#Calculation

#Result

equivalent hexadecimal number is 0x2d7b


## Example number 11.24, Page number 421¶

In [160]:
#importing modules
import math
from __future__ import division

#Variable declaration
a='1100111111011'    #binary number

#Calculation

#Result

equivalent hexadecimal number is 0x19fb


## Example number 11.25, Page number 421¶

In [161]:
#importing modules
import math
from __future__ import division

#Variable declaration
a='11001110001'    #integral part of binary number
b='000101111001';    #fractional part of binary number

#Calculation

#Result

equivalent hexadecimal number is 0x671 . 0x179


## Example number 11.26, Page number 421¶

In [162]:
#importing modules
import math
from __future__ import division

#Variable declaration

#Calculation
def float_to_binary(num):
exponent=0
shifted_num=num
while shifted_num != int(shifted_num):
shifted_num*=2
exponent+=1
if exponent==0:
return '{0:0b}'.format(int(shifted_num))
binary='{0:0{1}b}'.format(int(shifted_num),exponent+1)
integer_part=binary[:-exponent]
fractional_part=binary[-exponent:].rstrip('0')
return '{0}.{1}'.format(integer_part,fractional_part)

def floathex_to_binary(floathex):
num = float.fromhex(floathex)
return float_to_binary(num)

ans=(floathex_to_binary('6B9'));     #equivalent binary number

#Result
print "equivalent binary number is",ans

equivalent binary number is 11010111001


## Example number 11.27, Page number 421¶

In [163]:
#importing modules
import math
from __future__ import division

#Variable declaration

#Calculation
def float_to_binary(num):
exponent=0
shifted_num=num
while shifted_num != int(shifted_num):
shifted_num*=2
exponent+=1
if exponent==0:
return '{0:0b}'.format(int(shifted_num))
binary='{0:0{1}b}'.format(int(shifted_num),exponent+1)
integer_part=binary[:-exponent]
fractional_part=binary[-exponent:].rstrip('0')
return '{0}.{1}'.format(integer_part,fractional_part)

def floathex_to_binary(floathex):
num = float.fromhex(floathex)
return float_to_binary(num)

ans=(floathex_to_binary('6D.3A'));     #equivalent binary number

#Result
print "equivalent binary number is",ans

equivalent binary number is 1101101.0011101


## Example number 11.28, Page number 422¶

In [164]:
#importing modules
import math
from __future__ import division

#Variable declaration
a=5000;    #decimal number

#Calculation

#Result

equivalent hexadecimal number is 0x1388


## Example number 11.29, Page number 422¶

In [165]:
#importing modules
import math
from __future__ import division

#Variable declaration
a=14619;    #decimal number

#Calculation

#Result

equivalent hexadecimal number is 0x391b


## Example number 11.30, Page number 422¶

In [166]:
#importing modules
import math
from __future__ import division

#Variable declaration
a=15101;    #decimal number

#Calculation

#Result

equivalent hexadecimal number is 0x3afd


## Example number 11.31, Page number 423¶

In [167]:
#importing modules
import math
from __future__ import division

#Variable declaration

#Calculation
ans=int(a,16);    #equivalent decimal number

#Result
print "equivalent decimal number is",ans

equivalent decimal number is 1208


## Example number 11.32, Page number 423¶

In [168]:
#importing modules
import math
from __future__ import division

#Variable declaration

#Calculation
ans=int(a,16);    #equivalent decimal number

#Result
print "equivalent decimal number is",ans
print "answer given in the book is wrong"

equivalent decimal number is 14923
answer given in the book is wrong


## Example number 11.33, Page number 424¶

In [169]:
#importing modules
import math
from __future__ import division

#Variable declaration

#Calculation
ans=int(a,16);    #equivalent decimal number

#Result
print "equivalent decimal number is",ans

equivalent decimal number is 11117


## Example number 11.34, Page number 424¶

In [170]:
#importing modules
import math
from __future__ import division

#Variable declaration

difference of two numbers is 0x217