Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# program to check if binary representation is palindrome
A binary palindrome is a number whose binary representation reads the same forwards and backwards. To check if a number's binary representation is a palindrome, we need to reverse its binary form and compare it with the original.
For example, the number 5 has binary representation 101, which reads the same forwards and backwards, making it a binary palindrome.
How It Works
The algorithm uses bitwise operations to reverse the binary representation −
-
Left shift (<<=) moves bits to the left, effectively multiplying by 2
-
Right shift (>>=) moves bits to the right, effectively dividing by 2
-
AND operation (&) checks if the least significant bit is 1
-
XOR operation (^=) toggles the least significant bit
Syntax
The function to reverse binary bits −
public static long funcReverse(long num) {
long myRev = 0;
while (num > 0) {
myRev >= 1; // Right shift (divide by 2)
}
return myRev;
}
The function to check palindrome −
public static bool checkPalindrome(long num) {
long myRev = funcReverse(num);
return (num == myRev);
}
Example
The following example demonstrates checking if binary representations are palindromes −
using System;
public class Demo {
public static long funcReverse(long num) {
long myRev = 0;
while (num > 0) {
myRev >= 1;
}
return myRev;
}
public static bool checkPalindrome(long num) {
long myRev = funcReverse(num);
return (num == myRev);
}
public static void Main() {
// Test multiple numbers
long[] testNumbers = {5, 9, 17, 21};
foreach (long num in testNumbers) {
string binary = Convert.ToString(num, 2);
Console.WriteLine($"Number: {num}, Binary: {binary}");
if (checkPalindrome(num))
Console.WriteLine("Result: Palindrome");
else
Console.WriteLine("Result: Not a Palindrome");
Console.WriteLine();
}
}
}
The output of the above code is −
Number: 5, Binary: 101 Result: Palindrome Number: 9, Binary: 1001 Result: Palindrome Number: 17, Binary: 10001 Result: Palindrome Number: 21, Binary: 10101 Result: Palindrome
Using Built-in Methods
Alternative approach using string manipulation −
using System;
public class BinaryPalindrome {
public static bool IsBinaryPalindrome(int num) {
string binary = Convert.ToString(num, 2);
char[] chars = binary.ToCharArray();
Array.Reverse(chars);
string reversed = new string(chars);
return binary == reversed;
}
public static void Main() {
int[] numbers = {5, 7, 9, 15};
foreach (int num in numbers) {
string binary = Convert.ToString(num, 2);
bool isPalindrome = IsBinaryPalindrome(num);
Console.WriteLine($"{num} (binary: {binary}) - {(isPalindrome ? "Palindrome" : "Not Palindrome")}");
}
}
}
The output of the above code is −
5 (binary: 101) - Palindrome 7 (binary: 111) - Palindrome 9 (binary: 1001) - Palindrome 15 (binary: 1111) - Palindrome
Comparison
| Method | Advantages | Disadvantages |
|---|---|---|
| Bitwise Operations | Memory efficient, faster execution | More complex to understand |
| String Manipulation | Simple and readable code | Uses more memory for string operations |
Conclusion
Checking if a binary representation is a palindrome can be achieved using bitwise operations for efficiency or string manipulation for simplicity. The bitwise approach reverses bits mathematically, while the string method converts to binary string and compares with its reverse.
