MCPcopy Index your code
hub / github.com/numpy/numpy / binary_repr

Function binary_repr

numpy/_core/numeric.py:1998–2101  ·  view source on GitHub ↗

Return the binary representation of the input number as a string. 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 negati

(num, width=None)

Source from the content-addressed store, hash-verified

1996
1997@set_module('numpy')
1998def binary_repr(num, width=None):
1999 """
2000 Return the binary representation of the input number as a string.
2001
2002 For negative numbers, if width is not given, a minus sign is added to the
2003 front. If width is given, the two's complement of the number is
2004 returned, with respect to that width.
2005
2006 In a two's-complement system negative numbers are represented by the two's
2007 complement of the absolute value. This is the most common method of
2008 representing signed integers on computers [1]_. An N-bit two's-complement
2009 system can represent every integer in the range
2010 :math:`-2^{N-1}` to :math:`+2^{N-1}-1`.
2011
2012 Parameters
2013 ----------
2014 num : int
2015 Only an integer decimal number can be used.
2016 width : int, optional
2017 The length of the returned string if `num` is positive, or the length
2018 of the two's complement if `num` is negative, provided that `width` is
2019 at least a sufficient number of bits for `num` to be represented in
2020 the designated form. If the `width` value is insufficient, an error is
2021 raised.
2022
2023 Returns
2024 -------
2025 bin : str
2026 Binary representation of `num` or two's complement of `num`.
2027
2028 See Also
2029 --------
2030 base_repr: Return a string representation of a number in the given base
2031 system.
2032 bin: Python's built-in binary representation generator of an integer.
2033
2034 Notes
2035 -----
2036 `binary_repr` is equivalent to using `base_repr` with base 2, but about 25x
2037 faster.
2038
2039 References
2040 ----------
2041 .. [1] Wikipedia, "Two's complement",
2042 https://en.wikipedia.org/wiki/Two's_complement
2043
2044 Examples
2045 --------
2046 >>> import numpy as np
2047 >>> np.binary_repr(3)
2048 '11'
2049 >>> np.binary_repr(-3)
2050 '-11'
2051 >>> np.binary_repr(3, width=4)
2052 '0011'
2053
2054 The two's complement is returned when the input number is negative and
2055 width is specified:

Callers

nothing calls this directly

Calls 4

err_if_insufficientFunction · 0.85
zfillMethod · 0.80
indexMethod · 0.45
maxMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…