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

9 ビュー (過去 30 日間)
Alessandro
Alessandro 2014 年 12 月 10 日
コメント済み: Alessandro 2014 年 12 月 10 日
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 件のコメント
Roger Stafford
Roger Stafford 2014 年 12 月 10 日
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.

サインインしてコメントする。

採用された回答

Ryan
Ryan 2014 年 12 月 10 日
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 件のコメント
Ryan
Ryan 2014 年 12 月 10 日
Also, you may need the swapbytes command. Not sure what platform your data is coming from, or going to.
doc swapbytes
Alessandro
Alessandro 2014 年 12 月 10 日
Thank you... I had forgotten about these commands.... they works a few hundreds of time faster!
Cheers,
Alessandro

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeWaveform Generation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by