VOOZH about

URL: https://www.geeksforgeeks.org/dsa/check-binary-representation-number-palindrome/

⇱ Check if binary representation is palindrome - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Check if binary representation is palindrome

Last Updated : 31 Mar, 2026

Given an integer x, determine whether its binary representation is a palindrome. Return true if it is a palindrome; otherwise, return false.

Input:x = 9
Output:true
Explanation: The binary representation of 9 is 1001, which is a palindrome.

Input:x = 10
Output:false
Explanation: The binary representation of 10 is 1010, which is not a palindrome.

Compare Bits from Both Ends

Compare bits from the leftmost and rightmost positions. If all corresponding bits match, the binary representation is a palindrome.

  • Find the number of bits in x.
  • Set l = 0 (leftmost) and r = n - 1 (rightmost).
  • While l < r, compare bits at positions l and r.
  • If they differ, return false; otherwise move inward.
  • If no mismatch is found, return true.

Output
true
false

Time Complexity: O(log x), as the loop runs once for each bit in x.
Space Complexity: O(1).

Using reverse() Function

Convert the integer to its binary string form and check if it is equal to its reverse using the reverse() function.

  • Convert x to a binary string (without leading zeros).
  • Store the binary string.
  • Reverse it using reverse().
  • Compare original and reversed strings.
  • If equal, return true; otherwise, return false.

Output
true
false

Time Complexity: O(log x), as operations are performed on all bits of x.
Space Complexity: O(log x), due to storing the binary string and its reverse.

Using Bitwise Reverse

Reverse the binary representation using bitwise operations and compare it with the original number.

  • Create a copy of the input and initialize rev = 0.
  • Extract bits one by one and build the reversed number using OR.
  • Compare the reversed number with the original.
  • If equal, return true; otherwise, return false.

Output
true
false

Time Complexity: O(log x)
Space Complexity: O(1)

Comment