All submissions

Bit Spreading Interleave Bits with Zeros

Code

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

uint16_t spread_bits(uint8_t val) {

    uint16_t reg = 0;

    // Loop through the 8 bits of val and place them in the result
    for (int i = 0; i < 8; i++) {
        if (val & (1 << i)) {
            reg |= (1 << (2 * i)); // Spread the bit to the correct position 2 * i inserts in even pos
        }
    }
    
    return reg;
}

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

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

Solving Approach

 

 

 

Loading...

Input

202

Expected Output

20548