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)
| 1935 | |
| 1936 | @set_module('numpy') |
| 1937 | def binary_repr(num, width=None): |
| 1938 | """ |
| 1939 | Return the binary representation of the input number as a string. |
| 1940 | |
| 1941 | For negative numbers, if width is not given, a minus sign is added to the |
| 1942 | front. If width is given, the two's complement of the number is |
| 1943 | returned, with respect to that width. |
| 1944 | |
| 1945 | In a two's-complement system negative numbers are represented by the two's |
| 1946 | complement of the absolute value. This is the most common method of |
| 1947 | representing signed integers on computers [1]_. A N-bit two's-complement |
| 1948 | system can represent every integer in the range |
| 1949 | :math:`-2^{N-1}` to :math:`+2^{N-1}-1`. |
| 1950 | |
| 1951 | Parameters |
| 1952 | ---------- |
| 1953 | num : int |
| 1954 | Only an integer decimal number can be used. |
| 1955 | width : int, optional |
| 1956 | The length of the returned string if `num` is positive, or the length |
| 1957 | of the two's complement if `num` is negative, provided that `width` is |
| 1958 | at least a sufficient number of bits for `num` to be represented in the |
| 1959 | designated form. |
| 1960 | |
| 1961 | If the `width` value is insufficient, it will be ignored, and `num` will |
| 1962 | be returned in binary (`num` > 0) or two's complement (`num` < 0) form |
| 1963 | with its width equal to the minimum number of bits needed to represent |
| 1964 | the number in the designated form. This behavior is deprecated and will |
| 1965 | later raise an error. |
| 1966 | |
| 1967 | .. deprecated:: 1.12.0 |
| 1968 | |
| 1969 | Returns |
| 1970 | ------- |
| 1971 | bin : str |
| 1972 | Binary representation of `num` or two's complement of `num`. |
| 1973 | |
| 1974 | See Also |
| 1975 | -------- |
| 1976 | base_repr: Return a string representation of a number in the given base |
| 1977 | system. |
| 1978 | bin: Python's built-in binary representation generator of an integer. |
| 1979 | |
| 1980 | Notes |
| 1981 | ----- |
| 1982 | `binary_repr` is equivalent to using `base_repr` with base 2, but about 25x |
| 1983 | faster. |
| 1984 | |
| 1985 | References |
| 1986 | ---------- |
| 1987 | .. [1] Wikipedia, "Two's complement", |
| 1988 | https://en.wikipedia.org/wiki/Two's_complement |
| 1989 | |
| 1990 | Examples |
| 1991 | -------- |
| 1992 | >>> np.binary_repr(3) |
| 1993 | '11' |
| 1994 | >>> np.binary_repr(-3) |
nothing calls this directly
no test coverage detected