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 ");
else
Console.Write("0 ");
}
Console.Write("\n");
}
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?