in Education by
I have a numpy array (dtype bool) representing an array of bits. For example, the array np.array([True, False, False], dtype=bool) represents the number 4 (indeed, bin(4) == 0b100). I would like to convert the numpy array to an integer (4 in the previous example). So far I've tried with an iterative approach: bits = np.array([True, False, False], dtype=bool) n = 0 for bit in bits: n = (n << 1) | bit This approach does work, but I would prefer something that does not iterate over every element of the array, possibly a numpy built-in method. I also tried using numpy.packbits (together with numpy.pad, because packbits always automatically pad to the right, and not to the left): bits = np.array([True, False, False], dtype=bool) n = np.packbits(np.pad(bits, ((8 - len(bits) % 8) % 8, 0))).item() This approach only works for arrays with 8 or less elements. Indeed, if you try to use a longer array you end up having multiple results (because apparently packbits not only pads to the right but also converts every single byte to a number): bits = np.array( [True, False, False, False, False, False, False, False, False], dtype=bool, ) n = np.packbits(np.pad(bits, ((8 - len(bits) % 8) % 8, 0))) print(n) # this prints [1 0], but I need it to return 256 Expected behavior: np.array([True, True], dtype=bool) --> 3 np.array([True, True, False], dtype=bool) --> 6 np.array([True, False, False, True, True], dtype=bool) --> 19 np.array([True, False, False, False, False, False, False, True, False, False], dtype=bool) --> 516 JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
You can solve this problem by generating the power of two starting from the biggest one (eg. [16, 8, 4, 2, 1]), and then multiply this by bits before doing the final sum: powers = 1 << np.arange(bits.size, dtype=np.uint64)[::-1] result = np.sum(powers * bits) This is equivalent of doing: 2**n * bits[0] + 2**(n-1) * bits[1] + ... + 2**0 * bits[n]. Note that the final value needs to fit in 64 bits.

Related questions

0 votes
    import numpy as np from pint import UnitRegistry unit = UnitRegistry() Q_ = unit.Quantity a = 1.0 ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 17, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: Concatenate two NumPy arrays vertically (4 answers) Closed 3 years ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: Concatenate two NumPy arrays vertically (4 answers) Closed 3 years ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: Concatenate two NumPy arrays vertically (4 answers) Closed 3 years ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I want to encode a 1-D numpy array: x = array([1,0,3]) As a 2-D 1-hot array y = array([ ... some faster technique other than looping. Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    I'm creating a very basic AI with Tensorflow, and am using the code from the official docs/tutorial. Here's my ... Tensorflow 1.13.1. Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    I have a dataset that only contains y-values in one column. I want to insert a column of x- ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    I need to iterate through the elements in a numpy array so I can treat any zero elements separately. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    I need to iterate through the elements in a numpy array so I can treat any zero elements separately. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    I have the 4D array, which is defined as follows: B = np.array( [[[[0.5000, 0.5625], [0.5000, ... loops and ideally one function call. Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    How do you think I can get the intersection of the below-given array? a=np.array([[[[0,0],[0,1]],[[1,1],[1,1]]] ... , [[0, 1], [1, 1]]] Select the correct answer from above options...
asked Jan 9, 2022 in Education by JackTerrance
0 votes
    Write a Numpy program to create an array to 10 zeros,10 ones,10 fives. Select the correct answer from above options...
asked Dec 12, 2021 in Education by JackTerrance
0 votes
    Point out the wrong statement. (a) Each universal function takes array inputs and produces array outputs (b) Broadcasting ... input arguments are ndarrays (d) All of the mentioned...
asked Oct 7, 2021 in Technology by JackTerrance
0 votes
0 votes
    Shown below is the input NumPy array. Delete column two and replace it with the new column given below....
asked Apr 24, 2021 in Technology by JackTerrance
...