Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created July 16, 2016 23:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/7bfdcd0c1643bf01c2bf6ba7acea6f70 to your computer and use it in GitHub Desktop.
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
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;
}
}
@jianminchen
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment