c - Redistribute bits from byte array to set bits -
c - Redistribute bits from byte array to set bits -
i wish move bits 0,8,16,24 of 32-bit value bits 0,1,2,3 respectively. other bits in input , output zero.
obviously can this:
c = c>>21 + c>>14 + c>>7 + c; c &= 0xf;
but there faster (fewer instructions) way?
c = (((c&bits_0_8_16_24) * bits_0_7_14_21) >> 21) & 0xf;
or wait intel haswell processor, doing in 1 instruction (pext).
update
taking business relationship clarified constraints
, assuming 32-bit unsigned values
, code may simplified this:
c = (c * bits_7_14_21_28) >> 28;
c bit-manipulation
Comments
Post a Comment