I tried this code and it did not do what I expected.

byte byt = byte.MaxValue;
BitArray bitArray = new BitArray(byt);
Debug.WriteLine("bitarray " + bitArray.Get(0).ToString());
Debug.WriteLine("bitarray " + bitArray.Get(5).ToString());

What I expected was the opposite of what I got, which was the following.
bitarray False
bitarray False

I thought byte byt = byte.MaxValue would set the byte to 255, which I believe is all bits set.
I got the same results with byte byt = byte.MinValue So what exactly do these initializes do?

Thanks for reading.

Recommended Answers

All 9 Replies

Did you forgot the MinValue in your code?

When I do this,

Byte B = Byte.MaxValue;           
Console.WriteLine("B in hex is {0:X}", B);
Byte C = Byte.MinValue;
Console.WriteLine("C in hex is {0:X}", C);

I get:
FF
0

Oops, was completely out of tune. You intialized your bit array with a byte value of 255, which gets implicitely converted to an integer. And this MSDN article says the 255 bytes of your array are all initialized to false.

Thank's ddanbe but your link leads to a BitArray(int32), I'm passing a byte.
To me it does not make sense that MaxValue should have all its bits set to false.

As ddanbe pointed out, the byte value is being converted to an integer - BitArray(int32).
Effectively what you have written is:

BitArray bitArray = new BitArray(255);

255 bits all set to 0 (false).

A BitArray looks a bit(haha) confusing at first.
Internally real bits are stored, externally you get booleans back.
It is some sort of wrapper class to make working with bitwise operators "simpler".
It will all clear out after reading this I hope.

Thanks guys, I think I have it.

If I want a BitArray containing 8 bits I use BitArray bitArray = new BitArray(8);

Glad I could help. :)

After reading somewhere that the BitArray class actually contains an int array, I decided it might not be what I wanted, as I want to pass a primitive byte around rather than a larger class,

So I rolled my own, which appears to do what I want.
If anyone has any comments at all, or suggestions to improve, I'd be delighted to hear them.

namespace MyProgram
{
    public static class Bit
    {
        /// <summary>
        /// "Returns bool representation of the bit at position pos"
        /// </summary>
        /// <param name="b"></param>
        /// <param name="pos"></param>
        /// <returns>bool</returns>
        public static bool GetBit(byte b, int pos)
        {
            if (pos < 0 || pos > 7)
            {
                throw new ArgumentOutOfRangeException("pos", "value must be betweem 0 and 7");
            }

            return (b & (1 << pos)) != 0;
        }

        /// <summary>
        /// "Sets the bit at position pos"
        /// </summary>
        /// <param name="b"></param>
        /// <param name="pos"></param>
        public static void SetBit(ref byte b, int pos)
        {
            switch (pos)
            {
                case 0:
                    b |= 1 << 0;
                    break;
                case 1:
                    b |= 1 << 1;
                    break;
                case 2:
                    b |= 1 << 2;
                    break;
                case 3:
                    b |= 1 << 3;
                    break;
                case 4:
                    b |= 1 << 4;
                    break;
                case 5:
                    b |= 1 << 5;
                    break;
                case 6:
                    b |= 1 << 6;
                    break;
                case 7:
                    b |= 1 << 7;
                    break;
                default:
                    throw new ArgumentOutOfRangeException("pos", "value must be betweem 0 and 7");
            }
        }

        /// <summary>
        /// "Unsets the bit at position pos"
        /// </summary>
        /// <param name="b"></param>
        /// <param name="pos"></param>
        public static void UnsetBit(ref byte b, int pos)
        {
            switch (pos)
            {
                case 0:
                    b &= byte.MaxValue ^ (1 << 0);
                    break;
                case 1:
                    b &= byte.MaxValue ^ (1 << 1);
                    break;
                case 2:
                    b &= byte.MaxValue ^ (1 << 2);
                    break;
                case 3:
                    b &= byte.MaxValue ^ (1 << 3);
                    break;
                case 4:
                    b &= byte.MaxValue ^ (1 << 4);
                    break;
                case 5:
                    b &= byte.MaxValue ^ (1 << 5);
                    break;
                case 6:
                    b &= byte.MaxValue ^ (1 << 6);
                    break;
                case 7:
                    b &= byte.MaxValue ^ (1 << 7);
                    break;
                default:
                    throw new ArgumentOutOfRangeException("pos", "value must be betweem 0 and 7");
            }
        }
    }
}
commented: All right! +15

You made a bell ring. I looked in some old "drawers" on my computer and found this:

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 to 1
                else
                    _bits &= ~(1 << index); //set bit index to 0           
            }
        }
    }

I used it once as help to make a binary clock.

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.