13

Is there a way to convert an int to a bitmask?

example:

int i = 33;

should be converted to (not sure of the datatype)

bool[] bitmask = new[] {true, false, false, false, false, true};

Update
In reaction to most answers:

I need to do this:

BitArray bits = new BitArray(BitConverter.GetBytes(showGroup.Value));
List<String> showStrings = new List<string>();
for (int i = 0; i < bits.Length; i++)
{
    if(bits[i])
        showStrings.Add((i+1).ToString().PadLeft(2, '0'));
}

How would that go without converting it to a bitarray?

Boris Callens
  • 90,659
  • 85
  • 207
  • 305

5 Answers5

32

An int already is a bitmask. If you want to twiddle the bits, you can use bitwise operators freely on ints. If you want to convert the int to an enum that has the Flags attribute, a simple cast will suffice.

JSBձոգչ
  • 40,684
  • 18
  • 101
  • 169
13

Found it

BitArray bits = new BitArray(System.BitConverter.GetBytes(showGroup.Value));
Boris Callens
  • 90,659
  • 85
  • 207
  • 305
5

You could construct a bool[32] and loop through all bits in the int, masking it with 2^(loop counter) and setting the bools in the array appropriately.

Are you sure you need this, though? Most operations with bitmasks work with ints directly.

To answer the question in your edit:

int val = 35;
List<string> showStrings = new List<string>();
for (int i = 0; i < 32; i++)
{
    if (( (1 << i) & val) > 0)
    {
        showStrings.Add((i + 1).ToString().PadLeft(2, '0'));
    }
}

prints:

01  
02  
06 

Not the most obvious solution if you're not used to bit arithmetic, true. Mask each bit in the integer value with 2^(bit-index), and if the resulting value is greater than zero (indicating that the bit at that index is set), do something. 1 << i (left-shifting) is equivalent to 2^i, and may have the same performance characteristics once JITted, but I'm used to this form.

Expressed as a macro-like method:

bool IsSet(int val, int index)
{
    return (( (1 << (index-1)) & val) > 0);
}
Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
4
int val = 33;
var bitarray = new BitArray(new[] { val });
var att = bitarray.Cast<bool>().ToArray();
Matthew Whited
  • 22,160
  • 4
  • 52
  • 69
  • I like this. For those interested, the Cast method is an Extension Method and requires "using System.Linq" declaration which under Mono requires a reference to System.Core – Wayne Phipps Feb 10 '13 at 15:51
2

Since you asked, here is a solution without using BitArray:

// First define a bitmask enum for the bits you are interested in
[Flags]
public enum BitFlags
{
  Flag1 = 1,
  Flag2 = 2,
  Flag3 = 4,
  Flag4 = 8,
  Flag5 = 16
  // ...
}

int index = 0;
List<string> showStrings = new List<string>();
foreach(int flag in Enum.GetValues(typeof(BitFlags))cast<int>())
{
  index += 1;
  if ((input & flag) == flag)
    showStrings.Add(index.ToString().PadLeft(2, '0'));
}

It is about the same amount of code, with negligible performance difference. It does however let you strongly define your bit values and you can choose to omit bits in the BitFlags enum that you don't care about.

Annabelle
  • 10,596
  • 5
  • 27
  • 26