Given a 32 bit integer, reverse all the 0/1 bits so that the first bit becomes the last one, the second bit becomes the one before the last one…etc.
Sample Input:
2415919101 ( = 0b10001111111111111111111111111101)
Sample Output:
3221225457 ( = 0b10111111111111111111111111110001)
In the above sample input, decimal number 2415919101 is the same as binary number 0b10001111111111111111111111111101. To reverse all the bits, we got the binary number – 0b10111111111111111111111111110001, which is 3221225457 in decimal format.
数据结构和算法:初级练习题 #5 – 反转二进制位
给定一个32位的整数,反转所有的0/1比特位,首位变成末位,次位变成倒数第二位。。。以此类推。
输入样例:
2415919101 ( = 0b10001111111111111111111111111101)
输出样例:
3221225457 ( = 0b10111111111111111111111111110001)
在上面的输入样例里,十进位数字 2415919101 转成二进制为:0b10001111111111111111111111111101。反转比特位后的二进制数字是:0b10111111111111111111111111110001,相当于十进制数字 3221225457。