Code:
#define GET_BIT(p,i) ((p) & (1 << (i)))
bool isPalindrome(unsigned int n)
{
/* find length of n's binary representation: */
unsigned int k = n;
int len = 0;
while (k != 0)
{
k /= 2;
len++;
}
for (int i = 0; i < len / 2; i++)
{
if ( (GET_BIT(n, i) != 0 && GET_BIT(n, len - i - 1) != 0)
|| (GET_BIT(n, i) == 0 && GET_BIT(n, len - i - 1) == 0) )
;
else return false;
}
return true;
}
A little more than 6 lines i guess. The if clause is messy because for some ****ing reason, get_bit kept returning the integer value of the kth bit. I explained that in my email.
It does work i’m pretty sure though.
I need to sign up for leetcode and grind the *** out of puzzles, i’m really rusty
Last edited by jmakin; 02-08-2018 at 11:00 PM.