| | |
Display a number in binary
Please support our C# advertiser: $4.95 a Month - ASP.NET Web Hosting – Click Here!
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,158
Reputation:
Solved Threads: 136
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 2007 access algorithm array bitmap box c# camera check checkbox client combobox control conversion cryptographyc#winformsencryption cs4 csharp customactions database datagrid datagridview dataset datastructure date datetime degrees disabled displayingopenforms draganddrop drawing encryption enum eventcloseformc# excel file form format forms ftp function gdi+ gis image index index-error input install java label list listview load math mathematics mouseclick mysql operator path photoshop picturebox pixelinversion pixels post prime programming radians regex remoting richtextbox rotation search security server setup sleep socket sql statistics stream string table text textbox thread time timer totaldays update user usercontrol validation view visual visualstudio webbrowser winforms wordautomation wpf xml






