Display a number in binary

Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Oct 2008
Posts: 1,923
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 276
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Display a number in binary

 
0
  #1
Nov 10th, 2008
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 :
  1. static void Displaybinary( uint i)
  2. {
  3. const byte cByteSize = 8; //like the use of constants that seldom change
  4.  
  5. int maxbit = sizeof(uint) * cByteSize - 1;
  6.  
  7. uint d = Convert.ToUInt32(Math.Pow(2, maxbit)); //here d=2^31
  8.  
  9. for (uint size = d; size > 0; size = size / 2)
  10. {
  11. if (Convert.ToBoolean(i & size)) //C# is very picky here
  12. Console.Write("1 ");
  13. else
  14. Console.Write("0 ");
  15. }
  16. Console.Write("\n");
  17. }
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?
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,160
Reputation: dickersonka will become famous soon enough dickersonka will become famous soon enough 
Solved Threads: 137
dickersonka dickersonka is offline Offline
Veteran Poster

Re: Display a number in binary

 
1
  #2
Nov 10th, 2008
how about

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

little less lines of code
Custom Application & Software Development
www.houseshark.net
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,923
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 276
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: Display a number in binary

 
0
  #3
Nov 10th, 2008
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!
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC