小编Tom*_*zen的帖子

在Matlab中提取嵌入32位二进制数中的多个数字的最快方法是什么

我有一个 32 位数字 (uint32),其中包含四个数字,如下所示:

  • Var1 位于位 32:31 中
  • Var2 位于位 30:22 中
  • Var3 位于位 21:13 中
  • Var4 位于位 12:1 中

以下代码有效,但我想让它更快

Var1=bitshift(fourbytes,-30);
Var2_temp=bitshift(fourbytes,-21);
Var2=bitand(Var2_temp,511);
Var3_temp=bitshift(fourbytes,-12);
Var3=bitand(Var2_temp,511);
Var4=bitand(fourbytes,2^12-1));
Run Code Online (Sandbox Code Playgroud)

例子:

fourbytes = 2149007896;
Run Code Online (Sandbox Code Playgroud)

结果是

Var1=2;
Var2=0;
Var3=372
Var4=536
Run Code Online (Sandbox Code Playgroud)

我尝试过类似的东西

Var1=bin2dec(num2str(bitget(fourbytes,32:-1:31)));
Run Code Online (Sandbox Code Playgroud)

但这和 bi2de 一样慢得令人难以置信

bi2de(bitget(onebyte(1),32:-1:31),'left-msb');
Run Code Online (Sandbox Code Playgroud)

是我用 C 编写这部分的唯一选择,还是我缺少更好的方法?

matlab binary-data

4
推荐指数
1
解决办法
118
查看次数

标签 统计

binary-data ×1

matlab ×1