efficient extraction of bytes/bits fomr a long list of uint64s

9 Ansichten (letzte 30 Tage)
Alessandro
Alessandro am 10 Dez. 2014
Kommentiert: Alessandro am 10 Dez. 2014
Dear all,
I have an instrument that generate very long data streams, on the order of 30,000,000 uint64 numbers. For instance:
mem = uint64(rand(30000000,1))
I have to extract specific bits within each of these uint64 in order to read my data. First I need to convert mem to binary for instance with:
tmp = dec2bin(mem,64)
Then I extract a number of bits, e.g.,
val = tmp(:,1:32)
And convert them back to decimal
val = bin2dec(val)
unfortunately this operation takes minutes because of the very long array of values I have and I have to repeat it a few times to extract different values. The execution time of dec2bin and bin2dec is not great or at least not suitable for this specific task.
Do you have any suggestion on how to recover these values in a more efficient way within Matlab?
Kind regards,
Alessandro
  1 Kommentar
Roger Stafford
Roger Stafford am 10 Dez. 2014
Be aware of a limitation on 'dec2bin' as given in its site
http://www.mathworks.com/help/matlab/ref/dec2bin.html
"str = dec2bin(d) returns the binary representation of d as a string. d must be a nonnegative integer smaller than 2^52."
This apparently means that the upper 12 bits in uint64 integers are not handled properly in the conversion to a string of bits, so you presumably have more worries than just the time of execution.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Ryan
Ryan am 10 Dez. 2014
Instead of converting the array to binary (dec2bin converts the number of a string of the binary representation), use the bitand and bitshift commands. Another option would be to convert the unit64 to uint32, uint16, or uint8 as needed with the typecast command. This would be a lot faster, since you wouldn't be dealing with strings.
Check out
doc bitand
doc bitshift
doc typecast
  2 Kommentare
Ryan
Ryan am 10 Dez. 2014
Also, you may need the swapbytes command. Not sure what platform your data is coming from, or going to.
doc swapbytes
Alessandro
Alessandro am 10 Dez. 2014
Thank you... I had forgotten about these commands.... they works a few hundreds of time faster!
Cheers,
Alessandro

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by