Skip to content

Instantly share code, notes, and snippets.

@Redchards
Created September 8, 2015 09:41
Show Gist options
  • Save Redchards/b303b7bc2c11e6ca10db to your computer and use it in GitHub Desktop.
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).
#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;
}
}
#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