What's wrong with my c++ code?


  • 0
    Y
    class Solution {
    public:
        uint32_t reverseBits(uint32_t n) {
            int result = 0;
            int temp;
            int residual = n;
            for(int i=0;i<32;i++){
                temp = residual%2;
                result += temp*pow(2, 31-i);
                residual = residual/2;
            }
            return result;
        }
    };
    

    The input 1 will generate an output number with all 1s in each bit. What's wrong?


  • 0
    Y

    Oh. I should change the int to uint32_t. Then everything is ok.


Log in to reply
 

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