NumPy Binary operations: binary_repr() function
numpy.binary_repr() function
The binary_repr() function is used to get the binary representation of the given input number as a string.
Note:
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.
A N-bit two’s-complement
system can represent every integer in the range −2N−1 to +2N−1 − 1.
Some common applications of numpy.binary_repr() include:
Debugging: Displaying the binary representation of integers while debugging code related to bit manipulation, bit-wise operations, or low-level programming tasks.
Educational Purposes: Explaining or illustrating binary operations or concepts in computer science, digital logic, or electronics, where visualizing the binary representation of numbers is helpful.
Bit Manipulation: Analyzing or manipulating the individual bits of integers by converting them into a binary string, which can then be modified and converted back to an integer.
Cryptography: Visualizing or analyzing binary representations of keys, encrypted data, or internal states of cryptographic algorithms.
Version: 1.15.0
Syntax:
numpy.binary_repr(num, width=None)
Parameter:
Name | Description | Required / Optional |
---|---|---|
num | Only an integer decimal number can be used. | Required |
width | The length of the returned string if num is positive, or the length of the two’s complement if num is negative, provided that width is at least a sufficient number of bits for num to be represented in the designated form. | Optional |
Return value:
bin [str]
Binary representation of num or two’s complement of num.
Example: Generating binary representation of integers with NumPy
>>> import numpy as np
>>> np.binary_repr(5)
'101'
>>> np.binary_repr(-5)
'-101'
>>> np.binary_repr(5, width=4)
'0101
In the first line of code, the binary representation of the positive integer 5 is generated using numpy.binary_repr(), which returns the string '101'.
In the second line of code, the binary representation of the negative integer -5 is generated using numpy.binary_repr(), which returns the string '-101'. Note that numpy.binary_repr() returns a negative binary representation by prefixing the binary string with a minus sign.
In the third line of code, the binary representation of the integer 5 is generated with a specific width of 4 using the width parameter of numpy.binary_repr(). In this case, the returned string is '0101', which is padded with leading zeros to achieve the desired width.
Example: Generating negative binary representation of integers with NumPy
>>> import numpy as np
>>> np.binary_repr(-5, width=4)
'1011'
>>> np.binary_repr(-5, width=5)
'11011'
In the first line of code, the binary representation of the negative integer -5 is generated with a width of 4 using numpy.binary_repr(), which returns the string '1011'. Note that the negative binary representation of -5 is obtained by taking the two's complement of its absolute value (which is 5), and then prefixing the binary string with a minus sign.
In the second line of code, the binary representation of the negative integer -5 is generated with a width of 5 using numpy.binary_repr(), which returns the string '11011'. Since the width is greater than the number of bits required to represent the absolute value of -5, the returned string is padded with leading zeros to achieve the desired width.
Python Code Editor:
Previous: unpackbits()
Next: NumPy String operation Home
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/numpy/binary-operations/binary-repr.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics