Skip to content

Instantly share code, notes, and snippets.

@mgarod
Created October 27, 2016 17:51
Show Gist options
  • Save mgarod/6c308fe5a38f15e125f76008d12a3567 to your computer and use it in GitHub Desktop.
Save mgarod/6c308fe5a38f15e125f76008d12a3567 to your computer and use it in GitHub Desktop.
#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