# O(1) bit operation C++ solution (8ms)

• ``````class Solution {
public:
uint32_t reverseBits(uint32_t n) {
n = (n >> 16) | (n << 16);
n = ((n & 0xff00ff00) >> 8) | ((n & 0x00ff00ff) << 8);
n = ((n & 0xf0f0f0f0) >> 4) | ((n & 0x0f0f0f0f) << 4);
n = ((n & 0xcccccccc) >> 2) | ((n & 0x33333333) << 2);
n = ((n & 0xaaaaaaaa) >> 1) | ((n & 0x55555555) << 1);
return n;
}
};
``````

for 8 bit binary number abcdefgh, the process is as follow:

abcdefgh -> efghabcd -> ghefcdab -> hgfedcba

• Smart! kind of merge sort!

• This is not O(1), this is O(log sizeof(int) )

• I think you are right, this solution is too specific. With different size of int, the code needs to be changed to adapt the new size. The operations will always be log sizeof(int) to finish each individual tasks.

• Great! Divide and Conquer!

• sizeof(int) is fixed on the same computer and it's always a small number (64, I don't know whether there're larger numbers)

• SO SMART.....................................

• So a SMART\\\\\\\

• so cooooooool~~~~

• The same algorithm appears in `Integer.reverse` of JDK and is discussed in Hackers Delight. Just FYI

• Amazing! Diviiiiiiiiide and Coooonquer!

• Awesome!!!Amazing!!

• This post is deleted!

• poor guy tried to tell the source and got downvoted.

• You are the master of bit manipulation!!!

• hard to understand

• wow~~oh my god

• So beautiful, like a dancing.

• you are a genius, bro

• When I see your code, the very string "66666666666" come to my mind,

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.