Last active
January 13, 2016 18:56
-
-
Save cangoal/be43d839a8148bd1db72 to your computer and use it in GitHub Desktop.
LeetCode - Reverse Bits
This file contains hidden or 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
| // 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