Skip to content

binary_repr: extra bit for most-negative number #8670

Closed
@jsw-fnal

Description

@jsw-fnal

binary_repr does not correctly represent maximally-negative numbers of a given width. For example, an 8-bit two's complement can represent numbers in the range [-128, 127], inclusive. That is, -128 as an 8-bit two's complement number is "10000000", and 127 is "01111111". However, binary_repr mishandles -128:

>>> import numpy
>>> numpy.__version__
'1.12.0'
>>> numpy.binary_repr(-128, 8)
'110000000'

Notice, the result is a 9-bit string instead of the requested 8-bit string.

This is not limited to a width of 8:

>>> numpy.binary_repr(-8, 4)
'11000'
>>> numpy.binary_repr(-32768, 16)
'11000000000000000'
>>> numpy.binary_repr(-1, 1)
'11'

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions