Skip to content

Instantly share code, notes, and snippets.

@goldsborough
Created November 14, 2015 20:31
Show Gist options
  • Save goldsborough/0372e6a72de98bffa489 to your computer and use it in GitHub Desktop.
Save goldsborough/0372e6a72de98bffa489 to your computer and use it in GitHub Desktop.
Checks if an integer is a palindrome in its binary representation.
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