[issue28553] int.to_bytes docs logic error
New submission from Ofek Lev:
https://docs.python.org/3/library/stdtypes.html#int.to_bytes
To convert an int to the exact number of bytes required, the docs recommend "x.to_bytes((x.bit_length() // 8) + 1, ...)". This is incorrect when length is a multiple of 8, e.g. 296.
The correct way is "x.to_bytes((x.bit_length() + 7) // 8, ...)".
----------
assignee: docs@python
components: Documentation
messages: 279641
nosy: Ofekmeister, docs@python
priority: normal
severity: normal
status: open
title: int.to_bytes docs logic error
type: enhancement
versions: Python 3.3, Python 3.4, Python 3.5, Python 3.6, Python 3.7
_______________________________________
Python tracker
participants (5)
-
INADA Naoki
-
Mark Dickinson
-
Ofek Lev
-
Roundup Robot
-
Serhiy Storchaka