Why doesn't Long.bitCount work?


  • 0
    B

    Integer.bitCount() is accepted but Long.bitCount() doesn't work for 2147483648.

    I do not have any idea, could someone please explain?


  • 0

    Please always provide not just the input but also your output and the expected output.

    Expected is 1, your output is 33.

    Java doesn't have unsigned ints, so you're given a signed int with the same bit pattern instead. I think I saw that said in the problem statement, but it must've been another problem. Anyway, 2147483648 is one 1-bit followed by 31 zeros and thus you actually get -2147483648. Since it's negative, converting it to long means extending it with 1-bits, so it becomes 33 1-bits (followed by 31 0-bits).


  • 0
    B

    My bad. I will be more careful next time.


Log in to reply
 

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