Advertisement
Guest User

Untitled

a guest
Aug 10th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. class SInteger:
  2. '''A signed integer class.'''
  3.  
  4. def __init__(self, bit_str=''):
  5. self.val = []
  6. bit_len = len(bit_str)
  7. for i in range(LEN):
  8. if i < (bit_len):
  9. self.val.append(Bit(int(bit_str[i]))|Bit(int(bit_str[i])))
  10. elif (bit_len > 0):
  11. self.val.insert(0, Bit(int(bit_str[0]))|Bit(int(bit_str[0])))
  12. else:
  13. self.val.insert(0, ZERO)
  14.  
  15. def __str__(self):
  16. strval = ''
  17. for i in range(LEN):
  18. strval = strval + str(self.val[i])
  19. return strval
  20.  
  21. def __len__(self):
  22. return len(self.val)
  23.  
  24. def __invert__(self):
  25. invrlt = SInteger('')
  26. for i in range(LEN):
  27. invrlt.val[i] = ~self.val[i]
  28. return invrlt
  29.  
  30. def __int__(self):
  31. intrlt = 0
  32. tmp = SInteger('')
  33. if int(str(self.val[0])) == 0:
  34. for i in range(len(self.val)):
  35. intrlt += int(str(self.val[i]))
  36. return intrlt
  37. else:
  38. for i in range(len(self.val)):
  39. intrlt -= int(str(self.val[i]))
  40. return intrlt
  41.  
  42. def __ge__(self, other):
  43. if len(self.val) >= len(other.val):
  44. return True
  45. else:
  46. return False
  47.  
  48.  
  49. def __or__(self, other):
  50. orrlt = SInteger('')
  51. value = ''
  52. if len(self.val) == len(other.val):
  53. for i in range(LEN):
  54. if int(str(self.val[i])) | int(str(other.val[i])):
  55. value += '5'
  56. else:
  57. value += '0'
  58. return value
  59.  
  60. def __and__(self, other):
  61. andrlt = SInteger('')
  62. value = ''
  63. if len(self.val) == len(other.val):
  64. for i in range(LEN):
  65. if int(str(self.val[i])) & int(str(other.val[i])):
  66. value += '5'
  67. else:
  68. value += '0'
  69. return value
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement