Skip to content

Commit 18c2abf

Browse files
Add _setuintle method to Bits class
1 parent b876672 commit 18c2abf

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

bitstring/bits.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,25 @@ def _setuint(self, uint: int, length: Optional[int]=None) -> None:
446446
"""Reset the bitstring to have given unsigned int interpretation."""
447447
pass
448448

449+
def _setuintle(self, uint: int, length: Optional[int]=None) -> None:
450+
"""Reset the bitstring to have given unsigned int interpretation in little-endian."""
451+
if length is None:
452+
# Calculate the minimum number of bits needed
453+
length = max(uint.bit_length(), 1)
454+
if length % 8:
455+
length += 8 - (length % 8)
456+
if length % 8:
457+
raise ValueError("Little-endian integers must be whole-byte. Length = {0} bits.".format(length))
458+
if uint < 0:
459+
raise ValueError("Little-endian unsigned integer cannot be negative.")
460+
if uint >= (1 << length):
461+
raise ValueError("Little-endian unsigned integer is too large for length {0}.".format(length))
462+
# Convert to bytes in little-endian order
463+
num_bytes = length // 8
464+
byte_data = uint.to_bytes(num_bytes, byteorder='little', signed=False)
465+
self._setbytes(byte_data)
466+
pass
467+
449468
def _getuint(self) -> int:
450469
"""Return data as an unsigned int."""
451470
pass

0 commit comments

Comments
 (0)