Created
November 14, 2015 20:31
-
-
Save goldsborough/0372e6a72de98bffa489 to your computer and use it in GitHub Desktop.
Checks if an integer is a palindrome in its binary representation.
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
bool is_binary_palindrome(unsigned int value) | |
{ | |
unsigned int left = 1 << (sizeof(value) * 8 - 1); | |
while (!(value & left)) left >>= 1; | |
for (unsigned int right = 1; left > right; left >>= 1, right <<= 1) | |
{ | |
bool left_bit = value & left; | |
bool right_bit = value & right; | |
if (left_bit != right_bit) return false; | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment