All submissions

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 reg, mask16=1;
    uint8_t i, mask8=1;
    reg= 0;

    for (i=0; i < 8; i++) {
        if ((val & mask8) != 0 ) {
            reg = reg | mask16;
        }
        mask8 = mask8 << 1;
        mask16 = mask16 << 2;
    }
    return reg;
}

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

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

Solving Approach

use two sliding masks: for 8-bits and 16-bits

set the register as you check the bits of the 8-bit value

 

 

Loading...

Input

202

Expected Output

20548