- Trending Categories
- Data Structure
- Operating System
- C Programming
- 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 −
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))
- 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 a number is Palindrome in C++
- Check if a number is Palindrome in PL/SQLs
- Palindrome in Python: How to check a number is palindrome?
- Check if the binary representation of a number has equal number of 0s and 1s in blocks in Python
- Python program to check if a given string is number Palindrome
- Check if number is palindrome or not in Octal in Python
- Python program to check if binary representation of two numbers are anagram.
- Bash program to check if the Number is a Palindrome?
- Check if Decimal representation of an Octal number is divisible by 7 in Python
- Binary representation of a given number in C++
- Check if any anagram of a string is palindrome or not in Python