# Convert UInt array to bitarray

Hi everyone.
I want to convert unit array to bitarrays. Next, I must convert each 20 bits to uint . Is there fast method for doing it?
Best regards.

``````x= BitArray(undef,1280×8)
for i in 1:1280
### convert bytearray to bitarray

for j in 1:512
a=0,v=1
for i in 1:20
a+=x[(j-1)×20+i]×v
v<<=1
end
end
#### convert every 20 bit to uint
``````

Thanks a lot

You can take a look here for an efficient approach: I have: Vector{UInt8}. I need: BitVector

Accessing bitarrays element by element is going to be extremely inefficient, so you should try something else.

BTW, I think you probably want your output matrix to be 8x1280 instead of 1280x8. Julia Arrays are column major so neighbors along a column are next to each other in memory, and also linear indexing (which you are using) moves down columns.

Thanks a lot
First segment for converting data to bitarray is solved. Thanks for it. Second segment is here.
I want to convert bitarray to int32. I mean, I have 1280×8=10240 bits data. Now, I want to select 512 vector that each data has 20 bits. My result must be 512×20 arrays of int.

Again, I belive you have your dimensions reversed. Don’t you mean 20x512 instead of 512x20? This is what I tried to explain in my previous post. Or do I misunderstand something?

I don’t know how I spilit bitarray that can select 20 bits from data and convert it to int32. My sample code is done correctly but it is inefficient. My main problem is converting 20 bits bitarrays to int32 for all data.
If I want to convert bitarrays to uint16, it will be easy.

``````M=reinterpret(UInt16, x.chunks)
``````

We don’t have 20 bits variable. How does I do that?