Bit Spreading Interleave Bits with Zeros

Code

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

uint16_t spread_bits(uint8_t val) {
    // Your logic here
    uint16_t value = 0;
    uint8_t bit_value = 0;

    for(uint8_t count = 0; count < 16; count+=2 ){
        bit_value = ((val >> (count/2)) & 1);
        value = value | (bit_value << count);
    }

    return value;
}

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

    uint16_t result = spread_bits(val);
    printf("%u", result);
    return 0;
}

Solving Approach

The individual bits are extracted and the corresponding bit positions are set.

 

 

Upvote
Downvote
Loading...

Input

202

Expected Output

20548