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?

struct IntBits
    {
        private int _bits;

        public IntBits(int InitialBitValue)
        {
            _bits = InitialBitValue;
        }

        //declare indexer
        public bool this[int index]
        {
            get
            {
                return (_bits & (1 << index)) != 0;
            }
            set
            {
                if (value)
                    _bits |= (1 << index);  //set bit index 1
                else
                    _bits &= ~(1 << index); //set bit index 0           
            }
        }
    }

You could do it at the string level with .PadLeft() or are you wanting this done property with binary formatting?

private void button2_Click(object sender, EventArgs e)
    {
      int myInt = 5;
      string res = Convert.ToString(myInt, 2).PadLeft(16, '0');
      System.Diagnostics.Debugger.Break();
    }

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.

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

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!

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?

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.

Sigh... those C-guys really have a hard time;)

@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 ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.