Bitmapping Tables in C

Is there a template for the standard way of permuting bits in accordance with the permutation table, which sets the result for each bit position - which position is taken from the source.

those. table 0322will create the result 0011from0010

My current strategy was to read each table entry - create a bitmask, and then execute the binary code AND the mask and source, OR, which with a cumulative result.

to process the first record in the table:

result |= ( ( (int) pow(2,table[0]) & source)

It just seems expensive and repetitive and homegrown. Am I missing some obvious standard easier way?

+4
source share
2 answers

pow . .

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

. DES S-box, . , . - .

- . 4- :

int bits[4] = { 1, 2, 4, 8 };
result = (bits[table[0]] & source)
       | (bits[table[1]] & source)
       | (bits[table[2]] & source)
       | (bits[table[3]] & source);

[ , , .]

. !

+3

Source: https://habr.com/ru/post/1536528/


All Articles