Interview Question

Embedded Software Engineer Interview

-

Apple

Is the given number a bitwise palindrome?

AnswerAdd Tags

Interview Answers

4 Answers

0

I took it as without proceeding zeroes. Also, you can cut the cycles in half by only going halfway through the bits. uint8_t isPalindrome(uint input) { uint msb = log2(input); for (int i = 0; i < msb / 2; i++) { uint8_t left = ((1 << (msb - i)) & input) != 0 ? 1 : 0; uint8_t right = ((1 << i) & input) != 0 ? 1 : 0; if (left != right) { return 0; } } return 1; }

J O on

0

#include int main(void) { //1001001 int num = 0x49246; int count = 0; int a = num; while(a > 0){ printf("%d\n", a & 1); a >>= 1; count += 1; } int left = 0, right = count - 1; while (left > left) & 1) != ((num >> right) & 1)){ printf("No"); return 1; } left += 1; right -= 1; } printf("Yes"); return 0; }

Anonymous on

1

boolean isPalindrome(int n) { for(int i=0; i<32; i++) { int temp1=n&(1<

Anonymous on

0

Easy stuff

Anonymous on

Add Answers or Comments

To comment on this, Sign In or Sign Up.