Reverse integer bitwise without using loop

前端 未结 5 2153
孤独总比滥情好
孤独总比滥情好 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:07

    1100 0101

    >> 4 means we are shifting the bits by 4 to the right:

    0000 1100

    << 4 means we are shifting the bits by 4 to the left:

    0101 0000

    | is the or operation:

    0000 1100 
    ↓↓↓↓ ↓↓↓↓ |
    0101 0000
    ---------
    0101 1100
    

    As you see, the program changed the two blocks of 4 bits.

提交回复
热议问题