Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created July 16, 2016 23:59
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/2b753978ffe983600a031a0fcda7b3a6 to your computer and use it in GitHub Desktop.
Save jianminchen/2b753978ffe983600a031a0fcda7b3a6 to your computer and use it in GitHub Desktop.
Reverse bit 32 unsigned integer - fail to see the issue - 32 bit int primitive in Java language, need to do (long)1 >> power
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 += (long)(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