Reverse bits of a given 32 bits unsigned integer.
For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as 00111001011110000010100101000000).
Follow up:
If this function is called many times, how would you optimize it?
Related problem: Reverse Integer
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
其他人的解法:https://leetcode.com/discuss/questions/oj/reverse-bits?sort=hot
1 public class Solution 2 { 3 // you need treat n as an unsigned value 4 public static int reverseBits(int n) 5 { 6 int m=0; 7 for(int i=0;i<32;i++) 8 { 9 m=m<<1; 10 m=m|(n&1); 11 n=n>>1; 12 } 13 return m; 14 } 15 public static void main(String args[]) 16 { 17 System.out.println(reverseBits(43261596)); 18 } 19 }