I have a boolean array with 9 members on the one hand and an integer on the other. These methods convert one into the other.
Can this be made easier with binary operators?
static bool[] IndexToStatus(int index)
{
bool[] res = new bool[9] { false, false, false, false, false, false, false, false, false };
for (byte i = 8; i >= 0; i--)
{
int a = (int)(Math.Pow(2, i));
if (index >= a)
{
index -= a;
res[i] = true;
}
if (i == 0)
break;
}
return res;
}
static int StatusToIndex(bool[] status)
{
int res = 0;
for (byte i = 0; i < 9; i++)
if (status[i])
res += (int)(Math.Pow(2, i));
return res;
}


Sign In
Create Account


Back to top









