Check if binary representation of a number is palindrome in Python

Checking if a number's binary representation is a palindrome involves converting the number to binary and verifying if it reads the same forwards and backwards. Python provides several approaches to accomplish this task.

Understanding the Problem

A binary palindrome reads the same from left to right as from right to left. For example, the number 9 has binary representation "1001", which is a palindrome.

Method 1: Using String Conversion

The simplest approach is to convert the number to binary string and check if it's equal to its reverse −

def is_binary_palindrome_string(n):
    binary_str = bin(n)[2:]  # Remove '0b' prefix
    return binary_str == binary_str[::-1]

# Test with example
n = 9
print(f"Binary of {n}: {bin(n)[2:]}")
print(f"Is palindrome: {is_binary_palindrome_string(n)}")
Binary of 9: 1001
Is palindrome: True

Method 2: Using Binary Reversal

This method reverses the binary representation by manipulating bits directly −

def reverse_binary(num):
    ans = 0
    while num > 0:
        ans = ans << 1  # Left shift (multiply by 2)
        if num & 1 == 1:  # If last bit is 1
            ans = ans ^ 1  # Set last bit of ans to 1
        num = num >> 1  # Right shift (divide by 2)
    return ans

def is_binary_palindrome_bitwise(n):
    rev = reverse_binary(n)
    return n == rev

# Test with multiple examples
test_numbers = [9, 5, 17, 21]
for num in test_numbers:
    result = is_binary_palindrome_bitwise(num)
    print(f"Number: {num}, Binary: {bin(num)[2:]}, Palindrome: {result}")
Number: 9, Binary: 1001, Palindrome: True
Number: 5, Binary: 101, Palindrome: True
Number: 17, Binary: 10001, Palindrome: True
Number: 21, Binary: 10101, Palindrome: True

How the Binary Reversal Works

The bitwise method works by:

  • Starting with ans = 0
  • For each bit in the original number (from right to left):
  • Left−shift ans to make room for the next bit
  • If the current bit is 1, XOR ans with 1 to set the last bit
  • Right−shift the original number to process the next bit

Method 3: Using Two Pointers Approach

Convert to binary string and use two pointers to check palindrome −

def is_binary_palindrome_two_pointers(n):
    binary_str = bin(n)[2:]  # Remove '0b' prefix
    left, right = 0, len(binary_str) - 1
    
    while left < right:
        if binary_str[left] != binary_str[right]:
            return False
        left += 1
        right -= 1
    
    return True

# Test the function
n = 9
print(f"Number: {n}")
print(f"Binary: {bin(n)[2:]}")
print(f"Is palindrome: {is_binary_palindrome_two_pointers(n)}")
Number: 9
Binary: 1001
Is palindrome: True

Comparison

Method Time Complexity Space Complexity Readability
String Conversion O(log n) O(log n) High
Binary Reversal O(log n) O(1) Medium
Two Pointers O(log n) O(log n) High

Conclusion

The string conversion method is the most readable and straightforward approach. The binary reversal method is more memory−efficient but requires understanding of bitwise operations. Choose based on your specific requirements for readability versus memory efficiency.

Updated on: 2026-03-25T14:23:04+05:30

326 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements