Created
October 27, 2016 17:51
-
-
Save mgarod/6c308fe5a38f15e125f76008d12a3567 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
int reverseAsHex(int n) { | |
int mask = 0xF; // or 15 | |
int answer = 0; | |
while (n != 0) { | |
answer = (answer << 4) | (n & mask); | |
n >>= 4; | |
} | |
return answer; | |
} | |
int main() { | |
int num = 1234; | |
cout << std::dec << num << endl; | |
cout << std::hex << num << endl; | |
int reverse = reverseAsHex(num); | |
cout << std::dec << reverse << endl; | |
cout << std::hex << reverse << endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment