Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class SInteger:
- '''A signed integer class.'''
- def __init__(self, bit_str=''):
- self.val = []
- bit_len = len(bit_str)
- for i in range(LEN):
- if i < (bit_len):
- self.val.append(Bit(int(bit_str[i]))|Bit(int(bit_str[i])))
- elif (bit_len > 0):
- self.val.insert(0, Bit(int(bit_str[0]))|Bit(int(bit_str[0])))
- else:
- self.val.insert(0, ZERO)
- def __str__(self):
- strval = ''
- for i in range(LEN):
- strval = strval + str(self.val[i])
- return strval
- def __len__(self):
- return len(self.val)
- def __invert__(self):
- invrlt = SInteger('')
- for i in range(LEN):
- invrlt.val[i] = ~self.val[i]
- return invrlt
- def __int__(self):
- intrlt = 0
- tmp = SInteger('')
- if int(str(self.val[0])) == 0:
- for i in range(len(self.val)):
- intrlt += int(str(self.val[i]))
- return intrlt
- else:
- for i in range(len(self.val)):
- intrlt -= int(str(self.val[i]))
- return intrlt
- def __ge__(self, other):
- if len(self.val) >= len(other.val):
- return True
- else:
- return False
- def __or__(self, other):
- orrlt = SInteger('')
- value = ''
- if len(self.val) == len(other.val):
- for i in range(LEN):
- if int(str(self.val[i])) | int(str(other.val[i])):
- value += '5'
- else:
- value += '0'
- return value
- def __and__(self, other):
- andrlt = SInteger('')
- value = ''
- if len(self.val) == len(other.val):
- for i in range(LEN):
- if int(str(self.val[i])) & int(str(other.val[i])):
- value += '5'
- else:
- value += '0'
- return value
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement