Compress Interleaved Bits Reverse Bit Spreading

Code

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

uint8_t compress_bits(uint16_t val) {
    // Your logic here
    uint8_t ret = 0;
    for (size_t i = 0; i < 8; i++) {
        uint16_t masked = val & (1u << (i*2));
        if (masked != 0) {
            ret |= (1u << i);
        }
    }
    return ret;
}

int main() {
    uint16_t val;
    scanf("%hu", &val);

    uint8_t result = compress_bits(val);
    printf("%u", result);
    return 0;
}

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

20548

Expected Output

202