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 compressed_value = 0;
    uint8_t bit_pos = 0;

    for (uint8_t i = 0; i < 16; i++) {
        if (!(i % 2)) {
            compressed_value |= ((val & (1 << i)) >> i) << bit_pos++;
        }
    }
    return compressed_value;
}

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