0% found this document useful (0 votes)
32 views6 pages

Col100 Assignment 2 - Computer Science Iitd Assignmets

The document contains Python code implementing various functions related to binary arithmetic, including full adders, subtraction, and multiplication of binary numbers. It defines several helper functions to perform logical operations and uses them to compute results for different binary operations. The code includes examples of usage for each function, demonstrating their functionality.

Uploaded by

rohan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views6 pages

Col100 Assignment 2 - Computer Science Iitd Assignmets

The document contains Python code implementing various functions related to binary arithmetic, including full adders, subtraction, and multiplication of binary numbers. It defines several helper functions to perform logical operations and uses them to compute results for different binary operations. The code includes examples of usage for each function, demonstrating their functionality.

Uploaded by

rohan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

12/20/21, 11:06 PM [Link]

py

#question-1

def x(a,b):
return ((a or b) and not(a and b))
def y(a,b):
return (a and b)
def fulladder(a,b,c):
return ((x(x(a,b), c)),( (x(a,b) or y(a,b)) and (y(a,b) or c)) )
print (fulladder(True,True,False))

#question-2

def x(a,b):
return ((a or b) and not(a and b))
def y(a,b):
return (a and b)

def s0(a,b,c):
return (x(x(a,b), c))
def s1(a,b,c):
return ( (x(a,b) or y(a,b)) and (y(a,b) or c))

def fulladder(a,b,c):
return (s0(a,b,c),s1(a,b,c))

print ('fulladder is:',fulladder(True,True,True))

def sum(a0,a1,a2,a3,b0,b1,b2,b3):
c0 = False
c1 = s1(a0,b0,c0)
c2 = s1(a1,b1,c1)
c3 = s1(a2,b2,c2)
c4 = s1(a3,b3,c3)

return ( s0(a0,b0,c0), s0(a1,b1,c1), s0(a2,b2,c2), s0(a3,b3,c3), c4)

print (sum(False,False,False,True,False,False,False,True))

[Link] 1/6
12/20/21, 11:06 PM [Link]

#question-3

def cmp(a0,a1,a2,a3,b0,b1,b2,b3):
return not((a3 and not(b3)) or ((a3==b3)and(a2 and not(b2))) or ((a3==b3)and(a2==b2)and(a1 and
not(b1))) or ((a3==b3)and(a2==b2)and(a1==b1)and(a0 and not(b0))))

print (cmp(False,False,True,True,True,True,True,True))

#question-4

def x(a,b):
return ((a or b) and not(a and b))

[Link] 2/6
12/20/21, 11:06 PM [Link]

def y(a,b):
return (a and b)

def s0(a,b,c):
return (x(x(a,b), c))
def s1(a,b,c):
return ( (x(a,b) or y(a,b)) and (y(a,b) or c))

def sum(a0,a1,a2,a3,b0,b1,b2,b3):
c0 = False
c1 = s1(a0,b0,c0)
c2 = s1(a1,b1,c1)
c3 = s1(a2,b2,c2)
c4 = s1(a3,b3,c3)

return ( s0(a0,b0,c0), s0(a1,b1,c1), s0(a2,b2,c2), s0(a3,b3,c3), c4)

#print (sum(False,True,False,False,True,False,False,False))

def twoscmp(a0,a1,a2,a3):
return sum((not a0), (not a1),( not a2), (not a3),True,False,False,False)

def cmp(a0,a1,a2,a3,b0,b1,b2,b3):
return not((a3 and not(b3)) or ((a3==b3)and(a2 and not(b2))) or ((a3==b3)and(a2==b2)and(a1 and
not(b1))) or ((a3==b3)and(a2==b2)and(a1==b1)and(a0 and not(b0))))

def sub4(a0,a1,a2,a3,b0,b1,b2,b3):

(s0,s1,s2,s3,s4)=twoscmp(b0,b1,b2,b3)
(p0,p1,p2,p3,p4)=sum(a0,a1,a2,a3,s0,s1,s2,s3)
(q0,q1,q2,q3,q4)=twoscmp(p0,p1,p2,p3)
if p4 == True:
return p0,p1,p2,p3,cmp(a0,a1,a2,a3,b0,b1,b2,b3)

else:
return q0,q1,q2,q3,cmp(a0,a1,a2,a3,b0,b1,b2,b3)

