5 Best Ways to Check if Binary Representation of a Number is Palindrome in Python

Rate this post

πŸ’‘ Problem Formulation: We need a robust solution to determine if a number, when represented in binary, forms a palindrome. A binary palindrome is a sequence of binary digits (0s and 1s) that reads the same backward as forward. For example, the binary representation of the decimal number 9 is 1001, which is a palindrome.

Method 1: Using In-Built Python Functions

This method takes advantage of Python’s in-built functions to convert a number into its binary form, strip the leading ‘0b’ that Python adds for binary literals, and then check if the string is a palindrome. The simplicity of this method is its major advantage.

Here’s an example:

def is_binary_palindrome(num):
    binary_repr = bin(num)[2:]
    return binary_repr == binary_repr[::-1]


Output: True

This code snippet defines a function is_binary_palindrome that converts a number to its binary representation using bin(), strips the ‘0b’ using slicing, and checks if the string equals its reverse using slicing with step -1.

Method 2: Manual String Reversal

Instead of using slicing to reverse the string, this method manually constructs the reverse by iterating over the characters. This approach offers a clearer demonstration of the reversal process but is more verbose and potentially slower.

Here’s an example:

def is_binary_palindrome_manual(num):
    binary_repr = bin(num)[2:]
    reversed_binary = ''.join(reversed(binary_repr))
    return binary_repr == reversed_binary


Output: True

The function is_binary_palindrome_manual uses the reversed() function and join() to create a reversed version of the binary string, then compares it to the original binary string.

Method 3: Pointer Comparison

By using two pointers, you can compare characters at opposite ends of the string until they meet in the middle, avoiding the need to create an extra reversed string. This can be more efficient in terms of space.

Here’s an example:

def is_binary_palindrome_pointers(num):
    binary_repr = bin(num)[2:]
    left, right = 0, len(binary_repr) - 1
    while left <= right:
        if binary_repr[left] != binary_repr[right]:
            return False
        left, right = left + 1, right - 1
    return True


Output: True

The function is_binary_palindrome_pointers checks for palindrome property using two pointers from each end, moving towards the center until they meet or a mismatch is found.

Method 4: Bitwise Operation

This sophisticated method operates directly on the bitwise representation using bitwise operations. It is likely the most efficient and demonstrates a good understanding of both python and binary arithmetic.

Here’s an example:

def is_binary_palindrome_bitwise(num):
    original_num = num
    reversed_num = 0
    while num > 0:
        reversed_num = (reversed_num <>= 1
    return original_num == reversed_num


Output: True

This function is_binary_palindrome_bitwise applies bitwise operations to reverse the bits of the number and then checks if the reversed bit sequence is equal to the original number.

Bonus One-Liner Method 5: Using fmt and f-strings

Python 3.6 introduced f-strings for string formatting; a one-liner approach can be crafted combining f-strings with the slicing method to create a terse yet clear solution.

Here’s an example:

is_binary_palindrome_oneliner = lambda num: f'{num:b}' == f'{num:b}'[::-1]


Output: True

This one-liner uses a lambda function to convert the number to a binary string using f-strings and immediately checks for the palindrome property by reversing the string.


  • Method 1: Using In-Built Python Functions. Quick and easy, with minimal code. It might not be the fastest for very large numbers.
  • Method 2: Manual String Reversal. More explicit reversal process, easier for beginners to understand. Slower and more verbose.
  • Method 3: Pointer Comparison. Space-efficient since it avoids creating a new string. A good balance between readability and efficiency.
  • Method 4: Bitwise Operation. Most efficient, operates at bit-level. Complexity might not be ideal for quick implementation or clear readability.
  • Method 5: Bonus One-Liner Method. Concise and uses modern Python features. Great for code golf, but perhaps not as readable for those unfamiliar with f-strings or lambda.