Bit Spreading Interleave Bits with Zeros

Code

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

uint16_t spread_bits(uint8_t x) {
    uint16_t result = 0;
    
    for (int i = 0; i < 8; i++) {
        // Extract bit i from x
        uint8_t bit = (x >> i) & 1;

        // Place it at position 2*i in the 16-bit result
        result |= (bit << (2 * i));
    }
    
    return result;
}

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

    printf("%u", spread_bits(x));
    return 0;
}

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

202

Expected Output

20548