Last active
August 29, 2015 14:27
-
-
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
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 <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; | |
} | |
} |
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 <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