Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,894 questions

51,825 answers

573 users

How to check if the binary representation of a number is a palindrome in C#

3 Answers

0 votes
using System;

class Program
{
    public static int reverseBits(int n) {
        int reversed = 0;

        int temp = n;
        while (temp > 0) {
            reversed = (reversed << 1) | (temp & 1);
            temp = temp >> 1;
        }
        return reversed;
    }
    public static bool isPalindrome(int n) {
        return n == reverseBits(n);
    }
    static void Main() {
        int n = 27;
        
        Console.WriteLine(Convert.ToString(n, 2));
        
        Console.WriteLine((isPalindrome(n) ? "Yes" : "No"));
    }
}





/*
run:

11011
Yes

*/

 



answered Jul 13, 2022 by avibootz
0 votes
using System;
using System.Linq;
 
class Program
{
    public static bool isPalindrome(int n) {
        return Convert.ToString(n, 2) == String.Join("", Convert.ToString(n, 2).Reverse());
    }
    static void Main() {
        int n = 27;
         
        Console.WriteLine(Convert.ToString(n, 2));
         
        Console.WriteLine((isPalindrome(n) ? "Yes" : "No"));
    }
}
 
 
 
 
 
/*
run:
 
11011
Yes
 
*/

 



answered Jul 14, 2022 by avibootz
0 votes
using System;
using System.Linq;
 
public class MyClass
{
    private static bool is_binary_representation_of_number_palindrome(int num)  {
        string binary = Convert.ToString(num, 2);
 
        Console.WriteLine(binary);
 
        return binary.Equals(String.Join("", binary.Reverse()));
    }
 
    public static void Main(string[] args)
    {
        int num = 153; // 10011001
 
        if (is_binary_representation_of_number_palindrome(num)) {
            Console.Write("Palindrome");
        }
        else {
            Console.Write("Not Palindrome");
        }
    }
}
 
 
 
 
 
/*
run:
      
10011001
Palindrome
      
*/

 



answered Jan 8, 2024 by avibootz
...