Bit permutation tables in C

bit-manipulation, c

Solution

It's really expensive to use the `pow` function for this. The repetitive and home-brewed parts are unavoidable. A better way

result = 0;
for ( i = 0; i < table_size; i++ )
{ 
    result <<= 1;
    if ( source & (1 << table[i]) )
        result |= 1;
}

Problem

Is there a pattern for, or a standard way to permute bits according to a permutation table that specifies for each bit position of the result - which position is taken from the source. I.e. table `0322` would create result `0011` from `0010` My current strategy has been to read each entry of the table - create a bitmask and then perform a binary AND of the mask and the source, OR`ing that with a cumulative result. so to process the first table entry: ``` result |= ( ( (int) pow(2,table[0]) & source) ``` This just seems expensive and repetitive and homebrewed. Am I missing some obvious standard easier way?

Original source