逆转一个数中的位
Time Limit:1000MS Memory Limit:65536K
Total Submit:205 Accepted:58
Description
pzzer从不放弃任何一丝希望,只要有希望都想来个大逆袭,不过这次是想把一个无符号数x的位逆转过来,如10进制19(10011)倒转过来后就成为(11001)25.现在问题来了,就是输入个无符号数,要你求得位倒转过来的数。(0<x<=2^32-1)
Input
多组测试数据,每组包括一个无符号正整数
Output
输出逆转后的数
Sample Input
19
12
Sample Output
25
3
Source
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AK1086 {
class Program {
static void Main(string[] args) {
string sb;
while ((sb = Console.ReadLine()) != null) {
long n = long.Parse(sb);
long i = 1, ans = 0;
while (n > 0) {
long temp = n % 2;
ans = ans * 2 + temp;//摸出来的一次*2即可
n = n / 2;
}
Console.WriteLine(ans);
}
}
}
}