[C# Solution] Integer to Binary Representation

tom_mai78101

The Helper Connoisseur / Ex-MineCraft Host
Staff member
Reaction score
1,678
Here's the (alpha version) code. It's late at night, so bear with me. Will edit post.

PHP:
using System;

namespace Practice
{
    public class Lesson_2
    {
        public static void Main()
        {
            /*
             * Integer to Binary Representation 
             *
             * TODO: 
             * 1. Will try taking Binary to Integer 
             * Representation challenge.
             * 2. Will cleanup the code. It gives error when
             * no input is made before pressing Enter.
             * 
             * Commented out the goto statement, unless you 
             * want to test how far this program can go.
             * 
             * Finished this program in the middle of the 
             * night. I have school tomorrow.
             * 
             */

            ulong number;

            //retry:

            Console.Write("Enter a value: ");
            number = UInt64.Parse(Console.ReadLine());
            Binary(number);
            Console.WriteLine();

            //goto retry;
        }

        private static void Binary(ulong number)
        {
            ulong remainder;
            remainder = number % 2;

            if (number >= 2)
                Binary(number / 2);

            Console.Write("{0}", remainder);
        }
    }
}

Forgot to add a picture.

 

tom_mai78101

The Helper Connoisseur / Ex-MineCraft Host
Staff member
Reaction score
1,678
Nope, it's in the correct order.

Next time, I'll post pictures along with C# solutions...
 

Tru_Power22

You can change this now in User CP.
Reaction score
144
BAD! BAD PROGRAMMER!

Never ever, ever use goto. They are unsafe and make you program hard as hell to debug.

goto.png
 

Samuraid

Advisor
Reaction score
81
It would likely run a lot faster to use bitwise operations.

DISCLAIMER: This was just a quick code job, it could be optimized and cleaned up a little bit.
Code:
        private static void Binary(ulong number)
        {
            StringBuilder sb = new StringBuilder();
            string result;

            // Move through the number bit-by-bit.
            while (number != 0)
            {
                sb.Append(number & 0x0001);
                number >>= 1;
            }

            // If the number was zero to being with...
            if (sb.Length == 0)
            {
                sb.Append("0");
            }

            result = sb.ToString();

            // Write out the bits in reversed order (which is actually the correct order).
            for (int i = result.Length - 1; i >= 0; i--)
            {
                Console.Write(result[i]);
            }
        }
 

tom_mai78101

The Helper Connoisseur / Ex-MineCraft Host
Staff member
Reaction score
1,678
Never ever, ever use goto. They are unsafe and make you program hard as hell to debug.

Unless the program is too large to maintain within 15 minutes, using goto is fine. And my program is not like over 500 lines of code...I wouldn't use goto if vice versa.

And it's legal. Even if you don't recommend using goto, I still don't follow the books. ;)

It would likely run a lot faster to use bitwise operations.

Sorry, we haven't learned bitwise operators yet.

EDIT: I meant "any" of those yet.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top