Skip to content

Commit

Permalink
support.bits: fix __getitem__ for negative keys
Browse files Browse the repository at this point in the history
Fixes #244
  • Loading branch information
blazra authored and whitequark committed Dec 10, 2020
1 parent 13177a5 commit 047ed15
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion software/glasgow/support/bits.py
Expand Up @@ -114,7 +114,7 @@ def __repr__(self):
def __getitem__(self, key):
if isinstance(key, int):
if key < 0:
return (self._int_ >> (self._len_ - key)) & 1
return (self._int_ >> (self._len_ + key)) & 1
else:
return (self._int_ >> key) & 1
if isinstance(key, slice):
Expand Down Expand Up @@ -312,6 +312,9 @@ def test_getitem_int(self):
self.assertEqual(some[0], 1)
self.assertEqual(some[2], 0)
self.assertEqual(some[5], 0)
self.assertEqual(some[-1], 1)
self.assertEqual(some[-2], 0)
self.assertEqual(some[-5], 1)

def test_getitem_slice(self):
some = bits("10001001011")
Expand Down

0 comments on commit 047ed15

Please sign in to comment.