Created
September 8, 2015 09:41
-
-
Save Redchards/b303b7bc2c11e6ca10db to your computer and use it in GitHub Desktop.
To very small utility functions to extract bits from a number (or similar data type).
This file contains hidden or 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 <cstdint> | |
int64_t number = 0; | |
int main() | |
{ | |
while(true) | |
{ | |
std::cout << "Enter a number to parse" << std::endl; | |
std::cin >> number; | |
for(int i = (sizeof(number) * 8) - 1; i >= 0; --i) | |
{ | |
std::cout << ((number & ((int64_t)1 << i)) >> i); | |
} | |
std::cout << std::endl; | |
} | |
} |
This file contains hidden or 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 <cstdint> | |
int64_t number = 0; | |
int64_t index = 0; | |
int main() | |
{ | |
while(true) | |
{ | |
std::cout << "Enter a number to parse" << std::endl; | |
std::cin >> number; | |
std::cout << "Enter the bit to extract" << std::endl; | |
std::cin >> index; | |
/*for(int i = (sizeof(number) * 8) - 1; i >= 0; --i) | |
{*/ | |
std::cout << ((number & (1 << (index - 1))) >> (index - 1)); | |
//} | |
std::cout << std::endl; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment