Skip to content

Instantly share code, notes, and snippets.

@gluschenko
Created July 21, 2017 22:07
Show Gist options
  • Save gluschenko/cabf006ed38d536784a568df822c0c89 to your computer and use it in GitHub Desktop.
Save gluschenko/cabf006ed38d536784a568df822c0c89 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
using namespace std;
void trace(int n)
{
int nmem = n;
vector<int> M;
while(n > 0)
{
int bit = n % 2 == 0 ? 0 : 1;
M.push_back(bit);
n /= 2;
}
for(int i = M.size(); i < 32; i++){
cout << 0;
}
for(int i = M.size() - 1; i >= 0; --i){
cout << M[i];
}
cout << " = " << nmem << "\n";
}
int main(int argc, char** argv) {
int x = 2147483647;
trace(x);
x = x >> 16;
trace(x);
x = x << 16;
trace(x);
x = x >> 1;
trace(x);
x = x >> 1;
trace(x);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment