anon.. would this work for a number like 17 (10001)?

13 Answers

* This post has been removed. Please see our Community Guidelines or Terms of Service for more information.*

▲

0

▼

anon.. would this work for a number like 17 (10001)?

▲

0

▼

bool checkPalindrome(unsigned int n)

{

int m = n, k =0;

bool ret = false;

while(m!=0)

{

int i = 1;

i = i & m;

k = k > 1;

}

if((k^n)==0)

{

cout<<"Palindrome"<

▲

0

▼

I have a simple solution. Reverse the bits one by one and test equality of the two resulting numbers. (Matlab code)

function [r] = isPalindrome(a)

n = a;

m = 0;

while(n>0)

m = bitshift(m, 1);

m = m + mod(n, 2);

n = bitshift(n, -1);

end

r = (m==a);

end

▲

0

▼

public static boolean isPalindrome(int n) {

int nb = 0, nl=n;

while(nl>0) {

nb=(nb>1;

}

return nb==n;

}

▲

0

▼

@john/catalin4ever,

i think it'll fail because it doesn't concat the 0s. For example: if the input is 0110,

then after execution "nb" becomes to 0011. this is because of the condition: while(nl>0)

▲

0

▼

Ask interviewer if they want the MSB that is a 1 to dictate the bit range to check, have it given as a parameter, or assume sizeof(T)*8 perhaps. Little details and extras like this can make a difference to them.

public static bool CheckPalindrome(uint n)

{

return CheckPalindrome(n, 0);

}

unsafe public static bool CheckPalindrome(uint n, int bits)

{

if (bits == 0)

{

// Determine bits to check as the MSB having a 1

uint temp = n;

while (temp != 0)

{

++bits;

temp >>= 1;

}

}

uint m1 = (uint)1 > 1; i > 0; --i)

{

if (((n & m1) != 0) ^ ((n & m2) != 0))

{

return false;

}

m1 >>= 1;

m2 <<= 1;

}

return true;

}

Examples:

BitOps.CheckPalindrome(17, 0) is true

BitOps.CheckPalindrome(17, 8) is false

BitOps.CheckPalindrome(0xABD5, 0) is true

BitOps.CheckPalindrome(0xABD5, 16) is true

BitOps.CheckPalindrome(0x5BDA, 0) is false

BitOps.CheckPalindrome(0x5BDA, 16) is true

▲

0

▼

string binary;

int start=0,

int end= binary.length -1;

while(start < end)

{

if (binary[start] == binary[end])

{

start ++; end- -;

}

else return false;

}

return true;

▲

0

▼

int isPalindrome( int num )

{

int x = num & num;

return ( x == num ) ? 1 : 0;

}

▲

0

▼

/* function declaration goes here.*/

int isPalin(int orig);

int main()

{

int x = 9;

int i = isPalin(x);

printf("i = %d \n", i);

}

int isPalin(int orig)

{

int copy = orig;

static int reversed = 0;

while(copy!=0)

{

reversed >= 1;

}

return (reversed == orig);

▲

0

▼

We can compare the leftmost and rightmost bits by left shifting and right shifting the bits and also by getting rid of those bits . If the binary number is a palindrome, the left and right bits should be equal.

Here is a C# implementation of the above logic

static bool IsBinaryPalindrome(byte num)

{

int i = 0;

while (true)

{

i++;

bool right = false, left = false;

byte origNo = num;

if (((num > i) != origNo)

{

left = true; //left most bit contains one

}

num >>= i; //putting the right bit back in its original position

origNo = num;

if (((num >>= i) << i) != origNo)

{

right = true; // right most bit contains one

}

num <<= i; //putting the left bit back in its original position

if (left != right)

{

return false;

}

if (num == 0 || num == 1) break;

}

return true;

}

▲

0

▼

python one-line need to replace the the 0b

>>> str(bin(255).replace('0b',''))==str(bin(255).replace('0b',''))[::-1]

True

To comment on this, Sign In or Sign Up.

Would you like us to review something? Please describe the problem with this {0} and we will look into it.

Your feedback has been sent to the team and we'll look into it.

This was the first question I was asked and is considered a warm up.