Check if binary representation of a number is palindrome or not
Check if the binary representation of a positive number is palindrome or not.
For example, 101, 11, 11011, 1001001
are palindromes. 100, 110, 1011
, etc., are not palindromes.
The idea is simple – construct a reverse of the binary representation of n
and return true if it is the same as n
. The implementation can be seen below in C++, Java, and Python:
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
#include <iostream> using namespace std; // Returns true if the binary representation of `n` is a palindrome bool isPalindrome(unsigned n) { // `rev` stores reverse of a binary representation of `n` unsigned rev = 0; // do till all bits of `n` are processed unsigned k = n; while (k) { // add the rightmost bit to `rev` rev = (rev << 1) | (k & 1); k = k >> 1; // drop last bit } // Returns true if `reverse(n)` is the same as `n` return n == rev; } int main() { int n = 9; // 1001 if (isPalindrome(n)) { cout << n << " is a palindrome"; } else { cout << n << " is not a palindrome"; } return 0; } |
Output:
9 is a palindrome
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
class Main { // Returns true if the binary representation of `n` is a palindrome public static boolean isPalindrome(int n) { // `rev` stores reverse of a binary representation of `n` int rev = 0; // do till all bits of `n` are processed int k = n; while (k > 0) { // add the rightmost bit to `rev` rev = (rev << 1) | (k & 1); k = k >> 1; // drop last bit } // Returns true if `reverse(n)` is the same as `n` return n == rev; } public static void main(String[] args) { int n = 9; // 1001 if (isPalindrome(n)) { System.out.println(n + " is a palindrome"); } else { System.out.println(n + " is not a palindrome"); } } } |
Output:
9 is a palindrome
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# Returns true if the binary representation of `n` is a palindrome def isPalindrome(n): # `rev` stores reverse of a binary representation of `n` rev = 0 # do till all bits of `n` are processed k = n while k > 0: # add the rightmost bit to `rev` rev = (rev << 1) | (k & 1) k = k >> 1 # drop last bit # Returns true if `reverse(n)` is the same as `n` return n == rev if __name__ == '__main__': n = 9 # 1001 if isPalindrome(n): print(f'{n} is a palindrome') else: print(f'{n} is not a palindrome') |
Output:
9 is a palindrome
Also See:
Bit Hacks – Part 1 (Basic)
Bit Hacks – Part 2 (Playing with k’th bit)
Bit Hacks – Part 3 (Playing with the rightmost set bit of a number)
Bit Hacks – Part 4 (Playing with letters of the English alphabet)
Bit Hacks – Part 5 (Find the absolute value of an integer without branching)
Suggested Read:
https://graphics.stanford.edu/~seander/bithacks.html
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)