diff --git a/README.md b/README.md index a63abf0..ef940cf 100644 --- a/README.md +++ b/README.md @@ -156,3 +156,7 @@ This is a repository containing various python programs to understand the basic * [Remove Duplicates from a Sorted Array](https://github.com/altruistcoder/Data-Structures-Python/blob/master/remove_duplicates_sorted_array.py): Python Code for removing duplicate elements present in a sorted array. + +* [Check Binary Representation of a number is Palidrome or not](https://github.com/altruistcoder/Data-Structures-Python/blob/master/binary_palindrome.py): + + Python Code for checking whether binary representation of a number is palindrome or not. diff --git a/binary_palindrome.py b/binary_palindrome.py new file mode 100644 index 0000000..329bd37 --- /dev/null +++ b/binary_palindrome.py @@ -0,0 +1,15 @@ +def binaryPalindrome(num): + rev = 0 + n1 = num + while n1 > 0: + rev <<= 1 + if (n1 & 1) == 1: + rev ^= 1 + n1 >>= 1 + return num == rev + +n = int(input("Enter a number: ")) +if binaryPalindrome(n): + print("The given number's binary representation is palindrome") +else: + print("The given number's binary representation is not palindrome")