Bit hack: Expanding bits

前端 未结 8 1505
名媛妹妹
名媛妹妹 2021-02-13 21:27

I am trying to convert a uint16_t input to a uint32_t bit mask. One bit in the input toggles two bits in the output bit mask. Here is an example conver

相关标签:
8条回答
  • 2021-02-13 22:17

    The easiest way to map a 4-bit input to an 8-bit output is with a 16 entry table. So then it's just a matter of extracting 4 bits at a time from the uint16_t, doing a table lookup, and inserting the 8-bit value into the output.

    uint32_t expandBits( uint16_t input )
    {
        uint32_t table[16] = {
            0x00, 0x03, 0x0c, 0x0f,
            0x30, 0x33, 0x3c, 0x3f,
            0xc0, 0xc3, 0xcc, 0xcf,
            0xf0, 0xf3, 0xfc, 0xff
        };
    
        uint32_t output;
        output  = table[(input >> 12) & 0xf] << 24;
        output |= table[(input >>  8) & 0xf] << 16;
        output |= table[(input >>  4) & 0xf] <<  8;
        output |= table[ input        & 0xf];
        return output;
    }
    

    This provides a decent compromise between performance and readability. It doesn't have quite the performance of cmaster's over-the-top lookup solution, but it's certainly more understandable than thndrwrks' magical mystery solution. As such, it provides a technique that can be applied to a much larger variety of problems, i.e. use a small lookup table to solve a larger problem.

    0 讨论(0)
  • 2021-02-13 22:17

    Try this, where input16 is the uint16_t input mask:

    uint32_t input32 = (uint32_t) input16;
    uint32_t result = 0;
    uint32_t i;
    for(i=0; i<16; i++)
    {
        uint32_t bit_at_i = (input32 & (((uint32_t)1) << i)) >> i;
        result |= ((bit_at_i << (i*2)) | (bit_at_i << ((i*2)+1)));
    }
    // result is now the 32 bit expanded mask
    
    0 讨论(0)
提交回复
热议问题