Numpy attempts to convert your binary number to a float, except that your number contains a b which can't be interpreted; this character was added by the bin function, eg. bin(2) is 0b10. You should remove this b character before your zfill like this by using a "slice" to remove the first 2 characters:

b[i]=bin(int(a[i]))[2:].zfill(8)
Answer from Aaron Christiansen on Stack Overflow
๐ŸŒ
w3resource
w3resource.com โ€บ python-exercises โ€บ numpy โ€บ python-numpy-exercise-98.php
NumPy: Convert the raw data in an array to a binary string and then create an array - w3resource
August 29, 2025 - Write a NumPy program to convert raw array data to a binary string and create an array. Sample Solution: Python Code: # Importing the NumPy library and aliasing it as 'np' import numpy as np # Creating a NumPy array 'x' containing floating-point values x = np.array([10, 20, 30], float) # Printing a message indicating the original array will be displayed print("Original array:") # Printing the original array 'x' with its elements print(x) # Converting the array 'x' to a binary string using the 'tostring()' method s = x.tostring() # Printing a message indicating the display of the binary string
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ how to convert numpy array to binary arrary?
r/learnpython on Reddit: How to convert numpy array to binary arrary?
January 4, 2017 - This is two list comprehensions, the outer [(stuff) for row in a] just loops over the rows of the array. The inner, [int(el==max(row)) for el in row], loops over the elements on the row, and checks whether each is equal to the maximum element of the row (el==max(row)). This returns a list of True and False values, so each is converted to a 0 or a 1 using int(), as int(True) = 1 and int(False) = 0. Note that if you have duplicate maxima, you will get duplicate 1s. How do I take sections of a binary NumPy array, then convert them to decimal form?
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ stable โ€บ reference โ€บ generated โ€บ numpy.binary_repr.html
numpy.binary_repr โ€” NumPy v2.5 Manual
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.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ fill array with binary numbers
r/learnpython on Reddit: Fill array with binary numbers
June 12, 2019 -

Hello there. I am trying to learn python for a day. I need to create something like this:

5~ inputs

array = {}

fill array with binary 0(00000) to 31(11111) and execute some other function (not relevant here). So I need that array to be for i=0 -> array = {0,0,0,0,0}, i=1 -> array = {0,0,0,0,1}, i=2 -> array = {0,0,0,1,0} and so on, I think it can be also reversed, like i=1 -> array = {1,0,0,0,0} etc.

I managed to do a for loop to get my binary numbers 00000, 00001 etc, how can I split these numbers and put them into each array position?

๐ŸŒ
w3resource
w3resource.com โ€บ python-exercises โ€บ numpy โ€บ python-numpy-exercise-187.php
Python NumPy: Convert a given vector of integers to a matrix of binary representation - w3resource
August 29, 2025 - Write a NumPy program to convert a given vector of integers to a matrix of binary representation. ... # Importing the NumPy library import numpy as np # Creating a NumPy array 'nums' containing a set of integers nums = np.array([0, 1, 3, 5, 7, 9, 11, 13, 15]) # Displaying the original vector 'nums' print("Original vector:") print(nums) # Creating a binary representation of 'nums' using bitwise operations and reshaping # The resultant array represents the binary representation of each element in 'nums' bin_nums = ((nums.reshape(-1, 1) & (2 ** np.arange(8))) != 0).astype(int) # Displaying the binary representation of the vector 'nums' print("\nBinary representation of the said vector:") print(bin_nums[:, ::-1]) # Reversing the columns to display binary digits in the correct order
Find elsewhere
๐ŸŒ
w3resource
w3resource.com โ€บ python-exercises โ€บ numpy โ€บ basic โ€บ numpy-basic-exercise-35.php
NumPy: Save a given array to a binary file - w3resource
By saving the array to a binary file, the program ensures data persistence and facilitates efficient data handling for future use. Sample Solution : Python Code : # Importing the NumPy library with an alias 'np' import numpy as np # Importing the 'os' module for operating system-dependent functionality import os # Creating a NumPy array 'a' with integers from 0 to 19 using np.arange() a = np.arange(20) # Saving the NumPy array 'a' as a file named 'temp_arra.npy' using np.save() np.save('temp_arra.npy', a) # Printing a message checking if the file 'temp_arra.npy' exists or not print("Check if '
๐ŸŒ
Vultr Docs
docs.vultr.com โ€บ python โ€บ built-in โ€บ bin()
Python bin() - Convert Integer to Binary
September 27, 2024 - In this article, you will learn how to use the bin() function to convert integers to binary strings. The discussion includes converting both positive and negative integers, and how to manipulate or format the resulting binary strings for various applications.
Top answer
1 of 6
19

