Created
July 16, 2016 23:47
-
-
Save jianminchen/395ccf0277250e5d3d68498e0d2238f1 to your computer and use it in GitHub Desktop.
Reverse bit 32 unsigned integer - using Math.pow function
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 += Math.pow(2, 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