I thought it was a simple task at first.
Don't ask me why, but I wanted to display a number in it's binary form.
Manipulating bits in C# seems a bit(no pun...) harsh.
The best I could come up with was :

static void Displaybinary( uint i) 
            const byte cByteSize = 8; //like the use of constants that seldom change

            int maxbit = sizeof(uint) * cByteSize - 1;

            uint d = Convert.ToUInt32(Math.Pow(2, maxbit)); //here d=2^31

            for (uint size = d; size > 0; size = size / 2)
                if (Convert.ToBoolean(i & size))  //C# is very picky here
                    Console.Write("1 ");
                    Console.Write("0 ");

I tried to make it generic, but that's not that much of a problem,
beside the feeling I'm reenventing the wheel here, I'm not feeling so happy with the code.
Can anyone make any better suggestions?

Recommended Answers

All 2 Replies

how about

int myValue = 6000;
Debug.WriteLine(Convert.ToString (myValue, 2));

little less lines of code

commented: nice! +2

I knew I was somewhere on a wrong path.
The obvious is often just so close, you deny to see it. Thanks for putting me on track!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.