Bit Spreading Interleave Bits with Zeros

Code

#include <stdio.h>
#include <stdint.h>

#define IS_SET(reg, pos) ((1<<(pos) & (reg)) ? 1 : 0)
#define SET_BIT(reg, pos) (1<<(pos) | (reg))

uint16_t spread_bits(uint8_t val) {
    uint16_t result = 0;
    for (int pos = 0; pos < 8; pos++) {
        if (IS_SET(val, pos)) {
            result = SET_BIT(result, pos*2);
        }
    }
    return result;
}

int main() {
    uint8_t val;
    scanf("%hhu", &val);

    uint16_t result = spread_bits(val);
    printf("%u", result);
    return 0;
}

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

202

Expected Output

20548