Bit Spreading Interleave Bits with Zeros

Code

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

uint16_t spread_bits(uint8_t n) {
    // Your logic here
  uint16_t result = 0;

    for (int i = 0; i < 8; i++) {
        // 1. Isolate the i-th bit using a mask
        uint8_t bit = (n >> i) & 1;

        // 2. Shift that bit to the even position (i * 2)
        // 3. OR it into the result
        result |= (bit << (i * 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