Skip to content

Commit

Permalink
Add "where" string for fields
Browse files Browse the repository at this point in the history
  • Loading branch information
vkottler committed Sep 18, 2023
1 parent 9552cb3 commit 2f97973
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
16 changes: 16 additions & 0 deletions runtimepy/primitives/field/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,22 @@ def __init__(
self.mask = (2**self.width) - 1
self.shifted_mask = self.mask << self.index

def where_str(self) -> str:
"""
Get a simple string representing the bit locations this field occupies.
"""

result = ""

for idx in range(self.raw.size * 8):
val = 1 << idx
if val & self.shifted_mask:
result += "^"
else:
result += "-"

return result

def __call__(self, val: int = None) -> int:
"""
Set (or get) the underlying value of this field. Return the actual
Expand Down
3 changes: 3 additions & 0 deletions tests/primitives/field/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ def test_bit_fields_basic():
byte = BitFields.new()

flag1 = byte.flag("flag1")
assert flag1.where_str() == "^-------"

flag2 = byte.flag("flag2")
assert flag2.where_str() == "-^------"

flag1.set()
assert byte.raw.value == 1
Expand Down

0 comments on commit 2f97973

Please sign in to comment.