You should be able to vectorize this, something like

>>> d = np.array([1,2,3,4,5])
>>> m = 8
>>> (((d[:,None] & (1 << np.arange(m)))) > 0).astype(int)
array([[1, 0, 0, 0, 0, 0, 0, 0],
       [0, 1, 0, 0, 0, 0, 0, 0],
       [1, 1, 0, 0, 0, 0, 0, 0],
       [0, 0, 1, 0, 0, 0, 0, 0],
       [1, 0, 1, 0, 0, 0, 0, 0]])

which just gets the appropriate bit weights and then takes the bitwise and:

>>> (1 << np.arange(m))
array([  1,   2,   4,   8,  16,  32,  64, 128])
>>> d[:,None] & (1 << np.arange(m))
array([[1, 0, 0, 0, 0, 0, 0, 0],
       [0, 2, 0, 0, 0, 0, 0, 0],
       [1, 2, 0, 0, 0, 0, 0, 0],
       [0, 0, 4, 0, 0, 0, 0, 0],
       [1, 0, 4, 0, 0, 0, 0, 0]])

There are lots of ways to convert this to 1s wherever it's non-zero (> 0)*1, .astype(bool).astype(int), etc. I chose one basically at random.

2 of 6
11

One-line version, taking advantage of the fast path in numpy.binary_repr:

def bin_array(num, m):
    """Convert a positive integer num into an m-bit bit vector"""
    return np.array(list(np.binary_repr(num).zfill(m))).astype(np.int8)

Example:

In [1]: bin_array(15, 6)
Out[1]: array([0, 0, 1, 1, 1, 1], dtype=int8)

Vectorized version for expanding an entire numpy array of ints at once:

def vec_bin_array(arr, m):
    """
    Arguments: 
    arr: Numpy array of positive integers
    m: Number of bits of each integer to retain

    Returns a copy of arr with every element replaced with a bit vector.
    Bits encoded as int8's.
    """
    to_str_func = np.vectorize(lambda x: np.binary_repr(x).zfill(m))
    strs = to_str_func(arr)
    ret = np.zeros(list(arr.shape) + [m], dtype=np.int8)
    for bit_ix in range(0, m):
        fetch_bit_func = np.vectorize(lambda x: x[bit_ix] == '1')
        ret[...,bit_ix] = fetch_bit_func(strs).astype("int8")

    return ret 

Example:

In [1]: vec_bin_array(np.array([[100, 42], [2, 5]]), 8)

Out[1]: array([[[0, 1, 1, 0, 0, 1, 0, 0],
                [0, 0, 1, 0, 1, 0, 1, 0]],

               [[0, 0, 0, 0, 0, 0, 1, 0],
                [0, 0, 0, 0, 0, 1, 0, 1]]], dtype=int8)
๐ŸŒ
Real Python
realpython.com โ€บ lessons โ€บ binary-arrays
Binary Arrays (Video) โ€“ Real Python
Python provides two array types for dealing with binary data, the first of which is called bytes. 00:17 A bytes object is an immutable sequence of integers in the range of 0 to 255. Thatโ€™s the capacity of a single byte.
Published: February 23, 2021
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ how do i take sections of a binary numpy array, then convert them to decimal form?
r/learnpython on Reddit: How do I take sections of a binary NumPy array, then convert them to decimal form?
December 23, 2021 -

Hello,

