-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2a5d707
commit a74ed1b
Showing
4 changed files
with
93 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import unittest | ||
from impresso.utils.bitmask import BitMask64 | ||
|
||
|
||
class TestBitMask64(unittest.TestCase): | ||
|
||
def test_init_with_string(self): | ||
bitmask = BitMask64("010110101") | ||
self.assertEqual( | ||
str(bitmask), | ||
"0000000000000000000000000000000000000000000000000000000010110101", | ||
) | ||
|
||
reverted_bitmask = BitMask64("010110101", reverse=True) | ||
# we expect 0b101011010 = 346 | ||
self.assertEqual(int(reverted_bitmask), 0b101011010) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
class BitMask64: | ||
def __init__(self, value: str | int | bytes = 0, reverse: bool = False): | ||
if isinstance(value, str): | ||
if not all(c in "01" for c in value): | ||
raise ValueError("String must contain only '0' and '1'") | ||
if len(value) > 64: | ||
raise ValueError("String must contain maximum 64 characters") | ||
self._value = int(value[::-1], 2) if reverse else int(value, 2) | ||
elif isinstance(value, int): | ||
self._value = value | ||
elif isinstance(value, bytes): | ||
if len(value) > 8: | ||
raise ValueError("Bytes must contain maximum 8 bytes") | ||
self._value = int.from_bytes(value, byteorder="big") | ||
# // reverse the bits | ||
self._value = int(bin(self._value)[2:].zfill(64)[::-1], 2) | ||
|
||
else: | ||
raise TypeError("Value must be a string of bits or an integer.") | ||
# Ensure the value is within the 64-bit range and pad if necessary | ||
self._value &= 0xFFFFFFFFFFFFFFFF | ||
|
||
def __int__(self): | ||
return self._value | ||
|
||
def __str__(self): | ||
return bin(self._value)[2:].zfill(64) |