Question: Python 3 Complete the __str__ also in order to return the string representation of this integer in binary. You must write a SInteger class which
Python 3
Complete the __str__ also in order to return the string representation of this integer in binary.
You must write a SInteger class which is based on a list of Bits. It is a signed integer with bit length LEN. LEN is defined in test cases so you can simply use it without redefining it. While the bit string (bit_str) may have be shorter than LEN, sign-extension should be done. If the input bit string is an empty string, the new SInteger object should be a list of all ZEROs. Notice that all Bit objects stored in the list are either logic ONE or ZERO. For example, if a SInteger is instantiated as follow: bit_str is 55111140' b = SInteger (bit_str) The value of b is stored as 5555555555000050 in the processor. 'Notice that you may need to clear the carry before the operation and sign-extension is required.
class SInteger: '''A signed integer class.'''
def __init__(self, bit_str=''): self.val = [] bit_len = len(bit_str) for i in range(LEN): _______________ _______________ _______________ _______________ _______________ _______________ ''' Create an integer from the bit_str. The bit_str is either empty or a string of "0"s, "1"s, "4"s and "5"s. The number of bits of the SInteger is determined by the defined parameter LEN. SInteger should be a list of Bit objects. If the number of bits of bit_str is smaller than LEN, sign-extension should be done correctly. If bit_str is an empty string, the SInteger should be all ZERO''' def __str__(self): strval = '' for i in range(LEN): _______________ return strval ''' Return the string representation of this SInteger in binary. '''
This is the bit class:
class Bit: def __init__(self, a_val): if a_val not in [0, 1, 4, 5]: raise ValueError self.val = a_val def nor(self, b): if self: return ZERO elif b: return ZERO else: return ONE def __str__(self): return str(self.val) def __bool__(self): if self.val > 3: return True elif self.val < 2: return False def __invert__(self): return self.nor(self) def __or__(self, b): return ~(self.nor(b)) def __and__(self, b): return ((~self).nor((~b))) def full_adder(self, b): global carry bitsum = (~self & ~b & carry) | (~self & b & ~carry) | (self & ~b & ~carry) | (self & b & carry) carry = (self & b) | (self & carry) | (b & carry) return bitsum
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
