tags:

views:

134

answers:

6

I have method for converting array of Booleans to integer. It looks like this

class Program
    {
        public static int GivMeInt(bool[] outputs)
        {
            int data = 0;
            for (int i = 0; i < 8; i++)
            {
                data += ((outputs[i] == true) ? Convert.ToInt32(Math.Pow(2, i)) : 0);
            }
            return data;

        }

        static void Main(string[] args)
        {
            bool[] outputs = new bool[8];
            outputs[0] = false;
            outputs[1] = true;
            outputs[2] = false;
            outputs[3] = true;
            outputs[4] = false;
            outputs[5] = false;
            outputs[6] = false;
            outputs[7] = false;
            int data = GivMeInt(outputs);
            Console.WriteLine(data);
            Console.ReadKey();
        }
   }

Now I want to make opposite method returning array of Booleans values As I am short with knowledge of .NET and C# until now I have only my mind hardcoding of switch statement or if conditions for every possible int value.

public static bool[] GiveMeBool(int data)
        {
            bool[] outputs = new bool[8];

            if (data == 0)
            {
                outputs[0] = false;
                outputs[1] = false;
                outputs[2] = false;
                outputs[3] = false;
                outputs[4] = false;
                outputs[5] = false;
                outputs[6] = false;
                outputs[7] = false;
            }
            //After thousand lines of coed
            if (data == 255)
            {
                outputs[0] = true;
                outputs[1] = true;
                outputs[2] = true;
                outputs[3] = true;
                outputs[4] = true;
                outputs[5] = true;
                outputs[6] = true;
                outputs[7] = true;
            }
            return outputs;
        }

I know that there must be easier way.

+1  A: 

This whole thing can be changed to use bitmaps using shift operators.

Oded
+8  A: 

You need to use bitwise operators: (Tested)

public static bool[] GiveMeBool(int data) {
    bool[] outputs = new bool[8];

    for(int i = 0; i < 8; i++)
        outputs[i] = (data & (1 << i)) == (1 << i);

    return outputs;
}

You can also use bitwise operators to make your original much faster: (Untested)

public static int GivMeInt(bool[] outputs) {
    int data = 0;
    for (int i = 0; i < 8; i++) 
        data += outputs[i] ? 1 << i : 0;

    return data;
}
SLaks
+1, but the 2 << i should be 1 << i?
Chris Taylor
@Chris: Yes, you're right.
SLaks
+1  A: 

Try this...

public static bool[] GiveMeBool(int data)
{
    bool[] outputs = new bool[8];

    for (int i = 0; i < 8; i++)
        outputs[i] = (data & (int)Math.Pow(2, i)) != 0;

    return outputs;
}
David
A: 

What about something like the following?

Int32 x = 0xFF33;
bool[] retval = new bool[32];
for (int i = 0; i < 32 && x != 0; i++, x = x >> 1)
{
    retval[i] = (x & 1) == 1;
}

It uses bit-shifting to do its magic.

yodaj007
+1  A: 

I tested the methods in Java. The only significant difference is the bool vs boolean keywords.

public class Test {

    public static void main(String[] args) {
        boolean[] bools = new boolean[]{true,true,false,false,false,false,false,false};
        int num = GivMeInt(bools);
        boolean[] bools2 = GivMeBools(num);
        for(int i = 0; i < 8; i++) System.out.println(bools[i]==bools2[i]);
    }

    public static int GivMeInt(boolean[] outputs)
        {
            int data = 0;
            for (int i = 0; i < 8; i++)
            {
                if(outputs[i]) {
                    data += (1 << i);

                }
            }
            return data;
    }

    public static boolean[] GivMeBools(int input)
        {
            boolean[] outputs = new boolean[8];
            for (int i = 0; i < 8; i++)
            {
                outputs[i] = (input & 0x1) == 1;
                input = input >>> 1;
            }
            return outputs;

        }
}
glowcoder
+2  A: 

This uses bit shifting.

public static bool[] GiveMeBool(Int32 data)
{
    bool[] bits = new bool[32];

    for (i = 0; i <= bits.Length - 1; i++) {
        bits(i) = (data & 1) == 1;
        data >>= 1;
    }

    return bits;
}
Carter
+1 Thanks Works also
adopilot