Skip to content

Instantly share code, notes, and snippets.

@cangoal
Last active January 13, 2016 18:56
Show Gist options
  • Save cangoal/be43d839a8148bd1db72 to your computer and use it in GitHub Desktop.
Save cangoal/be43d839a8148bd1db72 to your computer and use it in GitHub Desktop.
LeetCode - Reverse Bits
// better solution
public int reverseBits(int n) {
int ret = 0;
for(int i=0; i<32; i++){
ret = (ret << 1) + (n & 1);
n >>=1;
}
return ret;
}
//
public int reverseBits(int n) {
int ret = 0;
for(int i=0; i<32; i++){
ret += ((n >>> i) & 1) << (31-i);
}
return ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment