Skip to content

Instantly share code, notes, and snippets.

@Redchards
Last active August 29, 2015 14:27
Show Gist options
  • Save Redchards/04ea557a99e8e31214f0 to your computer and use it in GitHub Desktop.
Save Redchards/04ea557a99e8e31214f0 to your computer and use it in GitHub Desktop.
Little tidbit that a friend asked me to write some time ago, in order for him to understand bitwise operations
#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;
std::cout << ((number & (1 << (index - 1))) >> (index - 1));
std::cout << std::endl;
}
}
#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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment