Check if actual binary representation of a number is palindrome - GeeksforGeeks

08 Apr, 2021
Given a non-negative integer n. The problem is to check if binary representation of n is palindrome or not. Note that the actual binary representation of the number is being considered for palindrome checking, no leading 0’s are being considered.Examples :  
Input : 9
Output : Yes
(9)10 = (1001)2

Input : 10
Output : No
(10)10 = (1010)2
 
Approach: Following are the steps: 
Get the number obtained by reversing the bits in the binary representation of n. Refer this post. Let it be rev.If n == rev, then print “Yes” else “No”.
Get the number obtained by reversing the bits in the binary representation of n. Refer this post. Let it be rev.
If n == rev, then print “Yes” else “No”.
 
C++
Java
Python3
C#
PHP
Javascript
// C++ implementation to check whether binary// representation of a number is palindrome or not#include <bits/stdc++.h>using namespace std; // function to reverse bits of a numberunsigned int reverseBits(unsigned int n){    unsigned int rev = 0;     // traversing bits of 'n' from the right    while (n > 0) {         // bitwise left shift 'rev' by 1        rev <<= 1;         // if current bit is '1'        if (n & 1 == 1)            rev ^= 1;         // bitwise right shift 'n' by 1        n >>= 1;    }     // required number    return rev;} // function to check whether binary representation// of a number is palindrome or notbool isPalindrome(unsigned int n){    // get the number by reversing bits in the    // binary representation of 'n'    unsigned int rev = reverseBits(n);     return (n == rev);} // Driver program to test aboveint main(){    unsigned int n = 9;    if (isPalindrome(n))        cout << "Yes";    else        cout << "No";    return 0;}
// Java code  to check whether // binary representation of a// number is palindrome or notimport java.util.*;import java.lang.*; public class GfG{    // function to reverse bits of a number    public static long reverseBits(long n)    {        long rev = 0;         // traversing bits of 'n'        // from the right        while (n > 0)        {            // bitwise left shift 'rev' by 1            rev <<= 1;             // if current bit is '1'            if ((n & 1) == 1)                rev ^= 1;             // bitwise right shift 'n' by 1            n >>= 1;        }         // required number        return rev;    }     // function to check a number    // is palindrome or not    public static boolean isPalindrome(long n)    {        // get the number by reversing        // bits in the  binary        // representation of 'n'        long rev = reverseBits(n);         return (n == rev);    }         // Driver function    public static void main(String argc[])    {        long n = 9;        if (isPalindrome(n))            System.out.println("Yes");        else            System.out.println("No");    }     } // This code is contributed by Sagar Shukla
# Python 3 implementation to check# whether binary representation of# a number is palindrome or not # function to reverse bits of a numberdef reverseBits(n) :    rev = 0       # traversing bits of 'n' from the right  while (n > 0) :      # bitwise left shift 'rev' by 1     rev = rev << 1      # if current bit is '1'     if (n & 1 == 1) :     rev = rev ^ 1      # bitwise right shift 'n' by 1     n = n >> 1          # required number     return rev     # function to check whether binary# representation of a number is# palindrome or notdef isPalindrome(n) :     # get the number by reversing    # bits in the binary    # representation of 'n'    rev = reverseBits(n)     return (n == rev)  # Driver program to test aboven = 9if (isPalindrome(n)) :    print("Yes")else :    print("No")     # This code is contributed by Nikita Tiwari.
// C# code to check whether// binary representation of a// number is palindrome or notusing System; public class GfG{    // function to reverse bits of a number    public static long reverseBits(long n)    {        long rev = 0;         // traversing bits of 'n'        // from the right        while (n > 0)        {            // bitwise left shift 'rev' by 1            rev <<= 1;             // if current bit is '1'            if ((n & 1) == 1)                rev ^= 1;             // bitwise right shift 'n' by 1            n >>= 1;        }         // required number        return rev;    }     // function to check a number    // is palindrome or not    public static bool isPalindrome(long n)    {        // get the number by reversing        // bits in the binary        // representation of 'n'        long rev = reverseBits(n);         return (n == rev);    }         // Driver function    public static void Main()    {        long n = 9;        if (isPalindrome(n))            Console.WriteLine("Yes");        else            Console.WriteLine("No");    }     } // This code is contributed by vt_m
<?php// PHP implementation to check// whether binary representation// of a number is palindrome or not // function to reverse bits of a numberfunction reverseBits($n){    $rev = 0;     // traversing bits of 'n'    // from the right    while ($n > 0)    {         // bitwise left shift 'rev' by 1        $rev <<= 1;         // if current bit is '1'        if ($n & 1 == 1)            $rev ^= 1;         // bitwise right shift 'n' by 1        $n >>= 1;    }     // required number    return $rev;} // function to check whether// binary representation of a// number is palindrome or notfunction isPalindrome($n){    // get the number by reversing    // bits in the binary representation    // of 'n'    $rev = reverseBits($n);     return ($n == $rev);} // Driver code$n = 9;if (isPalindrome($n))    echo "Yes";else    echo "No";return 0; // This code is contributed by mits?>
<script> // JavaScript program  to check whether // binary representation of a// number is palindrome or not     // function to reverse bits of a number    function reverseBits(n)    {        let rev = 0;           // traversing bits of 'n'        // from the right        while (n > 0)        {            // bitwise left shift 'rev' by 1            rev <<= 1;               // if current bit is '1'            if ((n & 1) == 1)                rev ^= 1;               // bitwise right shift 'n' by 1            n >>= 1;        }           // required number        return rev;    }       // function to check a number    // is palindrome or not    function isPalindrome(n)    {        // get the number by reversing        // bits in the  binary        // representation of 'n'        let rev = reverseBits(n);           return (n == rev);    } // Driver code         let n = 9;        if (isPalindrome(n))            document.write("Yes");        else            document.write("No"); </script>
Output :  
Yes
Time Complexity: O(num), where num is the number of bits in the binary representation of n. 
Mithun Kumar
code_hunt
palindrome
Bit Magic
Strings
Strings
Bit Magic
palindrome
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Cyclic Redundancy Check and Modulo-2 Division
Little and Big Endian Mystery
Add two numbers without using arithmetic operators
Binary representation of a given number
Find the element that appears once
Reverse a string in Java
Write a program to reverse an array or string
Longest Common Subsequence | DP-4
Write a program to print all permutations of a given string
C++ Data Types