Check if binary representation of a number is palindrome - GeeksforGeeks

02 Nov, 2021
Given an integer ‘x’, write a C function that returns true if binary representation of x is palindrome else return false.For example a numbers with binary representation as 10..01 is palindrome and number with binary representation as 10..00 is not palindrome.The idea is similar to checking a string is palindrome or not. We start from leftmost and rightmost bits and compare bits one by one. If we find a mismatch, then return false. 
Algorithm: isPalindrome(x) 1) Find number of bits in x using sizeof() operator. 2) Initialize left and right positions as 1 and n respectively. 3) Do following while left ‘l’ is smaller than right ‘r’. .......a) If bit at position ‘l’ is not same as bit at position ‘r’, then return false. .......b) Increment ‘l’ and decrement ‘r’, i.e., do l++ and r–-. 4) If we reach here, it means we didn’t find a mismatching bit.To find the bit at a given position, we can use the idea similar to this post. The expression “x & (1 << (k-1))” gives us non-zero value if bit at k’th position from right is set and gives a zero value if if k’th bit is not set.
Following is the implementation of the above algorithm. 
C++
Java
Python3
C#
PHP
Javascript
// C++ Program to Check if binary representation// of a number is palindrome#include<iostream>using namespace std; // This function returns true if k'th bit in x// is set (or 1). For example if x (0010) is 2// and k is 2, then it returns truebool isKthBitSet(unsigned int x, unsigned int k){    return (x & (1 << (k - 1))) ? true : false;} // This function returns true if binary// representation of x is palindrome.// For example (1000...001) is palindromebool isPalindrome(unsigned int x){    int l = 1; // Initialize left position    int r = sizeof(unsigned int) * 8; // initialize right position     // One by one compare bits    while (l < r)    {        if (isKthBitSet(x, l) != isKthBitSet(x, r))            return false;        l++; r--;    }    return true;} // Driver Codeint main(){    unsigned int x = 1 << 15 + 1 << 16;    cout << isPalindrome(x) << endl;    x = 1 << 31 + 1;    cout << isPalindrome(x) << endl;    return 0;}
// Java Program to Check if binary representation// of a number is palindromeclass GFG{     // This function returns true if k'th bit in x    // is set (or 1). For example if x (0010) is 2    // and k is 2, then it returns true    static int isKthBitSet(long x, long k)    {        int rslt = ((x & (1 << (k - 1))) != 0) ? 1 : 0;        return rslt;    }         // This function returns true if binary    // representation of x is palindrome.    // For example (1000...001) is palindrome    static int isPalindrome( long x)    {        long l = 1; // Initialize left position        long r = (Integer.SIZE/8 )* 8; // initialize right position             // One by one compare bits        while (l < r)        {            if (isKthBitSet(x, l) != isKthBitSet(x, r))            {                return 0;            }            l++; r--;        }        return 1;    }         // Driver Code    public static void main (String[] args)    {        long x = 1 << 15 + 1 << 16 ;        System.out.println(isPalindrome(x));                 x = (1 << 31) + 1 ;        System.out.println(isPalindrome(x));    }} // This code is contributed by AnkitRai01
# python 3 Program to Check if binary representation# of a number is palindromeimport sys# This function returns true if k'th bit in x# is set (or 1). For example if x (0010) is 2# and k is 2, then it returns truedef isKthBitSet(x, k):    if ((x & (1 << (k - 1))) !=0):        return True    else:        return False # This function returns true if binary# representation of x is palindrome.# For example (1000...001) is palindromedef isPalindrome(x):    l = 1 # Initialize left position    r = 2 * 8 # initialize right position     # One by one compare bits    while (l < r):        if (isKthBitSet(x, l) != isKthBitSet(x, r)):            return False        l += 1        r -= 1         return True # Driver Codeif __name__ =='__main__':    x = 1 << 15 + 1 << 16    print(int(isPalindrome(x)))    x = 1 << 31 + 1    print(int(isPalindrome(x))) # This code is contributed by# Surendra_Gangwar
// C# Program to Check if binary representation// of a number is palindromeusing System; class GFG{     // This function returns true if k'th bit in x    // is set (or 1). For example if x (0010) is 2    // and k is 2, then it returns true    static int isKthBitSet(long x, long k)    {        int rslt = ((x & (1 << (int)(k - 1))) != 0) ? 1 : 0;        return rslt;    }         // This function returns true if binary    // representation of x is palindrome.    // For example (1000...001) is palindrome    static int isPalindrome( long x)    {        long l = 1; // Initialize left position        long r = 4 * 8; // initialize right position             // One by one compare bits        while (l < r)        {            if (isKthBitSet(x, l) != isKthBitSet(x, r))            {                return 0;            }            l++; r--;        }        return 1;    }         // Driver Code    public static void Main ()    {        long x = 1 << 15 + 1 << 16 ;        Console.WriteLine(isPalindrome(x));                 x = (1 << 31) + 1 ;        Console.WriteLine(isPalindrome(x));    }} // This code is contributed by AnkitRai01
<?php// PHP Program to Check if binary representation// of a number is palindrome // This function returns true if k'th bit in x// is set (or 1). For example if x (0010) is 2// and k is 2, then it returns truefunction isKthBitSet($x, $k){    return ($x & (1 << ($k - 1))) ? true : false;} // This function returns true if binary// representation of x is palindrome.// For example (1000...001) is palindromefunction isPalindrome($x){    $l = 1; // Initialize left position    $r = sizeof(4) * 8; // initialize right position     // One by one compare bits    while ($l < $r)    {        if (isKthBitSet($x, $l) != isKthBitSet($x, $r))            return false;        $l++;        $r--;    }    return true;} // Driver Code$x = 1 << 15 + 1 << 16;echo isPalindrome($x), "\n";$x = 1 << 31 + 1;echo isPalindrome($x), "\n"; // This code is contributed by ajit.?>
<script> // Javascript program to Check if binary// representation of a number is palindrome // This function returns true if k'th bit in x// is set (or 1). For example if x (0010) is 2// and k is 2, then it returns truefunction isKthBitSet(x, k){    let rslt = ((x & (1 << (k - 1))) != 0) ? 1 : 0;    return rslt;}   // This function returns true if binary// representation of x is palindrome.// For example (1000...001) is palindromefunction isPalindrome(x){         // Initialize left position    let l = 1;         // initialize right position    let r = 4 * 8;       // One by one compare bits    while (l < r)    {        if (isKthBitSet(x, l) !=            isKthBitSet(x, r))        {            return 0;        }        l++; r--;    }    return 1;} // Driver codelet x = 1 << 15 + 1 << 16;document.write(isPalindrome(x) + "</br>"); x = (1 << 31) + 1;document.write(isPalindrome(x)); // This code is contributed by divyesh072019 </script>
Output: 
1
1
Time Complexity: O(x)
Auxiliary Space: O(1)
This article is contributed by Saurabh Gupta. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
jit_t
SURENDRA_GANGWAR
ankthon
divyesh072019
simranarora5sos
subhammahato348
Amazon
binary-representation
Microsoft
Bit Magic
Mathematical
Amazon
Microsoft
Mathematical
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Little and Big Endian Mystery
Cyclic Redundancy Check and Modulo-2 Division
Binary representation of a given number
Add two numbers without using arithmetic operators
Josephus problem | Set 1 (A O(n) Solution)
Program for Fibonacci numbers
Write a program to print all permutations of a given string
C++ Data Types
Set in C++ Standard Template Library (STL)
Coin Change | DP-7