bundles / numpy 2.5.0.dev0+git20251130.2de293a / numpy / binary_repr
function
numpy:binary_repr
source: /dev/numpy/build-install/usr/lib/python3.14/site-packages/numpy/_core/numeric.py :2049
Signature
def binary_repr ( num , width = None ) Summary
Return the binary representation of the input number as a string.
Extended Summary
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. This is the most common method of representing signed integers on computers [1]. A N-bit two's-complement system can represent every integer in the range to .
Parameters
num: intOnly an integer decimal number can be used.
width: int, optionalThe length of the returned string if
numis positive, or the length of the two's complement ifnumis negative, provided thatwidthis at least a sufficient number of bits fornumto be represented in the designated form. If thewidthvalue is insufficient, an error is raised.
Returns
bin: strBinary representation of
numor two's complement ofnum.
Notes
binary_repr is equivalent to using base_repr with base 2, but about 25x faster.
Examples
import numpy as np np.binary_repr(3) np.binary_repr(-3) np.binary_repr(3, width=4)✓
np.binary_repr(-3, width=3) np.binary_repr(-3, width=5)✓
See also
- base_repr
Return a string representation of a number in the given base system.
- bin
Python's built-in binary representation generator of an integer.
Aliases
-
numpy.binary_repr