Skip to content

Commit 9485b4a

Browse files
committed
add comment explaining the width calculation
1 parent ae209af commit 9485b4a

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

hazelcast/util.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,13 @@ def int_from_bytes(buf):
378378
if hasattr(int, "to_bytes"):
379379

380380
def int_to_bytes(number):
381-
# number of bytes to represent the number
381+
# number of bytes to represent the number.
382+
# For numbers that has not exactly 8n bit_length,
383+
# adding 8 and performing integer division with 8
384+
# let us get the correct width because
385+
# (8n + m + 8) // 8 = n + 0 + 1 (assuming m < 8).
386+
# For negative numbers, we add 1 to get rid of the
387+
# effects of the leading 1 (the sign bit).
382388
width = (8 + (number + (number < 0)).bit_length()) // 8
383389
return number.to_bytes(length=width, byteorder="big", signed=True)
384390

0 commit comments

Comments
 (0)