Write a C# Sharp program to reverse the binary representation of an given integer and convert the reversed binary number into an integer

  • برمجة سي شارب
  • برمجة

 Write a C# Sharp program to reverse the binary representation of an given integer and convert the reversed binary number into an integer. 
Expected Output:
Original number: 120
Reverse the binary representation of the said integer and convert it into an integer: 15
Original number: 321
Reverse the binary representation of the said integer and convert it into an integer: 261
Original number: 43
Reverse the binary representation of the said integer and convert it into an integer: 53
Original number: 4433
Reverse the binary representation of the said integer and convert it into an integer: 4433

الأجوبة

using System;
using System.Linq;
namespace exercises
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = 120;
            Console.WriteLine("Original number: " + n);
            Console.WriteLine("Reverse the binary representation of the said integer and convert it into an integer: " + test(n));
            n = 321;
            Console.WriteLine("\nOriginal number: " + n);
            Console.WriteLine("Reverse the binary representation of the said integer and convert it into an integer: " + test(n));
            n = 43;
            Console.WriteLine("\nOriginal number: " + n);
            Console.WriteLine("Reverse the binary representation of the said integer and convert it into an integer: " + test(n));
            n = 4433;
            Console.WriteLine("\nOriginal number: " + n);
            Console.WriteLine("Reverse the binary representation of the said integer and convert it into an integer: " + test(n));
        }
        public static int test(int n)
        {
            return (Convert.ToInt32(new string(Convert.ToString(n, 2).Reverse().ToArray()), 2));
        }
    }
}
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...