- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check if binary representation of a number is palindrome in Python
Suppose we have a number n. We have to check whether the binary representation of n is palindrome or not.
So, if the input is like n = 9, then the output will be True as binary representation of 9 is 1001, which is palindrome.
To solve this, we will follow these steps −
- ans := 0
- while num > 0, do
- ans := ans * 2
- if num is odd, then
- ans := ans XOR 1
- num := num / 2
- return ans
Let us see the following implementation to get better understanding −
Example
def reverse_binary(num) : ans = 0 while (num > 0) : ans = ans << 1 if num & 1 == 1 : ans = ans ^ 1 num = num >> 1 return ans def solve(n) : rev = reverse_binary(n) return n == rev n = 9 print(solve(n))
Input
9
Output
True
- Related Articles
- Python program to check if binary representation is palindrome?
- C# program to check if binary representation is palindrome
- Java program to check if binary representation is palindrome
- Golang Program to check if the binary representation of a number is palindrome or not
- Check if the binary representation of a number has equal number of 0s and 1s in blocks in Python
- Palindrome in Python: How to check a number is palindrome?
- Check if a number is Palindrome in C++
- Python program to check if a given string is number Palindrome
- Check if number is palindrome or not in Octal in Python
- Check if a number is Palindrome in PL/SQLs
- Python program to check if binary representation of two numbers are anagram.
- Check if Decimal representation of an Octal number is divisible by 7 in Python
- Bash program to check if the Number is a Palindrome?
- Prime Number of Set Bits in Binary Representation in Python
- Binary representation of a given number in C++

Advertisements