| | |
Display a number in binary
Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved |
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 :
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?
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 :
C# Syntax (Toggle Plain Text)
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"); }
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
Make love, no war. Cave ab homine unius libri.
Danny
•
•
Join Date: Aug 2008
Posts: 1,160
Reputation:
Solved Threads: 137
how about
little less lines of code
C# Syntax (Toggle Plain Text)
int myValue = 6000; Debug.WriteLine(Convert.ToString (myValue, 2));
little less lines of code
Custom Application & Software Development
www.houseshark.net
www.houseshark.net
![]() |
Similar Threads
- decimal to binary in while loop..stuck (C++)
- display the number of capitals letters in the sentence (Assembly)
- How do I do this??? (Java)
- Complex Number Calculator in C++ (C++)
- Can someone please help me with Binary Addtion. (C++)
- binary tree class (C++)
- binary search (C++)
Other Threads in the C# Forum
- Previous Thread: Share folder & files and setting permission on C# over the internet
- Next Thread: Keep main form open and visible
| Thread Tools | Search this Thread |
.net access algorithm array barchart bitmap box broadcast c# check checkbox client combobox control conversion csharp custom cyclethruopenforms data database datagrid datagridview dataset date/time datetime degrees development dll draganddrop drawing encryption enum event excel file finalyearproject form format forms function gdi+ getoutlookcontactusinfcsvfile globalization httpwebrequest image index input install installer java label list listbox mandelbrot math mono mouseclick mysql operator panel path photoshop picturebox pixelinversion post programming radians regex remote remoting richtextbox save server silverlight sleep socket sql sql-server statistics stream string table text textbox thread time timer timespan update usercontrol users validate validation visualstudio webbrowser wia windows winforms wpf xml






