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.
 

41 lines
1.0 KiB

class Byte:
def __init__(self, value):
self.value = value
def set_bit(self, bitnr, val):
if val == 1:
return bin((1 << bitnr -1) | self.value)
elif val == 0:
return bin(~(1 << bitnr -1) & self.value)
def is_set (self, bitnr=None):
if bit_nr:
if self.value & (1 << bitnr -1):
return True
else:
return False
else:
return "keine bitnr angegeben"
def toggle_bit(self, bitnr):
if bitnr:
return bin((1 << bitnr -1) ^ self.value)
else:
return "keine bitnr angegeben"
def swap_bit(self, bitnr1, bitnr2):
return bin((self.value ^ (1 << bitnr1)) ^ (1 << bitnr2))
num = Byte(8)
print ("set_bit True:", num.set_bit(4, 1))
print ("set_bit False:", num.set_bit(4, 0))
print ("is_set:", num.is_set(3))
print ("is_set overload:", num_.is_set())
print ("toggle_bit:", num.toggle_bit(3))
print ("toggle_bit overload:", num.toggle_bit())
print ("swap_bit:", num.swap_bit(3, 0))