0% found this document useful (0 votes)
12 views1 page

Bitwise Palindrome Check in Java

1) The document describes a method to check if an integer is a palindrome by comparing the leftmost and rightmost bits. 2) It explains that the number of bits in the integer is first determined, and then the leftmost and rightmost pointers are set accordingly before comparing the bits using bitwise AND. 3) Sample Java code is provided to get the number of bits in an integer, set the pointers, and check if the bits at the pointers are equal or not over half the length of the integer to determine if it is a palindrome.

Uploaded by

venitia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views1 page

Bitwise Palindrome Check in Java

1) The document describes a method to check if an integer is a palindrome by comparing the leftmost and rightmost bits. 2) It explains that the number of bits in the integer is first determined, and then the leftmost and rightmost pointers are set accordingly before comparing the bits using bitwise AND. 3) Sample Java code is provided to get the number of bits in an integer, set the pointers, and check if the bits at the pointers are equal or not over half the length of the integer to determine if it is a palindrome.

Uploaded by

venitia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

The idea is simple, we take two pointers one at leftmost bit and another at rightmost bit, extract

bits information using the pointers both bits should either be 0 or 1. and move the pointers until
they
meet.
Now how would you extract the bit is tricky one, basically you first get number of bits in the
integer and then shift 1 left by (number of bits -1) this will become our leftmost pointer and
number 1 will be our rightmost pointer. Use & operator between original number and these
pointers if the result is 0 then the corresponding bit is zero else(non-zero) then corresponding bit
is
1.
Note: check for non-zero rather then > 0 as 1000000000000 will be negative.

Here is the java code to get the size of the integer


public int getSizeOfInteger(){
int size =1;
int num =1;
//shift the number untill it becomes negative(1 followed by
0's)

while(num > 0){


num = num<<1;
size++;
}
return size;
}

Here is the java code to get if the bit representation of the number is palindrome or not
public boolean isPalindrome(int n){
int rightPointer =1;
int leftPointer = 1<<(getSizeOfInteger()-1);

then negate

int count = getSizeOfInteger()/2;


while(count > 0){
//Checking if both bits are 0 or both are non-zero and
if( !(((n & leftPointer) != 0 && (n & rightPointer) !=

0) ||

( (n & leftPointer) == 0 && (n &

rightPointer) == 0))){

return false;//Both bits are not same


}
count--;

}
return true;

You might also like