How to swap nibbles in C?

后端 未结 4 1833
梦谈多话
梦谈多话 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:15

    Start from the fact that hexadecimal 0xf covers exactly four bits. There are four nibbles in a 16-bit number. The masks for the nibbles are 0xf000, 0xf00, 0xf0, and 0xf. Then start masking, shifting and bitwise OR-ing.

提交回复
热议问题