From: Stefano Rivera Date: Sat, 17 May 2014 15:16:20 +0000 (+0200) Subject: Actually barrel-shift X-Git-Tag: 0.1~92^2~1 X-Git-Url: https://git.ctpug.org.za/?a=commitdiff_plain;h=8f1005d3b07d61d44d5a8a0302099126f7f2e6a9;p=naja.git Actually barrel-shift --- diff --git a/naja/player.py b/naja/player.py index b471c62..d4ff732 100644 --- a/naja/player.py +++ b/naja/player.py @@ -50,11 +50,12 @@ class PlayerBits(object): 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): """ diff --git a/naja/tests/test_player.py b/naja/tests/test_player.py index 2e17379..07583b3 100644 --- a/naja/tests/test_player.py +++ b/naja/tests/test_player.py @@ -85,6 +85,22 @@ class TestPlayerBits(TestCase): 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):