Bit Spreading Interleave Bits with Zeros

Code

#include <stdio.h>
#include <stdint.h>
#define PRINTBIN16(x) \
    printf("%d%d%d%d %d%d%d%d %d%d%d%d %d%d%d%d\n", \
        ((x) >> 15) & 1, \
        ((x) >> 14) & 1, \
        ((x) >> 13) & 1, \
        ((x) >> 12) & 1, \
        ((x) >> 11) & 1, \
        ((x) >> 10) & 1, \
        ((x) >> 9) & 1,  \
        ((x) >> 8) & 1,  \
        ((x) >> 7) & 1,  \
        ((x) >> 6) & 1,  \
        ((x) >> 5) & 1,  \
        ((x) >> 4) & 1,  \
        ((x) >> 3) & 1,  \
        ((x) >> 2) & 1,  \
        ((x) >> 1) & 1,  \
        ((x) >> 0) & 1   \
    )

uint16_t spread_bits(uint8_t val) {
    // Your logic here
    uint16_t ans = 0; 
    for(int i =7;i>=0;i--){
    ans |= (val&1u<<i)<<i;
    //PRINTBIN16(ans);
    }
    return ans;
}

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