# Decimal to binary conversion: 10
import math
#variable declaration
N = 10 # decimal no to be convered to binary
k = N
#Calculations
i = 0
b =[0,0,0,0,0,0]
while(N!=0):
b[i] = N%2
N = N/2
i = i+1
#Result
print("Binary equivalent of decimal %d is %d%d%d%d"%(k,b[3],b[2],b[1],b[0]))
# Decimal to binary conversion: 25
import math
#variable declaration
N = 25 # decimal no to be convered to binary
#Calculations
k = N
i = 0
b =[0,0,0,0,0,0]
while(N!=0):
b[i] = N%2
N = N/2
i = i+1
#Result
print("Binary equivalent of decimal %d is %d%d%d%d%d"%(k,b[4],b[3],b[2],b[1],b[0]))
# Binary to decimal:101110
import math
b0 = 0 # bit 0
b1 = 1 # bit 1
b2 = 1 # bit 2
b3 = 1 # bit 3
b4 = 0 # bit 4
b5 = 1 # bit 5
#Calculations
D = (2**0)*b0+(2**1)*b1+(2**2)*b2+(2**3)*b3+(2**4)*b4+(2**5)*b5
#Result
print("Decimal no is %d"%D)
# Decimal to binary conversion: 15,31
import math
#######################################################################################
# for N = 15
#variable declaration
N = 15 # decimal no to be convered to binary
#Calculations
k = N
i = 0
b =[0,0,0,0,0,0]
while(N!=0):
b[i] = N%2
N = N/2
i = i+1
#Result
print("(a)\nBinary equivalent of decimal %d is %d%d%d%d%d"%(k,b[4],b[3],b[2],b[1],b[0]))
########################################################################################
# For N =31
#variable declaration
N = 31 # decimal no to be convered to binary
#Calculations
k = N
i = 0
b =[0,0,0,0,0,0]
while(N!=0):
b[i] = N%2
N = N/2
i = i+1
#Result
print("Binary equivalent of decimal %d is %d%d%d%d%d"%(k,b[4],b[3],b[2],b[1],b[0]))
##########################################################################################
#Addition
c= bin(15+31)[2:]
print("(b) Addition of 15 and 31 is %s in binary. Decimal equivalent of %s is %d"%(c,c,int(c,2)))
# Binary subtraction
import math
#Variable declaration
n1 = 11001 # first number
n2 = 10001 # second number
#calculations
n1 = int(str(n1),2)
n2 = int(str(n2),2)
c = bin(n1-n2)[2:]
#Result
print("Subtraction result in binary = %s"%c)
# Binary subtraction
import math
#Variable declaration
n1 = 1010 # first number
n2 = 111 # second number
#calculations
n1 = int(str(n1),2)
n2 = int(str(n2),2)
c = bin(n1-n2)[2:]
#Result
print("Subtraction result in binary = %s"%c)
# 16- bit signed binary representation
import math
#Variable declaration
a = 8
b = -8
c = 165
d = -165
#Calculations
#c = bin(d)
a = format(a,'#018b')[2:]
b = format(b,'#018b')[3:]
c = format(c,'#018b')[2:]
d = format(d,'#018b')[3:]
str = '1'
#Result
print("In the leading bit we will have 1 to represent '-' sign\n")
print("(a) +8 --> %s"%(a))
print("(b) -8 --> %s%s"%(str,b))
print("(c) +167 --> %s"%(c))
print("(d) -167 --> %s%s"%(str,d))
a = format(-167%(1<<16),'016b')
b = format(167%(1<<16),'016b')
print a
print b
# 2's complement
import math
# variable declaration
a = int('00011111',2)
b = int('11100101',2)
c = int('11110111',2)
#Calculations
a = format(-a%(1<<8),'08b')
b = format(-b%(1<<8),'08b')
c = format(-c%(1<<8),'08b')
print("(a) 2's complement of 00011111 --> %s " %a)
print("(b) 2's complement of 11100101 --> %s " %b)
print("(c) 2's complement of 11110111 --> %s " %c)
# 8-bit number range
import math
#calculations
a = int('01111111',2)
b = int('10000000',2)
#Result
print("largest positive 8-bit no in %d and smallest negative number is -%d"%(a,b))
# 2's complement and addition,subtraction
import math
#Variable declaration
A = -24
B = 16
#Calculations
#2's complement
a = format(A%(1<<8),'08b')
b = format(-B%(1<<8),'08b')
# addition
c = bin(A+B)[3:]
c = format(-int(c,2)%(1<<8),'08b')
#Subtraction
d = bin(A-B)[3:]
d = format(-int(d,2)%(1<<8),'08b')
#Result
print("(a)\n2's complement of -24 --> %s " %a)
print("2's complement of 16 --> %s " %b)
print("(b) A+B = %s"%c)
print("(c) A-B = %s"%d)
# 2's complement and addition,subtraction
import math
#Variable declaration
A = -60
B = -28
#Calculations
#2's complement
a = format(A%(1<<8),'08b')
b = format(B%(1<<8),'08b')
# addition
c = bin(A+B)[3:]
c = format(-int(c,2)%(1<<8),'08b')
#Subtraction
x = B-A
d = bin(B-A)[2:]
d = format(int(d,2),'08b')
#Result
print("(a)\n2's complement of A --> %s " %a)
print("2's complement of B --> %s " %b)
print("(b) B+A = %s"%c)
print("(c) B-A = %s"%d)
# decimal to binary
# For N = 0.6875
#variable declaration
N = 0.6875 # decimal no to be convered to binary
#Calculations
k = N
i = 0
b =[0,0,0,0,0,0,0,0,0,0,0,0]
while(N!=0):
N = N*2
#print N
b[i]=0
#print b[i]
if (N>1) or(N==1):
N =N-1
b[i]=1
i=i+1
#Result
print("Binary equivalent of decimal %f is 0.%d%d%d%d "%(k,b[0],b[1],b[2],b[3]))
# decimal to binary
# For N = 0.634
#variable declaration
N = 0.634 # decimal no to be convered to binary
#Calculations
k = N
i = 0
b =[0,0,0,0,0,0,0,0,0,0,0,0]
while(i!=7):
N = N*2
#print N
b[i]=0
#print b[i]
if (N>1) or(N==1):
N =N-1
b[i]=1
i=i+1
#Result
#Result
print("Binary equivalent of decimal %f is 0.%d%d%d%d%d%d%d "%(k,b[0],b[1],b[2],b[3],b[4],b[5],b[6]))
# decimal to binary
# For N = 0.634
#variable declaration
N = 39.12 # decimal no to be convered to binary
N1 = 39
k = N1
#Calculations
i = 0
b =[0,0,0,0,0,0,0,0]
while(N1!=0):
b[i] = N1%2
N1 = N1/2
i = i+1
#Result
print("Binary equivalent of decimal %d is %d%d%d%d%d%d%d"%(k,b[6],b[5],b[4],b[3],b[2],b[1],b[0]))
N2 =0.12
#Calculations
k = N2
i = 0
b =[0,0,0,0,0,0,0,0,0,0,0,0]
while(i!=7):
N2 = N2*2
#print N
b[i]=0
#print b[i]
if (N2>1) or(N2==1):
N2 =N2-1
b[i]=1
i=i+1
#Result
print("Binary equivalent of decimal %f is 0.%d%d%d%d%d%d%d "%(k,b[0],b[1],b[2],b[3],b[4],b[5],b[6]))
# binary Addition
import math
#Variable declaration
a1 = int('101101',2) # integer part of first no
a2 = int('0101',2) # fractiona part of first no
b1 = int('10001',2) # integer part of second no
b2 = int('1010',2) # fractiona part of second no(in fraction we can add any no of 0s)
#Calculations
c1= bin(a1+b1)[2:]
c2 = bin(a2+b2)[2:]
print("Addition --> %s.%s"%(c1,c2))
# binary to decimal conversion
import math
#Variable declaration
i0 = 1 # LSB of integer
i1 = 0
i2 = 0
i3 = 1
i4 = 1 # MSB of integer
f1 = 0
f2 = 0
f3 = 1
f4 = 0
f5 = 1
f6 = 1
#Calculations
D = i0*(2**0)+i1*(2**1)+i2*(2**2)+i3*(2**3)+i4*(2**4)+f1*(2**-1)+f2*(2**-2)+f3*(2**-3)+f4*(2**-4)+f5*(2**-5)+f6*(2**-6)
#Result
print("Decimal equivalent = %f"%D)
# Hexadecimal to decimal
import math
#variable declaration
a = 3
b = 10 # decimal equivalent of A
c = 8
#Calculations
# binary equvalent of 8 A 3
# 1000 1010 0011
# a
Da = (1*2**0)+(1*2**1)+(0*2**2)+(0*2**3)+(0*2**4)+(1*2**5)+(0*2**6)+(1*2**7)+(0*2**8)+(0*2**9)+(0*2**10)+(1*2**11)
print("(a) Decimal equivalent of 8A3 is %d"%Da)
# b
Db = (a*(16**0))+(b*(16**1))+(c*(16**2))
print("(b) Decimal equivalent of 8A3 is %d"%Db)
# decimal to hexadecimal
import math
#Variable declaration
N = 268 # no to be converted into hexadecimal
N1 = 268
k = N1
#Calculations
i = 0
b =[0,0,0,0,0,0,0,0]
while(N1!=0):
b[i] = N1%16
N1 = N1/16
i = i+1
#Result
print("Remainder1: %d = C\nRemainder2: %d\nRemainder3: %d\n\nHexaecimal equivalent of %d = 10C"%(b[0],b[1],b[2],k))
# decimal to hexadecimal
import math
#Variable declaration
N = 5741 # no to be converted into hexadecimal
N1 = 5741
k = N1
#Calculations
i = 0
b =[0,0,0,0,0,0,0,0]
while(N1!=0):
b[i] = N1%16
N1 = N1/16
i = i+1
#Result
print("Remainder1: %d = D\nRemainder2: %d\nRemainder3: %d\nRemainder4: %d\n\nHexaecimal equivalent of %d =166D"%(b[0],b[1],b[2],b[3],k))
# Hexadecimal to decimal
import math
#variable declaration
a = 0
b = 7
c = 13 # decimal equivalent of D
#Calculations
D = (a*(16**0))+(b*(16**1))+(c*(16**2))
#Result
print("(b) Decimal equivalent of 8A3 is %d"%D)