Bit Spreading Interleave Bits with Zeros

Code

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

#define is_bit_set(reg, pos) (reg & (1 << pos))
#define set_bit(reg, pos)    (reg | (1 << pos))

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

    for(int pos = 0; pos < 8; pos++)
    {
        if (is_bit_set(val, pos))
        {
            result = set_bit(result, pos * 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