How to swap nibbles in C?

后端 未结 4 1831
梦谈多话
梦谈多话 2021-01-07 04:41

How to swap the nibble bit positions of a number?

For example: 534, convert it into binary, the rightmost 4 bits has to be interchanged with the leftmost 4 bits and

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-07 05:05

    Sean Anderson's bit twiddling guide has the following:

    // swap nibbles ... 
    v = ((v >> 4) & 0x0F0F0F0F) | ((v & 0x0F0F0F0F) << 4);
    

    under the entry for Reverse an N-bit quantity in parallel in 5 * lg(N) operations.

提交回复
热议问题