All submissions

Bit Spreading Interleave Bits with Zeros

Code

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

void printBinary(uint16_t num) {
    // Determine the number of bits for the integer type
    // sizeof(int) * 8 gives the total number of bits
    for (int i = sizeof(uint16_t) * 8 - 1; i >= 0; i--) {
        // Use bitwise right shift (>>) and bitwise AND (&) to check each bit
        printf("%u", (num >> i) & 1);
    }
    printf("\n");
}


uint16_t spread_bits(uint8_t val) {
    // Your logic here
    uint16_t spread = 0;
    for(int i = 0; i < 8; i++) {
        uint16_t bit = val & (1 << i);
        spread = spread | (bit << (i*2)-i);
    }
    return spread;
}

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