Skip to content

Commit b38fc04

Browse files
committed
Check Binary Representation is Palindrome
1 parent d9f4148 commit b38fc04

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,7 @@ This is a repository containing various C++ Programs to understand the basic con
138138

139139
C++ Code for removing duplicate elements present in a sorted array.
140140

141+
* [Check Binary Representation of a number is Palidrome or not](https://github.com/altruistcoder/Data-Structures/blob/master/binary_palindrome.cpp):
142+
143+
C++ Code for checking whether binary representation of a number is palindrome or not.
144+

binary_palindrome.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
bool binaryPalindrome(int num)
5+
{
6+
long n1=num, rev=0;
7+
while(n1>0)
8+
{
9+
rev <<= 1;
10+
if((n1&1)==1)
11+
rev ^= 1;
12+
n1 >>= 1;
13+
}
14+
return num==rev;
15+
}
16+
17+
int main()
18+
{
19+
long n;
20+
cout<<"Enter a number: ";
21+
cin>>n;
22+
if(binaryPalindrome(n))
23+
cout<<"The given number's binary representation is palindrome";
24+
else
25+
cout<<"The given number's binary representation is not palindrome";
26+
}

0 commit comments

Comments
 (0)