For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as 00111001011110000010100101000000).
翻转一个32位整数的每一个位.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
classSolution { public: uint32_treverseBits(uint32_t n){ for(int i = 0; i < 16; i++){ swapIntBit(n, i); } return n; }
voidswapIntBit(uint32_t &n, int i){ uint32_t b1 = n & (1 << i); uint32_t b2 = n & (((unsignedint)0x80000000) >> i); n &= ~(1 << i); n &= ~(((unsignedint)0x80000000) >> i); n |= b1 << (31 - i * 2); n |= b2 >> (31 - i * 2); } };