Quantcast
Viewing all articles
Browse latest Browse all 74

Answer by Tom Hale for Convert int to binary string in Python

numpy.binary_repr(num, width=None)

Examples from the documentation link above:

>>> np.binary_repr(3)'11'>>> np.binary_repr(-3)'-11'>>> np.binary_repr(3, width=4)'0011'

The two’s complement is returned when the input number is negative and width is specified:

>>> np.binary_repr(-3, width=3)'101'>>> np.binary_repr(-3, width=5)'11101'

Viewing all articles
Browse latest Browse all 74

Trending Articles