What's wrong with my code


  • 0
    S

    This is my code, I don't know what's wrong with it.
    Appreciate any help.

    public class Solution {
        // you need treat n as an unsigned value
        public int reverseBits(int n) {
            int result = 0;
            for (int i = 0; i < 32; i++) {
                result = (result << 1) | (n & (1 << i));
            }
            return result;
        }
    }
    

    This is the wrong answer for the test cases:

    Input:
               2 (00000000000000000000000000000010)
    Output:
      2147483648 (10000000000000000000000000000000)
    Expected:
      1073741824 (01000000000000000000000000000000)

  • 0
    T

    You should not left move res << 1 when i = 31


Log in to reply
 

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