Binary formatting an integer

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

Join Date: Oct 2008
Posts: 1,995
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: 294
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Binary formatting an integer

 
0
  #1
Jul 18th, 2009
Hi all,
I like to write out integers in binary format to the console.
Let's say I want Myint=5 to be written I use Convert.ToString(Myint,2)
I get 101, what I want is 0000000000000101

I found this struct on the net which could do the trick, but is there a better way?
  1. struct IntBits
  2. {
  3. private int _bits;
  4.  
  5. public IntBits(int InitialBitValue)
  6. {
  7. _bits = InitialBitValue;
  8. }
  9.  
  10. //declare indexer
  11. public bool this[int index]
  12. {
  13. get
  14. {
  15. return (_bits & (1 << index)) != 0;
  16. }
  17. set
  18. {
  19. if (value)
  20. _bits |= (1 << index); //set bit index 1
  21. else
  22. _bits &= ~(1 << index); //set bit index 0
  23. }
  24. }
  25. }
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: Feb 2009
Posts: 3,328
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 601
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: Binary formatting an integer

 
0
  #2
Jul 18th, 2009
You could do it at the string level with .PadLeft() or are you wanting this done property with binary formatting?

  1. private void button2_Click(object sender, EventArgs e)
  2. {
  3. int myInt = 5;
  4. string res = Convert.ToString(myInt, 2).PadLeft(16, '0');
  5. System.Diagnostics.Debugger.Break();
  6. }
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,995
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: 294
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: Binary formatting an integer

 
0
  #3
Jul 18th, 2009
Thanks Scott, I was not aware of the existence of the PadLeft function
This will do for what I want(thanks again), but what do you mean by
or are you wanting this done property with binary formatting?
As you probably know English is not my native language.
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: Feb 2009
Posts: 3,328
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 601
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: Binary formatting an integer

 
0
  #4
Jul 18th, 2009
I was meaning if there is some way to use an IFormatProvider for binary to get the string padded with zeros. I don't know of a way but I didn't know if you weren't aware of .PadLeft() or if you didn't want to use it, so that is what I was trying to ask

It sounds like you're adding binary support to your calculator? When you're done I can replace my TI-85 with your program I think
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,995
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: 294
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: Binary formatting an integer

 
0
  #5
Jul 18th, 2009
No I was not planning on doing that.
Did you know there are some subtle differences between the MS calculator and the one I have at home? I am glad I finished it as it is
The reason I needed a binary format is I wanted to see what graphical pattern I would get when using prime numbers. So I would make colored squares depending if a bit is 0 or 1.
I first made a test program to get the zeros and ones, thanks to you I can continue!
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: Feb 2009
Posts: 3,328
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 601
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: Binary formatting an integer

 
0
  #6
Jul 19th, 2009
I was actually reading that thread you posted about the calculators being different earlier today but I didn't understand it and it was so old I didn't want to post on it. What differences have you noticed since then?
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,995
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: 294
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: Binary formatting an integer

 
0
  #7
Jul 19th, 2009
I think you are referring to this post http://www.daniweb.com/forums/thread187538.html
It was while I was testing my C# calculator I came up with this.
I tried out the most wierd keystroke sequences to let it somehow "misbehave". I did not come up with new ones, besides this is a keystroke sequence you normally would not use. Given the fact that I can not try out all the possible keystroke permutations and that my C# calculator works (as far as I know), I left it with that and did not look for new issues.
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: Oct 2008
Posts: 1,995
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: 294
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso
 
0
  #8
5 Days Ago
Sigh... those C-guys really have a hard time
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 2009
Posts: 398
Reputation: Ryshad has a spectacular aura about Ryshad has a spectacular aura about Ryshad has a spectacular aura about 
Solved Threads: 80
Ryshad's Avatar
Ryshad Ryshad is offline Offline
Posting Whiz
 
1
  #9
4 Days Ago
@sknake, i noticed one..if you key in 1+2=-= MS calc will return zero (total - total = 3 - 3 = 0) whilst my Xerox returns -1 (last number - total = 2 - 3 = -1).

I'm also curious as to whether this is truely incorrect key sequenes :p the same way that if you hit enter more than once it re-applies the last calculation, perhaps this is actually a feature
Please don't take for granted the work that solvers do for you. Take the time to fully understand the code they give you so that you might adapt it to future problems.

"Learning is more than absorbing facts, it is acquiring understanding.” - William Arthur Ward
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



Tag cloud for C#
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC