Boris Callens
Boris Callens

Reputation: 93327

Why does the BitConverter return Bytes and how can I get the bits then?

As input I get an int (well, actually a string I should convert to an int).
This int should be converted to bits.
For each bit position that has a 1, I should get the position.
In my database, I want all records that have an int value field that has this position as value.
I currently have the following naive code that should ask my entity(holding the databaseValue) if it matches the position, but obviously doesn't work correctly:

Byte[] bits = BitConverter.GetBytes(theDatabaseValue);
return bits[position].equals(1);

Firstly, I have an array of byte because there apparantly is no bit type. Should I use Boolean[] ? Then, how can I fill this array? Lastly, if previous statements are solved, I should just return bits[position]

I feel like this should somehow be solved with bitmasks, but I don't know where to start..

Any help would be appreciated

Upvotes: 0

Views: 2003

Answers (3)

Tmdean
Tmdean

Reputation: 9299

Your feeling is correct. This should be solved with bitmasks. BitConverter does not return bits (and how could it? "bits" isn't an actual data type), it converts raw bytes to CLR data types. Whenever you want to extract the bits out of something, you should think bitmasks.

If you want to check if a bit at a certain position is set, use the & operator. Bitwise & is only true if both bits are set. For example if you had two bytes 109 and 33, the result of & would be

  0110 1101
& 0010 0001
-----------
  0010 0001

If you just want to see if a bit is set in an int, you & it with a number that has only the bit you're checking set (ie 1, 2, 4, 8, 16, 32 and so forth) and check if the result is not zero.

List<int> BitPositions(uint input) {
    List<int> result = new List<int>();
    uint mask = 1;
    int position = 0;
    do {
        if (input & mask != 0) {
            result.Add(position);
        }
        mask <<= 1;
        position++;
    } while (mask != 0);

    return result;
}

Upvotes: 4

Szymon Rozga
Szymon Rozga

Reputation: 18168

Do not use Boolean. Although boolean has only two values, it is actually stored using 32 bits like an int.

EDIT: Actually, in array form Booleans will be packed into bytes, not 4 bytes.

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1500675

I suspect BitArray is what you're after. Alternatively, using bitmasks yourself isn't hard:

for (int i=0; i < 32; i++)
{
    if ((value & (1 << i)) != 0)
    {
        Console.WriteLine("Bit {0} was set!", i);
    }
}

Upvotes: 4

Related Questions