Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def _xor(a, b):
- return (not a and b) or (a and not b)
- def _and(a, b):
- return a and b
- def _or(a, b):
- return a or b
- def calculateSum(bita, bitb, cin):
- return _xor(_xor(cin, bita), bitb)
- def calculateCarry(bita, bitb, cin):
- return _or(_and(bita, bitb), _and(cin, _xor(bita, bitb)))
- class BinaryNumber:
- def __init__(self, string = None):
- self.string = string or ""
- def at(self, index):
- return self.string[index] == '1'
- def addtobegining(self, booleanvalue):
- digit = '1' if booleanvalue else '0'
- self.string = digit + self.string
- def prettyprint(self):
- print(self.string)
- while True:
- carrybit = False
- binaryno1 = BinaryNumber(input("Enter binary number 1: "))
- binaryno2 = BinaryNumber(input("Enter binary number 2: "))
- binarysize = 8
- output = BinaryNumber()
- for i in range(binarysize - 1, -1, -1):
- x = binaryno1.at(i)
- y = binaryno2.at(i)
- sumoutput = calculateSum(x, y, carrybit)
- carrybit = calculateCarry(x, y, carrybit)
- output.addtobegining(sumoutput)
- output.prettyprint()
Add Comment
Please, Sign In to add comment