self.toggle_bit(bit)
def shift_bits_left(self, shift):
- self.bits <<= shift
+ wrap = self.bits >> (8 - shift)
+ self.bits = (self.bits << shift & 0xff | wrap)
def shift_bits_right(self, shift):
- self.bits >>= shift
-
+ wrap = self.bits << (8 - shift) & 0xff
+ self.bits = (self.bits >> shift | wrap)
class Player(object):
"""
bits.toggle_bits([BITS.NORTH, BITS.BLUE, BITS.MSB])
self.assertEqual(bits._bits, 0x92)
+ def test_shift_bits_left(self):
+ bits = PlayerBits(0x03)
+ self.assertEqual(bits._bits, 0x03)
+ bits.shift_bits_left(1)
+ self.assertEqual(bits._bits, 0x06)
+ bits.shift_bits_left(6)
+ self.assertEqual(bits._bits, 0x81)
+
+ def test_shift_bits_right(self):
+ bits = PlayerBits(0x06)
+ self.assertEqual(bits._bits, 0x06)
+ bits.shift_bits_right(1)
+ self.assertEqual(bits._bits, 0x03)
+ bits.shift_bits_right(1)
+ self.assertEqual(bits._bits, 0x81)
+
class TestPlayer(TestCase):
def test_new_player(self):