本文主要是介绍**Leetcode 190. Reverse Bits,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
for 8 bit binary number abcdefgh, the process is as follow:
abcdefgh -> efghabcd -> ghefcdab -> hgfedcba
非常牛逼的写法。。
转自:
https://leetcode.com/problems/reverse-bits/discuss/54741/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;}
};
这篇关于**Leetcode 190. Reverse Bits的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!