![]() |
VOOZH | about |
Given a number n, the task is to check whether its binary representation is a palindrome or not. A binary palindrome means that the binary digits of a number read the same backward as forward.
For Example:
Input: n = 9 -> Binary: 1001 -> Output: True
Input: n = 10 -> Binary: 1010 -> Output: False
Let’s explore different ways to check this in Python.
This method checks palindrome property purely with bit manipulation. It extracts bits from both ends using shifting and masking. Avoids any string operation.
True
Explanation:
This method converts the number into its binary form, removes the '0b' prefix, and checks if the binary string is the same when reversed. It’s short, efficient, and uses Python’s built-in slicing.
True
Explanation:
Instead of reversing the string, this method compares characters from both ends moving toward the center. It avoids creating an extra reversed copy, making it slightly more memory-efficient.
True
Explanation:
This method builds the reverse binary manually using bit manipulation and then compares it to the original. It’s slower than the direct check but useful for understanding binary manipulation.
True
Explanation: