diff --git a/BitManipulation/Reverse_Bits/Reverse.cpp b/BitManipulation/Reverse_Bits/Reverse.cpp new file mode 100644 index 0000000..bf5f1a7 --- /dev/null +++ b/BitManipulation/Reverse_Bits/Reverse.cpp @@ -0,0 +1,22 @@ +class Solution { +public: + uint32_t reverseByte(uint32_t byte, map cache) { + if (cache.find(byte) != cache.end()) { + return cache[byte]; + } + uint32_t value = (byte * 0x0202020202 & 0x010884422010) % 1023; + cache.emplace(byte, value); + return value; + } + + uint32_t reverseBits(uint32_t n) { + uint32_t ret = 0, power = 24; + map cache; + while (n != 0) { + ret += reverseByte(n & 0xff, cache) << power; + n = n >> 8; + power -= 8; + } + return ret; + } +};