Compress Interleaved Bits Reverse Bit Spreading

Code (Could have done better)

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

uint8_t compress_bits(uint16_t val) {
    // Your logic here
    uint8_t result = 0;
    for (int i = 0; i < 8; ++i) {
        result <<= 1;
        result |= ((val & 0x4000) == 0x4000) ? 1 : 0;
        val <<= 2;
    }

    return result;
}

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