编写函数reverse,将val(32位无符号整数)的二进制位反序。比如,如果val的二进制表示为1011000011111111,反序后val的二进制表示为1111111100001101。
unsigned int reverse(unsigned int val)
{
}
16进制的一个无符号整数
16进制的一个无符号整数
0x1
80000000
unsigned int reverse(unsigned int num) { unsigned int res; unsigned int temp; for(int i = 0; i < 32; i++){ temp = (num >> i) & 1; res = res | (temp << 31 - i); } return res; }
unsigned int reverse(unsigned int num) { //TODO: int stack[32]={}; // 入栈 for(int i=0; i<32;i++){ int tmp = (num>>1)<<1; if(tmp == num) // 先左移再右移 和原来相等 则低位为零 stack[i]=0; else stack[i]=1; num=num>>1; } //出栈 unsigned int res=0; for(int i=0; i<31; i++){ // 循环30次,但结束循环左移31位 res=res+stack[i]; res=res<<1; } res=res+stack[31]; // 加上最后一位 return res; }
#include <iostream> #include <vector> #include <string> #include <map> #include <algorithm> using namespace std; int flag = 0; int main() { map<char, string>m; map<char,string>m2; map<char,string>m1; m1.insert(pair<char, string>('0', "0000")); m1.insert(pair<char, string>('1', "0001")); m1.insert(pair<char, string>('2', "0010")); m1.insert(pair<char, string>('3', "0011")); m1.insert(pair<char, string>('4', "0100")); m1.insert(pair<char, string>('5', "0101")); m1.insert(pair<char, string>('6', "0110")); m1.insert(pair<char, string>('7', "0111")); m1.insert(pair<char, string>('8', "1000")); m1.insert(pair<char, string>('9', "1001")); m.insert(pair<char, string>('A', "1010")); m.insert(pair<char, string>('B', "1011")); m.insert(pair<char, string>('C', "1100")); m.insert(pair<char, string>('D', "1101")); m.insert(pair<char, string>('E', "1110")); m.insert(pair<char, string>('F', "1111")); m2.insert(pair<char, string>('a', "1010")); m2.insert(pair<char, string>('b', "1011")); m2.insert(pair<char, string>('c', "1100")); m2.insert(pair<char, string>('d', "1101")); m2.insert(pair<char, string>('e', "1110")); m2.insert(pair<char, string>('f', "1111")); string str1; string str2; string str3; string str4; cin >> str1; str1 = str1.substr(2, str1.size() - 2); //cout << str1 << endl; for (auto it = str1.begin(); it != str1.end(); it++)//逆序遍历 { if(*it >= 'A' && *it <= 'F') { flag = 1; for (auto it2 = m.begin(); it2 != m.end(); it2++) { if (*it == it2->first) { str2 += it2->second; break; } } } else if(*it >= 'a' && *it <= 'f') { flag = 0; for (auto it3 = m2.begin(); it3 != m2.end(); it3++) { if (*it == it3->first) { str2 += it3->second; break; } } } else if(*it >= '0' && *it <= '9') { for (auto it4 = m1.begin(); it4 != m1.end(); it4++) { if (*it == it4->first) { str2 += it4->second; break; } } } } int num = 32 - str2.size(); if (num > 0) { str3.append(num, '0'); str2 = str3 + str2; } //cout << str2 <<" " <<str2.size()<< endl; reverse(str2.begin(), str2.end());//翻转二进制 //cout << str2 << endl; for (int i = 0; i < str2.size();i+=4) { if(flag == 1) { m.insert(m1.begin(),m1.end()); for (auto it2 = m.begin(); it2 != m.end(); it2++) { if (str2.substr(i, 4) == it2->second) { str4 += it2->first; break; } } } else { m2.insert(m1.begin(),m1.end()); for (auto it2 = m2.begin(); it2 != m2.end(); it2++) { if (str2.substr(i, 4) == it2->second) { str4 += it2->first; break; } } } } cout << str4 << endl; }
#include <stdio.h> #include <string.h> unsigned int reverse(unsigned int num) { //TODO: unsigned int res=0; unsigned int i=0; for(i=0;i<32;i++) { res=res<<1; if(num & (1 << i)) res+=1; } return res; } int main(int argc, char *argv[]) { unsigned int num = 0; unsigned int ret = 0; if (1 != fscanf(stdin, "0x%x", &num)) { fprintf(stderr, "input error\n"); return 0; } ret = reverse(num); printf("%08x\n", ret); return 0; }