none
How do you work with bits in C#?

    Question

  • Hi There,

    I have an application where I need to work with bit strings. My existing code is already written in C# so I'd like to continue with it. So far I haven't had too much finding out how to do things. There's a BitArray Class but it's a little obtuse to use, there's the byte type, but again a little obtuse. When I say obtuse, they're a little hard to initialize and you can't easily print a bit string of 0's and 1's. What I'd really like to be able to do is create a bit array or something similar and use that and be able to deal on the individual bit level. Any way I can do this? Or some ideas and examples of working with the classes and data types that are available? I guess bool really is a bit and I could make a class that uses bools to have all my nice features. Maybe that's the way to go. Just looking for some direction.

    Thanks for any help,
    Chris
    Friday, September 16, 2005 9:26 PM

Answers

  • The class to use is BitArray. Depending on how many bits you're dealing with, you could use an array of bools. Bools are fast, but they're 32-bit data types that store one bit of data. (This is by design. On a 32-bit processor, you can quickly see if a bool is true or false without any bit-shifting.) BitArray pack the bits more densely - they are stored internally as integers, but are packed 32 bits to an int. Much more efficient on space, but at the cost of performance because you need to bit-shift if you want to determine if a particular bit is set. The BitArray does have an indexer defined. So you can use it quite simply as a bag of bits like so:

    BitArray bits = new BitArray(42);
    for(int i=0; i<bits.Length; i+=2) {
      bits[ i ] = true;
    }
    foreach(bool bit in bits) {
      Console.WriteLine(bit);
    }

    If you want better ability to write out/read in strings of ones and zeros, you could always write another class that encapsulates BitArray essentially using it for storage, while you implement your own functionality. Unfortunately BitArray is marked sealed so you cannot derive a new class from it.

    Friday, September 16, 2005 10:25 PM
    Moderator

All replies

  • The class to use is BitArray. Depending on how many bits you're dealing with, you could use an array of bools. Bools are fast, but they're 32-bit data types that store one bit of data. (This is by design. On a 32-bit processor, you can quickly see if a bool is true or false without any bit-shifting.) BitArray pack the bits more densely - they are stored internally as integers, but are packed 32 bits to an int. Much more efficient on space, but at the cost of performance because you need to bit-shift if you want to determine if a particular bit is set. The BitArray does have an indexer defined. So you can use it quite simply as a bag of bits like so:

    BitArray bits = new BitArray(42);
    for(int i=0; i<bits.Length; i+=2) {
      bits[ i ] = true;
    }
    foreach(bool bit in bits) {
      Console.WriteLine(bit);
    }

    If you want better ability to write out/read in strings of ones and zeros, you could always write another class that encapsulates BitArray essentially using it for storage, while you implement your own functionality. Unfortunately BitArray is marked sealed so you cannot derive a new class from it.

    Friday, September 16, 2005 10:25 PM
    Moderator
  • What exactly do you want to do with the bit strings?

    Convert Integer to a Binary string:


    someBinaryString = Convert.ToString(someInteger, 2);

     

    Convert Binary string to Integer:


    someInteger = Convert.ToInt32(someBinaryString, 2);

     
    Saturday, September 17, 2005 12:36 PM
  • you are completely awesome. if anyone ever tells you different, direct them to me.
    Friday, December 15, 2006 9:25 AM
  • And what about  "BitVector32 structure"
    Tuesday, May 22, 2007 3:45 AM
  • Hey man - thanks for the BitVector32 tip. It is great!

    I'd never heard of it before you mentioned it.

    Ronnie

    Wednesday, August 29, 2007 6:17 PM