Reverse bits of a given the unsigned integer.
For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represent Ed in binary as00111001011110000010100101000000).
Follow up:
If This function is called many times, what would you optimize it?
Related Problem:reverse Integer
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
Subscribe to see which companies asked this question
1PublicclassSolution {2//You need treat n as an unsigned value3 public int reversebits (int n) {4//returninteger.reverse (n);5int result =0;6 for(int i = 0; i <; i++){7int tmp = (n&1);8n = n >> 1;9result = Result << 1;TenResult + =tmp; One } A returnresult; - } -}
Direct AC using the Integer.reverse () API.
Each time the end binary number of n is taken out, the result is shifted to the left, and the removed number is added to the end of result, and after 32 cycles, the result is obtained.
Reverse Bits Java solutin