Micropython: implement bit_length() for mp_type_int

Created on 19 Aug 2018  路  3Comments  路  Source: micropython/micropython

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'

Most helpful comment

Attached a patch file that implement int.bit_length()

int_length.zip

All 3 comments

Attached a patch file that implement int.bit_length()

int_length.zip

@dmazzella

If you went to the trouble to implement it, why not submit a pull-request?

+1 for getting this added in.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

miketeachman picture miketeachman  路  5Comments

rolandvs picture rolandvs  路  3Comments

ruky87 picture ruky87  路  5Comments

joelhoro picture joelhoro  路  5Comments

ds2k5 picture ds2k5  路  3Comments