#print (twoscmp(True,False,False,True))
print (sub4(True,False,False,True,True,True,True,False))

#question-5

[Link] 3/6
12/20/21, 11:06 PM [Link]

def x(a,b):
return ((a or b) and not(a and b))
def y(a,b):
return (a and b)

def s0(a,b,c):
return (x(x(a,b), c))
def s1(a,b,c):
return ( (x(a,b) or y(a,b)) and (y(a,b) or c))

def sum(a0,a1,a2,a3,b0,b1,b2,b3,c0):
c1 = s1(a0,b0,c0)
c2 = s1(a1,b1,c1)
c3 = s1(a2,b2,c2)
c4 = s1(a3,b3,c3)

return s0(a0,b0,c0), s0(a1,b1,c1), s0(a2,b2,c2), s0(a3,b3,c3), c4

def add8(a0,a1,a2,a3,a4,a5,a6,a7,b0,b1,b2,b3,b4,b5,b6,b7):
(p0,p1,p2,p3,p4)=sum(a0,a1,a2,a3,b0,b1,b2,b3,False)
(q0,q1,q2,q3,q4)=sum(a4,a5,a6,a7,b4,b5,b6,b7,p4)
return ((p0,p1,p2,p3,q0,q1,q2,q3),q4)

print
(add8(True,True,True,True,True,True,False,False,True,False,False,False,False,False,False,False))

#question-6

from typing import AnyStr

def x(a,b):
return ((a or b) and not(a and b))
def y(a,b):
return (a and b)

[Link] 4/6
12/20/21, 11:06 PM [Link]

def s0(a,b,c):
return (x(x(a,b), c))
def s1(a,b,c):
return ( (x(a,b) or y(a,b)) and (y(a,b) or c))

def sum(a0,a1,a2,a3,b0,b1,b2,b3,c0):
c1 = s1(a0,b0,c0)
c2 = s1(a1,b1,c1)
c3 = s1(a2,b2,c2)
c4 = s1(a3,b3,c3)

return ( s0(a0,b0,c0), s0(a1,b1,c1), s0(a2,b2,c2), s0(a3,b3,c3), c4)

def twoscmp(a0,a1,a2,a3):
return sum((not a0), (not a1),( not a2), (not a3),True,False,False,False,False)

def cmp(a0,a1,a2,a3,b0,b1,b2,b3):
return not((a3 and not(b3)) or ((a3==b3)and(a2 and not(b2))) or ((a3==b3)and(a2==b2)and(a1 and
not(b1))) or ((a3==b3)and(a2==b2)and(a1==b1)and(a0 and not(b0))))

def sub(a,b):
(a0,a1,a2,a3)= a
(b0,b1,b2,b3)= b

(s0,s1,s2,s3,s4)=twoscmp(b0,b1,b2,b3)
(p0,p1,p2,p3,p4)=sum(a0,a1,a2,a3,s0,s1,s2,s3,False)
(q0,q1,q2,q3,q4)=twoscmp(p0,p1,p2,p3)
if p4 == True:
return p0,p1,p2,p3

else:
return q0,q1,q2,q3

#print (twoscmp(True,False,False,True))
#print (sub(True,False,False,True,True,True,True,False))

def add8(a,b):
a0,a1,a2,a3,a4,a5,a6,a7 = a
b0,b1,b2,b3,b4,b5,b6,b7 = b
(p0,p1,p2,p3,p4)=sum(a0,a1,a2,a3,b0,b1,b2,b3,False)
(q0,q1,q2,q3,q4)=sum(a4,a5,a6,a7,b4,b5,b6,b7,p4)
return p0,p1,p2,p3,q0,q1,q2,q3

#print
(add8(True,True,True,True,True,True,False,False,True,False,False,False,False,False,False,False))

def mul4(a,b):

(a0,a1,a2,a3)= a
(b0,b1,b2,b3)= b

c = a+(False,False,False,False)
d = b+(False,False,False,False)

if b==(False,False,False,False):
return d

else:

[Link] 5/6
12/20/21, 11:06 PM [Link]

e = sub(b,(True,False,False,False))
return add8(c,mul4(a,e))

a = True,True,False,False
b = False,False,True,False

print(mul4(a,b))

[Link] 6/6

You might also like