1

I am trying to create a console application where the user inputs a base 10 number and the program outputs the number in binary.

I want to get the exact number of times to execute the for loop to get correct value (the value HERE in the code below).

I also have to reverse everything that the console is now outputting for everything to be correct but that I can do on my own later.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Decimal_to_Binary
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Please enter a number and the computer will convert it into binary.");
            string NumberIn = Console.ReadLine();
            decimal Number = Decimal.Parse(NumberIn);

            decimal[] Squared = new decimal[100];

            for (int i = 0; i < HERE + 1; i++)
            {
                //Squared[i] = Math.Pow(2, i);
                Squared[i] = 2 ^ i ; 

                if (Number == Squared[i])
                {
                    Console.Write("1");
                }
                else
                {
                    Console.Write("0");
                }
            }

            Console.ReadKey();
        }
    }
}
Frank Boyne
  • 4,400
  • 23
  • 30
  • 4
    First point: `2 ^ i` doesn't do what you think it does. Second point: this approach will only handle *exact* powers of 2. Try thinking about it a different way - build up a string with the bits in *backwards* by repeatedly testing whether the number is odd or not, and dividing by 2. When you've got to 0, you're done - and can reverse the string and print it out. – Jon Skeet Sep 10 '15 at 20:55
  • http://stackoverflow.com/questions/1838963/easy-and-fast-way-to-convert-an-int-to-binary – MethodMan Sep 10 '15 at 20:55
  • to convert to binary use the module function start with 12. Divide by 2 and get remained : 6 R0. Repeat 3 R00. Repeat 1 R100. And Again 0 R1100. 12 binary is 1100. – jdweng Sep 10 '15 at 21:15

1 Answers1

0

A nice feature of logarithms is that they'll tell you the highest order bit (in the case of log2) or digit that is set for any number. You can use log2 to find out the highest order bit set and then shift the number repeatedly checking for the 0th bit and print the binary representation.

using System;
using System.Text;

namespace BinaryCount
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Please enter a number and the computer will convert it into binary.");
            string NumberIn = Console.ReadLine();
            int Number = Int32.Parse(NumberIn);

            double loopCount = Math.Log(Number, 2); // Log2 of a number will tell you the highest order bit that's set
            StringBuilder s = new StringBuilder();
            for (int i = 0 ; i <  loopCount ; ++i)
            {
                if (Number % 2 == 1)
                    s.Append("1");
                else
                    s.Append("0");
                Number = Number >> 1;
            }
            StringBuilder done = new StringBuilder();
            for (int i = s.Length - 1 ; i >= 0 ; i--)
            {
                done.Append(s[i]);
            }
            Console.WriteLine(done);
            Console.ReadLine();
        }
    }
}
P. Hinker
  • 299
  • 2
  • 7
  • I did tried this solution but when I entered 4 it gave me 00 back which should be 100. The same thing happened with 8 where it gave me 000 back where it should be 1000. Any ideas why that might be happening? – Zacharias Veiksaar Sep 11 '15 at 09:43
  • @ZachariasVeiksaar : You're correct, when the input is an exact power of two the last bit isn't being displayed. The better loop construct would probably be : while (Number != 0) and dispense with loopCount all together. – P. Hinker Sep 11 '15 at 17:47