本文介绍了将最低有效位从4字节数组重新分配给半字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望将32位值的位0、8、16、24分别移动到位0、1、2、3.输入和输出中的所有其他位将为零.
I wish to move bits 0,8,16,24 of a 32-bit value to bits 0,1,2,3 respectively. All other bits in the input and output will be zero.
很明显,我可以这样:
c = c>>21 + c>>14 + c>>7 + c;
c &= 0xF;
但是有没有一种更快的(更少的说明)方式?
But is there a faster (fewer instructions) way?
推荐答案
c = (((c&BITS_0_8_16_24) * BITS_0_7_14_21) >> 21) & 0xF;
或者等待Intel Haswell处理器,仅用一条指令(pext)完成所有这些操作.
Or wait for Intel Haswell processor, doing all this in exactly one instruction (pext).
更新
考虑到clarified constraints
并假设32-bit unsigned values
,代码可以简化为:
Taking into account clarified constraints
and assuming 32-bit unsigned values
, the code may be simplified to this:
c = (c * BITS_7_14_21_28) >> 28;
这篇关于将最低有效位从4字节数组重新分配给半字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!