Skip to content

Commit

Permalink
Add new utils to type_converter to encode arbitrary sized ints.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 733934165
  • Loading branch information
code-perspective authored and copybara-github committed Mar 6, 2025
1 parent 43e7ae0 commit 67cb802
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions jaxite/jaxite_bool/type_converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,35 @@
from typing import List, Optional


def bit_slice_to_u8(bit_slice: List[bool]) -> int:
"""Given a bit slice of length 8, returns a base-10 int representation."""
if len(bit_slice) != 8:
def bit_slice_to_uint(bit_slice: List[bool], num_bits: int) -> int:
"""Given a bit slice of num_bits, returns a base-10 int representation."""
if len(bit_slice) != num_bits:
raise ValueError(f'Expected an 8-bit representation but got: {bit_slice}.')
result = 0
for i in range(8):
for i in range(num_bits):
result |= (int(bit_slice[i])) << i
return result

def uint_to_bit_slice(input_int: int, num_bits: int) -> List[bool]:
"""Given an integer [0, 255], returns a bitwise representation."""
result: List[bool] = [False] * num_bits
for i in range(num_bits):
result[i] = ((input_int >> i) & 1) != 0
return result


def bit_slice_to_u8(bit_slice: List[bool]) -> int:
"""Given a bit slice of length 8, returns a base-10 int representation."""
if len(bit_slice) != 8:
raise ValueError(f'Expected an 8-bit representation but got: {bit_slice}.')
return bit_slice_to_uint(bit_slice, 8)


def u8_to_bit_slice(input_int: int) -> List[bool]:
"""Given an integer [0, 255], returns a bitwise representation."""
if input_int < 0 or input_int > 255:
raise ValueError(f'Expected a u8, but got: {input_int}.')
result: List[bool] = [False] * 8
for i in range(8):
result[i] = ((input_int >> i) & 1) != 0
return result
return uint_to_bit_slice(input_int, 8)


def u8_list_to_bit_slice(input_list: List[int]) -> List[bool]:
Expand Down

0 comments on commit 67cb802

Please sign in to comment.