I have a problem using NumPy arrays. Basically, I have a NumPy array consisting of 0's and 1's. An array might look something like: np.array([1,1,0,1,1,0,1,1,0,1). However, I want to take that array and cut it into 'n' sections so that I could convert the sections into decimal form.

For example, I want to take the array presented earlier and cut it into two sections. The first section will contain [1,1,0,1,1]. Then I want to convert that into decimal form. I will do the same to the second section as well.

My question is: how do I go about sectioning my array? I think I know how to convert them to decimal But the sectioning is where I'm having a lot of difficulties. Thanks!

Things I tried:

> I tried doing array indexing and taking the first section and converting it to decimal, but I can't seem to get the second section.

> My next idea would be to put it in a for loop, but I don't know how to go about implementing that.

Thanks!

Top answer
1 of 4
4

This is the fastest I came up with. A slight variation of your initial solution:

digits = ['0', '1']
int("".join([ digits[y] for y in x ]), 2)

%timeit int("".join([digits[y] for y in x]),2)
100000 loops, best of 3: 6.15 us per loop
%timeit int("".join(map(str, x)),2)
100000 loops, best of 3: 7.49 us per loop

(Btw, it seems that in this case, using a list comprehension is faster than using a generator expression.)

EDIT:

Also, I hate being a smartass, but you can always trade memory for speed:

# one time precalculation
cache_N = 16  # or much bigger?!
cache = {
   tuple(x): int("".join([digits[y] for y in x]),2)
   for x in itertools.product((0,1), repeat=cache_N)
}

Then:

res = cache[tuple(x)]

Way faster. Of course, this is only feasible up to a point...

EDIT2:

I now see you say your lists have 32 elements. In this case the caching solution is probably infeasible, BUT we have more ways to trade speed for memory. E.g., with cache_N=16, which is surely feasible, you can access it twice:

c = 2 ** cache_N # compute once
xx = tuple(x)
cache[xx[:16]] * c + cache[xx[16:]]

%timeit cache[xx[:16]] * c + cache[xx[16:]]
1000000 loops, best of 3: 1.23 us per loop  # YES!
2 of 4
2

I decided to create a script to trial 4 different methods of doing this task.

import time

trials = range(1000000)
list1 = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0]
list0 = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
listmix = [1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0]

def test1(l):
    start = time.time()
    for trial in trials:
        tot = 0
        n = 1
    for i in reversed(l):
            if i:
                tot += 2**n
            n += 1
    print 'Time taken:', str(time.time() - start)

def test2(l):
    start = time.time()
    for trial in trials:
        int("".join(map(str, l)),2)
    print 'Time taken:', str(time.time() - start)

def test3(l):
    start = time.time()
    for trial in trials:
        sum(x << i for i, x in enumerate(reversed(l)))
    print 'Time taken:', str(time.time() - start)

def test4(l):
    start = time.time()
    for trial in trials:
        int("".join([str(i) for i in l]),2)
    print 'Time taken:', str(time.time() - start)


test1(list1)
test2(list1)
test3(list1)
test4(list1)
print '.'
test1(list0)
test2(list0)
test3(list0)
test4(list0)
print '.'
test1(listmix)
test2(listmix)
test3(listmix)
test4(listmix)

My results:

Time taken: 7.14670491219
Time taken: 5.4076821804
Time taken: 4.7349550724
Time taken: 7.24234819412
.
Time taken: 2.29213285446
Time taken: 5.38784003258
Time taken: 4.70707392693
Time taken: 7.27936697006
.
Time taken: 4.78960323334
Time taken: 5.36612486839
Time taken: 4.70103287697
Time taken: 7.22436404228

Conclusion: @goncalopp's solution is probably the best one. It is consistently fast. On the other hand, if you're likely to have more zeros than ones, stepping through the list and manually multiplying powers of two and adding them will be fastest.

EDIT: I re-wrote my script to use timeit, the source code is at http://pastebin.com/m6sSmmR6

My output result:

7.78366303444
2.79321694374
5.29976511002
.
5.72017598152
5.70349907875
5.66881299019
.
5.25683712959
5.17318511009
5.20052909851
.
8.23388290405
8.24193501472
8.15649604797
.
3.94102287292
3.95323395729
3.9201271534

My method of stepping through the list backwards adding powers of two is still faster if you have all zeros, but otherwise, @sxh2's method is definitely the fastest, and my implementation didn't even include his caching optimization.

๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 75595545 โ€บ how-to-convert-a-byte-array-to-binary-python
How to convert a byte array to binary? Python - Stack Overflow
... It looks like you answered your question already. If you have bytes that you want to write to a file, you can just write them. There's no such thing as converting to binary ...
๐ŸŒ
Finxter
blog.finxter.com โ€บ home โ€บ learn python blog โ€บ 5 best ways to convert an integer to a binary array in python
5 Best Ways to Convert an Integer to a Binary Array in Python - Be on the Right Side of Change
February 18, 2024 - ๐Ÿ’ก Problem Formulation: In Python, converting an integer to a binary array requires representing the integer as an array of bits, where each bit is a binary digit (0 or 1). For instance, converting the integer 5 into binary gives us โ€˜101โ€™, ...