Created
July 16, 2016 23:49
-
-
Save jianminchen/7bfdcd0c1643bf01c2bf6ba7acea6f70 to your computer and use it in GitHub Desktop.
reverse bit 32 unsigned integer - with issue - integer 32 bit - first bit 1 goes to negative number
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution { | |
public long reverse(long a) { | |
long ret = 0; | |
int index=0; | |
long power = 31; | |
while(a > 0 && index < 32) | |
{ | |
int val = (int)(a &1); | |
if(val == 1) | |
ret += 1 << power; | |
a = a >>1; // right shift | |
index++; | |
power--; | |
} | |
return ret; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://codelab.interviewbit.com/problems/revbits/
Sorry, wrong answer. Your program's output doesn't match the expected output. You can try testing your code with custom input and try putting debug statements in your code.
Your submission failed for the following input:
A : 3
Your function returned the following :
-1073741824
The expected returned value :
3221225472