Skip to content

Commit

Permalink
Check Binary Representation is Palindrome
Browse files Browse the repository at this point in the history
  • Loading branch information
altruistcoder committed Aug 15, 2019
1 parent 303316b commit 75d3387
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
15 changes: 15 additions & 0 deletions binary_palindrome.py
Original file line number Diff line number Diff line change
@@ -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")

0 comments on commit 75d3387

Please sign in to comment.