Check If an Integer's Bit Representation Is a Palindrome | Code Samurai
Check If an Integer's Bit Representation Is a Palindrome
Question: how can you determine if a positive integer is a palindrome. For example, 5 is a palindrome because 5 = 101. Also, 9 is a palindrome because 9 = 1001.
Solution: an integer is palindrome when its bit representation is the same reading from left to right or from right to left. Thus, in order to know if it is a palindrome or not, we just have to check if the number's value is still the same when we read the number's bits backward (right to left). For example, 5 is 101 and when we read its bits from right to left, which is 101, the result is still 5. However, 4 is 100 and when read backward it becomes 001 which is 1, so 4 is not a palindrome.
Pseudocode: here is the pseudocode for the algorithm:
Read full article from Check If an Integer's Bit Representation Is a Palindrome | Code Samurai
No comments:
Post a Comment