Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

NumPy Binary operations: binary_repr() function

numpy.binary_repr() function

The binary_repr() function is used to get the binary representation of the given input number as a string.

Note: For negative numbers, if width is not given, a minus sign is added to the front. If width is given, the two’s complement of the number is returned, with respect to that width.
In a two’s-complement system negative numbers are represented by the two’s complement of the absolute value.
A N-bit two’s-complement system can represent every integer in the range −2N−1 to +2N−1 − 1.

Version: 1.15.0

Syntax:

numpy.binary_repr(num, width=None)

Parameter:

Name Description Required /
Optional
num Only an integer decimal number can be used. Required
width The length of the returned string if num is positive, or the length of the two’s complement if num is negative, provided that width is at least a sufficient number of bits for num to be represented in the designated form. Optional

Return value:
bin [str]
Binary representation of num or two’s complement of num.

Example-1: numpy.binary_repr() function

>>> import numpy as np
>>> np.binary_repr(5)
'101'
>>> np.binary_repr(-5)
'-101'
>>> np.binary_repr(5, width=4)
'0101

Example-2: numpy.binary_repr() function

>>> import numpy as np
>>> np.binary_repr(-5, width=4)
'1011'
>>> np.binary_repr(-5, width=5)
'11011'

Python Code Editor:

Previous: unpackbits()
Next: NumPy String operation Home