Bit Spreading Interleave Bits with Zeros

Code

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

uint16_t spreadBits(uint8_t n) {
    uint16_t result = 0;

    // Process each bit from LSB to MSB
    for (int i = 0; i < 8; i++) {
        if (n & (1u << i)) {
            result |= (1u << (2 * i));  // place bit in even position
        }
    }

    return result;
}

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

    printf("%hu", spreadBits(n));
    return 0;
}

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

202

Expected Output

20548