Code: Alles auswählen
class Foo(object):
def status_from_byte1(self, status):
self.flag_C = [0, 1][0 != status & 1]
self.flag_V = [0, 1][0 != status & 2]
self.flag_Z = [0, 1][0 != status & 4]
self.flag_N = [0, 1][0 != status & 8]
self.flag_I = [0, 1][0 != status & 16]
self.flag_H = [0, 1][0 != status & 32]
self.flag_F = [0, 1][0 != status & 64]
self.flag_E = [0, 1][0 != status & 128]
def status_from_byte2(self, status):
self.flag_C = 0 if status & 1 == 0 else 1
self.flag_V = 0 if status & 2 == 0 else 1
self.flag_Z = 0 if status & 4 == 0 else 1
self.flag_N = 0 if status & 8 == 0 else 1
self.flag_I = 0 if status & 16 == 0 else 1
self.flag_H = 0 if status & 32 == 0 else 1
self.flag_F = 0 if status & 64 == 0 else 1
self.flag_E = 0 if status & 128 == 0 else 1
def status_from_byte3(self, status):
data = tuple([int(x) for x in '{0:08b}'.format(status)])
self.flag_E, \
self.flag_F, \
self.flag_H, \
self.flag_I, \
self.flag_N, \
self.flag_Z, \
self.flag_V, \
self.flag_C = data
def status_from_byte4(self, status):
data = [0 if status & x == 0 else 1 for x in (128, 64, 32, 16, 8, 4, 2, 1)]
self.flag_E, \
self.flag_F, \
self.flag_H, \
self.flag_I, \
self.flag_N, \
self.flag_Z, \
self.flag_V, \
self.flag_C = data
def status_as_byte(self):
return self.flag_C | \
self.flag_V << 1 | \
self.flag_Z << 2 | \
self.flag_N << 3 | \
self.flag_I << 4 | \
self.flag_H << 5 | \
self.flag_F << 6 | \
self.flag_E << 7
f = Foo()
for i in xrange(255):
f.status_from_byte4(i)
i2 = f.status_as_byte()
assert i == i2
Welches ist die beste? Noch weitere Vorschläge?
Ich denke status_from_byte2() oder status_from_byte4() sind meine Favoriten.
Was könnte man bei status_as_byte() machen?
(Natürlich ist & nur ein &)