Reverse integer bitwise without using loop

前端 未结 5 2132
孤独总比滥情好
孤独总比滥情好 2021-02-10 01:39

I want to write a program which reverses the bits of an integer.
Ex 11000101 to 10100011
I know how to solve this using a loop, but I came across solutions that do it us

5条回答
  •  醉话见心
    2021-02-10 02:02

    If your integers are in range 0..255, it is acceptable to tabulate all 256 reversed values. (You can tabulate just nibbles but that would be a poor man's solution.)

    For larger values, swap and simultaneously reverse the bytes using that table. You can do the swap by a combination of masks and shifts, or by mapping four bytes onto the int.

提交回复
热议问题