Open In App

Check if actual binary representation of a number is palindrome

Last Updated : 01 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  1. Get the number obtained by reversing the bits in the binary representation of n. Refer this post. Let it be rev.
  2. If n == rev, then print “Yes” else “No”.

Implementation:

C++




// 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 number
unsigned 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 not
bool 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 above
int main()
{
    unsigned int n = 9;
    if (isPalindrome(n))
        cout << "Yes";
    else
        cout << "No";
    return 0;
}


Java




// Java code  to check whether 
// binary representation of a
// number is palindrome or not
import 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


Python3




# Python 3 implementation to check
# whether binary representation of
# a number is palindrome or not
 
# function to reverse bits of a number
def 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 not
def isPalindrome(n) :
 
    # get the number by reversing
    # bits in the binary
    # representation of 'n'
    rev = reverseBits(n)
 
    return (n == rev)
 
 
# Driver program to test above
n = 9
if (isPalindrome(n)) :
    print("Yes")
else :
    print("No")
     
# This code is contributed by Nikita Tiwari.


C#




// C# code to check whether
// binary representation of a
// number is palindrome or not
using 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
// PHP implementation to check
// whether binary representation
// of a number is palindrome or not
 
// function to reverse bits of a number
function 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 not
function 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
?>


Javascript




<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.
Auxiliary Space: O(1).



Previous Article
Next Article

Similar Reads

Check if binary representation of a number is palindrome
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
15 min read
Check if Binary representation is Palindrome in Python
Given an integer ‘n’, write a Python function that returns true if binary representation of x is palindrome else return false. Examples: Input : n = 9 Output : True Binary representation of n=9 is 1001 which is palindrome as well. Input : n = 10 Output : False Binary representation of n=10 is 1010 which is not palindrome. We have existing solution
4 min read
Find the n-th number whose binary representation is a palindrome
Find the nth number whose binary representation is a palindrome. Do not consider the leading zeros, while considering the binary representation. Consider the 1st number whose binary representation is a palindrome as 1, instead of 0  Examples:  Input : 1Output : 11st Number whose binary representation is palindrome is 1 (1)Input : 9Output : 279th Nu
39 min read
Reverse actual bits of the given number
Given a non-negative integer n. The problem is to reverse the bits of n and print the number obtained after reversing the bits. Note that the actual binary representation of the number is being considered for reversing the bits, no leadings 0's are being considered.Examples : Input : 11Output : 13Explanation: (11)10 = (1011)2.After reversing the bi
16 min read
Invert actual bits of a number
Given a non-negative integer n. The problem is to invert the bits of n and print the number obtained after inverting the bits. Note that the actual binary representation of the number is being considered for inverting the bits, no leading 0’s are being considered. Examples: Input : 11Output : 4(11)10 = (1011)[2]After inverting the bits, we get:(010
13 min read
Check if the binary representation of a number has equal number of 0s and 1s in blocks
Given an integer N, the task is to check if its equivalent binary number has an equal frequency of consecutive blocks of 0 and 1. Note that 0 and a number with all 1s are not considered to have a number of blocks of 0s and 1s. Examples: Input: N = 5 Output: Yes Equivalent binary number of 5 is 101. The first block is of 1 with length 1, the second
10 min read
Check if given number contains only “01” and “10” as substring in its binary representation
Given a number N, the task is to check if the binary representation of the number N has only "01" and "10" as a substring or not. If found to be true, then print "Yes". Otherwise, print "No".Examples: Input: N = 5 Output: Yes Explanation: (5)10 is (101)2 which contains only "01" and "10" as substring.Input: N = 11 Output: No Explanation: (11)10 is
6 min read
Check if binary representation of a given number and its complement are anagram
Given a positive number you need to check whether it's complement and the number are anagrams or not.Examples: Input : a = 4294967295 Output : Yes Binary representation of 'a' and it's complement are anagrams of each other Input : a = 4 Output : No Simple Approach: In this approach calculation of the complement of the number is allowed.1. Find bina
9 min read
Number of leading zeros in binary representation of a given number
Given a positive integer N, the task is to find the number of leading zeros in its binary representation.A leading zero is any 0 digit that comes before the first nonzero digit in a number's binary form. Examples: Input : N = 16Output : 27Explanation: As Binary(16) = (00000000000000000000000000010000) Input : N = 33Output : 26Explanation: As Binary
10 min read
Count number of trailing zeros in Binary representation of a number using Bitset
Given a number. The task is to count the number of Trailing Zero in Binary representation of a number using bitset. Examples: Input : N = 16Output : 4Binary representation of N is 10000. Therefore,number of zeroes at the end is 4.Input : N = 8Output : 3Recommended: Please try your approach on {IDE} first, before moving on to the solution.Approach:
5 min read
Article Tags :
Practice Tags :