Bit Spreading Interleave Bits with Zeros

Code

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

uint16_t spread_bits(uint8_t val) {
    uint16_t output=0; // create 16 bit integer
    for(int i = 0; i < 16; i++){ // iterate through each bit in the integer
        if(i % 2 == 0){ // check if on even bit
            if(val & (1 << (i / 2))){ // check if the bit in val is 1 (must divide bit by two since val is an 8-bit integer)
                output |= (1 << i); // if that certain bit in val is 1, then it's respective bit in output should be 1
            }
        }
        
    }
    return output; // return output
}

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