Bit Spreading Interleave Bits with Zeros

Code

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

uint16_t spread_bits(uint8_t val) {
    int a=0;
    int b=0;
    int reg=0x0000;
    while(a!=8){
        if(val & (1<<a)){
            reg |=(1<<b);
        }
        else{
            reg &=~(1<<b);
        }
        a++;
        b=b+2;

    }
    // Your logic here
    
    return reg;
}

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