Bit Spreading Interleave Bits with Zeros

Code

#include <stdio.h>
#include <stdint.h>
#define SET(reg, pos) reg |= (1 << pos)
#define CLEAR(reg, pos) reg &= ~(1 << pos)
#define IS_POS_SET(reg, pos) (reg >> pos) & 1
uint16_t spread_bits(uint8_t val) {
    // Your logic here
    uint16_t result = 0x0000;
    for (int i = 0; i < 8; i++){
        if(IS_POS_SET(val, i)){
            SET(result, 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