tags:

views:

555

answers:

5

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.HasValue));
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?

+14  A: 

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.

JSBangs
And if you need to access the ith bit (with i being 0-indexed), use `1 << i`.
Brian
I'll check that out. Thanks :)
borisCallens
+2  A: 

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
+8  A: 

Found it

BitArray bits = new BitArray(System.BitConverter.GetBytes(showGroup.Value));
borisCallens
You'll probably find that the in method that @JS Bangs suggested will be much quicker.
Kieron
Well, it's such a tiny conversion, it will hardly matter. But for the sake of correctness I would love to learn :)
borisCallens
There is an example on SO: http://stackoverflow.com/questions/8447/enum-flags-attribute
Groo
Thanks. [more characters to please the SO overlord]
borisCallens
A: 
int val = 33;
var bitarray = new BitArray(new[] { val });
var att = bitarray.Cast<bool>().ToArray();
Matthew Whited
+1  A: 

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
Thanks. I didn't know this was possible. Another tool in my belt :)
borisCallens