← index #4065PR #14387
Related · high · value 0.272
QUERY · ISSUE

implement bit_length() for mp_type_int

openby adritiumopened 2018-08-19updated 2024-08-28
py-core

int.to_bytes(size, byteorder) requires one to know how many bytes - size - the output is but int.bit_length() is not implemented.

>>> (1024).to_bytes(2, byteorder='big')
b'\x04\x00'
>>> (1024).to_bytes(10, byteorder='big')
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> (-1024).to_bytes(10, byteorder='big', signed=True)
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes((x.bit_length() + 7) // 8, byteorder='little')
b'\xe8\x03'
CANDIDATE · PULL REQUEST

py/objint: Make byteorder argument optional in byte-conversion methods.

mergedby AmirHmZzopened 2024-04-28updated 2024-09-02
py-core

This PR intends to make byteorder argument optional in int.to_bytes() and int.from_bytes() methods. According to CPython documentation, byteorder argument is optional and set to "big" by default. This PR makes Micropython compatible with CPython in this case.

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied