How do you know if a binary number is palindrome?

How do you know if a binary number is palindrome?

Algorithm:

  1. Find number of bits in x using sizeof() operator.
  2. Initialize left and right positions as 1 and n respectively.
  3. Do following while left ‘l’ is smaller than right ‘r’. ..…..
  4. If we reach here, it means we didn’t find a mismatching bit.

How do you reverse a bit in an integer?

  1. # Function to reverse bits of a given integer.
  2. def reverseBits(n):
  3. pos = SIZE – 1 # maintains shift.
  4. # store reversed bits of `n`. Initially, all bits are set to 0.
  5. reverse = 0.
  6. # do till all bits are processed.
  7. while pos >= 0 and n:
  8. # if the current bit is 1, then set the corresponding bit in the result.

How do you find palindrome numbers?

Using the distributive property, any four digit palindrome can be written as x(1001) + y(110) where x is some integer between 1 and 9, inclusive, and y is some integer between 0 and 9, inclusive. For example, 6(1001) + 3(110) = 6006 + 330 = 6336 is a palindrome.

How to check if a binary number is a palindrome?

As we have learned how to convert a decimal number into a binary number in the above program and the binary representation of 90 is not a palindrome and this is our main task to check palindrome using Python. Now, we can easily solve it.

Is the number 10.01 a palindrome or a string?

For example a numbers with binary representation as 10..01 is palindrome and number with binary representation as 10..00 is not palindrome. The idea is similar to checking a string is palindrome or not.

How to check the binary representation of a given number?

In this problem, a number will be given by the user and we have to convert it into a binary number and after this, we will check the binary representation is a palindrome or not. Before going to do the given task, we will learn how to convert a number into a binary number.

How to find the bit at a given position?

To find the bit at a given position, we can use the idea similar to this post. The expression “ x & (1 << (k-1)) ” gives us non-zero value if bit at k’th position from right is set and gives a zero value if if k’th bit is not set. Following is the implementation of the above algorithm.