How to convert a sequence of 32 char (0/1) to 32 bits (uint32_t)?

前端 未结 3 1965
既然无缘
既然无缘 2021-01-27 00:29

I have an array of char (usually thousands of bytes long) read from a file, all composed of 0 and 1 (not \'0\' and \'1\', in which case I could use strtoul). I want

3条回答
  •  遥遥无期
    2021-01-27 01:03

    Restricted to the x86 platform, you can use the PEXT instruction. It is part of the BMI2 instruction set extension on newer processors.

    Use 32-bit instructions in a row and then merge the results in one value with shifts.

    This is probably the optimal approach on Intel processors, but the disadvantage is that this instruction is slow on AMD Ryzen.

提交回复
热议问题