Created
February 15, 2018 15:34
-
-
Save jamby77/f112927273ed8fff86dcdd50c0d9d617 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> | |
#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