# numpy.binary_repr() – The NumPy binary_repr Python Function

## Introduction to NumPy binary_repr

The numpy.binary_repr() function is use to express the input number’s binary form as a string.

If no width is specified for negative integers, a minus sign is insert at the front. If width is specified, the number’s two’s complement is returned with respect to that width. Negative numbers are represent as the two’s complement of the absolute value in a two’s complement system. On computers, this is the most common method of representing signed integers.

Syntax:

` numpy.binary_repr(number, width=None)`

Parameters:

• number: Input number. Only an integer decimal number can be use as input.
• width: [int, optional]
• The length of the returned string if a number is positive, or the length of the two’s complement
• if a number is negative, provide that width is at least a sufficient number of bits for a number to be represent in the designated form.
• If the width value is insufficient, it will be ignored, and the number will be returned in binary (number > 0) or two’s complement (number < 0) form with its width equal to the minimum number of bits needed to represent the number in the designated form.

Return: binary string representation of the input number.

## Code Examples of NumPy binary_repr

Example 01: When the input is a number

``````# welcome to softhunt.net
# Python program explaining
# binary_repr() function

import numpy as np
num = 15

print ("Input number : ", num)

ans = np.binary_repr(num)
print ("binary representation of 15 : ", ans)``````

Output:

```Input number :  15
binary representation of 15 :  1111```

Example 02: When the input is an array

``````# welcome to softhunt.net
# Python program explaining
# binary_repr() function
import numpy as np

arr = [12, -33 ]

print ("Input array : ", arr)

# binary representation of first array
# element without using width parameter
ans = np.binary_repr(arr[0])
print ("Binary representation of 12 Without using width parameter : ", ans)

# binary representation of first array
# element using width parameter
ans = np.binary_repr(arr[0], width = 7)
print ("Using width parameter: ", ans)

# binary representation of 2nd array
# element without using width parameter
ans = np.binary_repr(arr[1])
print ("Binary representation of -33 Without using width parameter : ", ans)

# binary representation of 2nd array
# element using width parameter
ans = np.binary_repr(arr[1], width = 7)
print ("Using width parameter : ", ans)``````

Output:

```Input array :  [12, -33]
Binary representation of 12 Without using width parameter :  1100
Using width parameter:  0001100
Binary representation of -33 Without using width parameter :  -100001
Using width parameter :  1011111```

## FAQs

### Does NumPy have a dictionary?

There are two necessary entries in the dictionary: ‘names’ and ‘formats,’ as well as four optional keys: ‘offsets,’ item size,’ aligned,’ and ‘titles.’ The values for ‘names’ and ‘formats’ should be a list of field names and a list of dtype specifications, both of which should be of the same length. The ‘offsets’ value, which is optional, should be a list of integer byte-offsets, one for each field in the structure. If the keyword ‘offsets’ is not used, the offsets are calculate automatically. The optional ‘item size’ value should be an integer that describes the dtype’s overall size in bytes, which must be large enough to hold all fields.

## Conclusion

That’s all for this article. If you have any confusion contact us through our website or email us at [email protected] or by using LinkedIn. And make sure you check out our NumPy tutorials.

Suggested Articles:

1. The NumPy unpackbits Python Function