You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

44 lines
907 B

class Byte:
def __init__(self, value: int):
self.value = value
def set_bit(self, bitnr: int, val: bool):
chain1 = (1 << bitnr)
chain2 = ~chain1
if val == 1:
self.value |= chain1
else:
self.value &= chain2
def is_set(self, bitnr: int):
return (self.value >> bitnr) & 1
def toggle_bit(self, bitnr: int):
self.value = (self.value ^ (1 << bitnr))
def swap_bit(self,bitnr1: int, bitnr2: int):
b1 = (self.value >> bitnr1) & 0b1
b2 = (self.value >> bitnr2) & 0b1
self.value &= ~(1 << bitnr1)
self.value |= b2 << bitnr1
self.value &= ~(1 << bitnr2)
self.value |= b1 << bitnr2
def __str__(self):
return bin(self.value)[2:]
def __int__(self):
return self.value
def __and__(self, other):
return Byte(int(self) & int(other))
def __or__(self, other):
return Byte(int(self) | int(other))
def __xor__(self, other):
return Byte(int(self) ^ int(other))