-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOR_AND_XOR_operation.py
67 lines (61 loc) · 1.88 KB
/
OR_AND_XOR_operation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
def calclen(num1,num2):
a = len(num1)
b = len(num2)
if a!=b :
if a>b:
for _ in range(a-b):
num2='0'+num2
else:
for _ in range(b-a):
num1='0'+num1
return num1,num2
def calcor(num1,num2):
numor=""
for i in range(len(num1)):
if num1[i]=='1' or num2[i]=='1':
numor+='1'
else:
numor+='0'
return numor
def calcand(num1,num2):
numand=""
for i in range(len(num1)):
if num1[i]=='1' and num2[i]=='1':
numand+='1'
else:
numand+='0'
return numand
def calcxor(num1,num2):
numxor=""
for i in range(len(num1)):
if ((num1[i]=='0' and num2[i]=='0') or (num1[i]=='1'and num2[i]=='1')):
numxor+='0'
elif ((num1[i]=='0' and num2[i]=='1') or (num1[i]=='1' and num2[i]=='0')):
numxor+='1'
return numxor
def fromdectobin(num,baseA,baseB):
strr=""
while num>=1:
n = num%baseB
if n>= 0 and n<=9:
n = chr(n + ord('0'))
else:
n = chr(n - 10 + ord('A'))
strr = strr + str(n)
num = num//baseB
strr = ''.join(reversed(strr))
return strr
num1 = int(input("Enter first integer : "))
num2 = int(input("Enter second integer : "))
bnum1 = fromdectobin(num1,10,2)
bnum2 = fromdectobin(num2,10,2)
bnum1,bnum2 = calclen(bnum1,bnum2)
print("The binary of",num1,"is",bnum1)
print("The binary of",num2,"is",bnum2)
print("OR operation on",num1,"and",num2,"gives",calcor(bnum1,bnum2))
print("AND operation on",num1,"and",num2,"gives",calcand(bnum1,bnum2))
print("XOR operation on",num1,"and",num2,"gives",calcxor(bnum1,bnum2))
#SAME RESULT WITH LOGICAL OPERATORS
#print("OR of 1st Integer by 2nd Integer : ",num1|num2)
#print("AND of 1st integer by 2nd Integer : ",num1&num2)
#rint("XOR of 1st integer by 2nd Integer : ",num1^num2)