Two 8ms C++ efficient solution


  • 0
    C
    class Solution {
    public:
    	uint32_t reverseBits(uint32_t n) {
    		int count = 0;
    		uint32_t res=0;
    		while (n)
    		{
    			res = res << 1;
    			res += (n & 1);
    			n = n >> 1;
    			count++;
    		}
    		res=res << (32 - count);
    		return res;
    	}
    };
    
    class Solution {
    public:
    	uint32_t reverseBits(uint32_t n) {
    		uint32_t mask = 0xFFFF0000;
    		n = ((n&mask) >> 16) | ((n&(~mask)) << 16);
    		mask = 0xFF00FF00;
    		n = ((n&mask) >> 8) | ((n&(~mask)) << 8);
    		mask = 0xF0F0F0F0;
    		n = ((n&mask) >> 4) | ((n&(~mask)) << 4);
    		mask = 0xCCCCCCCC;
    		n = ((n&mask) >> 2) | ((n&(~mask)) << 2);
    		mask = 0xAAAAAAAA;
    		n = ((n&mask) >> 1) | ((n&(~mask)) << 1);
    		return n;
    	}
    };

Log in to reply
 

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