# C program to check whether a given number is palindrome or not using Bitwise Operator

Palindrome number check program in C: In this article, we are going to see how to use bitwise operators to find whether a number (binary representation) is palindrome or not?
Submitted by Radib Kar, on December 26, 2018

## Problem statement

Write a C program to check whether a number (binary representation) is palindrome or not using bitwise operators. Maximum input is 255..

Solution: We can use bitwise operator here to solve the problem.

Pre-requisite: Input number n

Input Example:

```    Input number: 24
Binary representation: 00011000
Thus it's a palindrome

Input number 153:
Binary representation: 10011001
Thus it's a palindrome

Input number: 25
Binary representation: 00011001
Thus it's not a palindrome
```

## Algorithm

```1)  Take the input.
2)  Create an array of length 8 to store 8 bit binary representation
of input number
3)  Use bitwise operators to convert into binary from
a)  Initialize i to 7 (8-1)
b)  Find ith bit & store in the array
array[i]=n & 1
c)  Right shift n & decrement i
n=n>>1
d)  IF n=0
Break
ELSE
Repeat step b-d
4)  Check the array where the binary representation is stored,
for palindrome
a)  Set j= 0 & k=7 (8-1, 8 is the MAX SIZE)
b)  IF (array [j]!= array [k])
It's not a palindrome
c)  IF j < k
Increment j& decrement k
Repeat b, c
ELSE
It's a palindrome
```

## Example with explanation

```Input no: 24
Converting to binary representation using bitwise operator

Initially,
N=24
Array[8]={0};

0	0	0	0	0	0	0	0 (LSB)
i=7
-----------------------------------------------------------------

first iteration,
array[7]=n&1 = 0 (refer to bitwise operators and
their working for understanding the outcome)
0	0	0	0	0	0	0	0 (current bit)
n=n>>1=12
i=6
-----------------------------------------------------------------

second iteration,
array [6]=n&1 = 0
0	0	0	0	0	0	0 (current bit)	0

n=n>>1=6
i=5
-----------------------------------------------------------------

third iteration,
array [5]=n&1 = 0
0	0	0	0	0	0 (current bit)	0 	0

n=n>>1=3
i=4
-----------------------------------------------------------------

fourth iteration,
array [4]=n&1 = 1
0	0	0	0	1(current bit)	0 	0 	0

n=n>>1=1
i=3
-----------------------------------------------------------------

fifth iteration,
array [3]=n&1 = 1
0	0	0	1 (current bit)	1	0 	0 	0

n=n>>1=0
i=2
-----------------------------------------------------------------

sixth iteration,
n=0
so no more processing
thus the array finally becomes which is
the binary representation
0	0	0	1 	1	0 	0 	0  (LSB)

Checking palindrome
Initially,
j=0 & k=7 (8-1)
-----------------------------------------------------------------

First iteration
Array[j] == array [k] (both 0)
j=j+1=1
k=k-1=6
j<k
-----------------------------------------------------------------

Second iteration
Array[j] == array [k] (both 0)
j=j+1=2
k=k-1=5
j<k
-----------------------------------------------------------------

Third iteration
Array[j] == array [k] (both 0)
j=j+1=3
k=k-1=4
j<k
-----------------------------------------------------------------

Fourth iteration
Array[j] == array [k] (both 1)
j=j+1=4
k=k-1=3
j>k
thus, stops processing & prints it's a palindrome
```

## C Implementation

```#include <stdio.h>

#define SIZE 8

int main() {
unsigned int n;

printf("enter the no ( max range 255)\n");
scanf("%d", & n);

int c[SIZE] = {0};

int i = SIZE - 1;

printf("binary representation is: ");
while (n != 0) {
c[i--] = n & 1;
n = n >> 1;

}

for (int j = 0; j < SIZE; j++)
printf("%d", c[j]);
printf("\n");

for (int j = 0, k = SIZE - 1; j < k; j++, k--) {
if (c[j] != c[k]) {
printf("Not palindrome\n");
return 0;
}
}

printf("it's palindrome\n");

return 0;
}
```

### Output

```First run:
enter the no ( max range 255)
153
binary representation is: 10011001
it's palindrome

Second run:
enter the no ( max range 255)
24
binary representation is: 00011000
it's palindrome

Third run:
enter the no ( max range 255)
-8
binary representation is: 11111000
Not palindrome
```