what is the quickest way to convert a bool array to an int? - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

what is the quickest way to convert a bool array to an int?

spencerjackspencerjack Posts: 4Member
the array is 8 elements and is contained in a class i created called 'zippie'

[code]

int bin2int(zippie bin)
{
int count;
int temp;
for (count = 0; count < zippie_size; count++)
{

if (zippie.zip_index[count])
{
temp += pow(2,(float)count);
}

}

return temp;

}


[/code]

surely there's a quicker way to do this

and is there a version of pow() that will take an int?


Comments

  • AsmGuru62AsmGuru62 Posts: 6,519Member
    [code]
    int PackBools (bool* v, int n)
    {
    int mask = 1, result = 0;
    for (int i=0; i<n; i++, mask <<= 1)
    {
    if (v [i] == true) result |= mask;
    }
    return result;
    }

    int main ()
    {
    bool a [] = { true, false, false, true, true, false, true };
    int packed = PackBools (a, sizeof (a) / sizeof (a [0]));
    return 0;
    }
    [/code]
Sign In or Register to comment.