Compress Interleaved Bits Reverse Bit Spreading

Code

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


  uint8_t compress_bits(uint16_t val) {
    uint8_t out = 0;

    for (int i = 0; i < 16; i += 2) {       // even bit positions
        uint8_t bit = (val >> i) & 1;       // extract bit at pos i
        out |= (bit << (i / 2));            // place bit into correct 8-bit position
    }

    return out;
}


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