Skip to content

Instantly share code, notes, and snippets.

@jamby77
Created February 15, 2018 15:34
Show Gist options
  • Save jamby77/f112927273ed8fff86dcdd50c0d9d617 to your computer and use it in GitHub Desktop.
Save jamby77/f112927273ed8fff86dcdd50c0d9d617 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
using namespace std;
void toBin(int n)
{
if (n == 0 || n == 1)
{
cout << n;
return;
}
int a[64] = {0}, i;
for (i = 0; n > 0; i++)
{
a[i] = n % 2;
n = n / 2;
}
for (i = i - 1; i >= 0; i--)
{
cout << a[i];
}
}
void outputNumInBin(string msg, int n)
{
cout << msg;
toBin(n);
cout << endl;
}
int main()
{
unsigned int x = 25;
unsigned int mask = 1;
unsigned int n = 3;
outputNumInBin("mask: ", (mask << n));
outputNumInBin("x: ", x);
outputNumInBin("N bit is: ", ((x & (mask << n)) >> n));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment