Binary representation of very large decimal numbers

using System;

using System.Collections.Generic;

using System.Text;

using System.Text.RegularExpressions;

using System.Collections;

 

namespace ConsoleApplication1

{

    class Program

    {

        static void Main(string[] args)

        {

            string decimalNumber = "9632337040368467967";

            Console.WriteLine("Binary representation of " + decimalNumber + ":");

            ArrayList binary = new ArrayList();

            int remainder = 0;

            do

            {

                decimalNumber = Quotient(decimalNumber, out remainder);

                binary.Add(remainder);

            } while (!Check(decimalNumber));

           

            for (int i = binary.Count – 1; i >= 0; i–)

            {

                Console.Write(binary[i].ToString());

            }

            Console.WriteLine();

        }

        public static string Quotient(string num, out int remainder)

        {

            int tempNum = 0;

            remainder = 0;

            string quo = "";

            for (int i = 0; i < num.Length; i++)

            {

                tempNum = (remainder * 10) + int.Parse(num[i].ToString());

                quo = quo + (tempNum / 2);

                remainder = tempNum % 2;

            }

            return quo;

        }

        public static bool Check(string str)

        {

            for (int i = 0; i < str.Length; i++)

            {

                if (int.Parse(str[i].ToString()) != 0)

                {

                    return false;

                }

            }

            return true;

        }

    }

}

Binary representation of 9632337040368467967:
1000010110101100111011111000000011111111111111111111111111111111
Press any key to continue . . .

Advertisements
This entry was posted in Information Technology. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s