Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Python 3 For the following code, complete the __or__ and __and__ methods with on

ID: 2246915 • Letter: P

Question

 Python 3  For the following code, complete the __or__ and __and__ methods with one line of code using self.nor method and not operator.  Complete the full_adder method in the Bit class so that it returns a Bit which is the sum of adding 2 Bits and the carry Bit together.  Extra Condition: Your answer should be the Bit class as given with one changed line of code,  using the logical operators.  e.g. something like: sum = a | b & carry | a & b  The full_adder method then produces a one bit answer with the side effect of setting the carry bit (I have done that part for you).  The input is 0 or 1, it is classified as logic ZERO (displayed as 0). If the input is 4 or 5, it is classified as logic ONE (displayed as 5).  For Bit object with ZERO, it can be 0 or 1.  For Bit object with ONE it can be 4 or 5.  You do not need to redefine ZERO and ONE.  # The Bit class. class Bit:     '''A single bit class.     All of the operations: "invert/not", "or", "and" and "full_adder" are built directly or indirectly on "nor".     '''      def __init__(self, a_val):         '''Instantiate the Bit object with any valid inputs'''         if a_val not in [0, 1, 4, 5]:             raise ValueError         self.val = a_val      def nor(self, b):         ''' Return a NOR b.         Yes I could have written this in one line but this version doesn't use any binary logical operators.         '''         if self:             return ZERO         elif b:             return ZERO         else:             return ONE      def __str__(self):         return str(self.val)      def __bool__(self):         ''' True if it is 4 or 5; False if it is 0 or 1. '''         if self.val > 3:             return True         elif self.val < 2:             return False      def __invert__(self):         ''' Return the NOT of self. Built on NOR.         '''         return self.nor(self)       def __or__(self, b):         ''' Return self OR b.         Built on NOR and NOT. '''         return _______________       def __and__(self, b):         ''' Return self AND b.         Built on NOT and OR using DeMorgan's law. '''         return _______________       def full_adder(self, b):         ''' Return self + b + carry as a single bit with carry set as well. Built on AND, OR and NOT.         See Figure 3.15 in the textbook.         '''         global carry  # it is processor global status         bitsum = _______________         carry = (self & b) | (self & carry) | (b & carry)         return _______________ 

Explanation / Answer


class Bit:
'''A single bit class.
All of the operations: "invert/not", "or", "and" and "full_adder" are built directly or indirectly on "nor".
'''

def __init__(self, a_val):
'''Instantiate the Bit object with any valid inputs'''
if a_val not in [0, 1, 4, 5]:
raise ValueError
self.val = a_val

def nor(self, b):
''' Return a NOR b.
Yes I could have written this in one line but this version doesn't use any binary logical operators.
'''
if self:
return ZERO
elif b:
return ZERO
else:
return ONE

def __str__(self):
return str(self.val)

def __bool__(self):
''' True if it is 4 or 5; False if it is 0 or 1. '''
if self.val > 3:
return True
elif self.val < 2:
return False

def __invert__(self):
''' Return the NOT of self. Built on NOR.
'''
return self.nor(self)


def __or__(self, b):
''' Return self OR b.
Built on NOR and NOT. '''
return ~(self.nor(b))


def __and__(self, b):
''' Return self AND b.
Built on NOT and OR using DeMorgan's law. '''
return ~((~self) || (~b))

def full_adder(self, b):
''' Return self + b + carry as a single bit with carry set as well. Built on AND, OR and NOT.
See Figure 3.15 in the textbook.
'''
global carry # it is processor global status
bitsum = (~self & ~b & carry) | (~self & b & ~carry) | (self & ~b & ~carry) | (self & b & carry)
carry = (self & b) | (self & carry) | (b & carry)
return (bitsum, carry)