92. Compress Interleaved Bits Reverse Bit Spreading

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

// Reverse spread: extract bits at even positions
uint8_t compress_bits(uint16_t val) {
    uint8_t result = 0;

    for (int i = 0; i < 8; i++) {
        // Extract bit at position 2*i from val
        uint8_t bit = (val >> (2 * i)) & 1;

        // Place it at i-th bit position in result
        result |= (bit << i);
    }

    return result;
}

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

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

What’s the Goal?

Reverse the spreading/interleaving logic and reconstruct the original 8-bit byte.

Why It Matters?

  • Used when reading hardware registers that store interleaved bit lines
  • Helps decode compressed or rearranged signals (Z-order, Morton code)

Logic Summary

  • Extract bits from even positions (0, 2, 4…) in 16-bit value
  • Combine into a new 8-bit result
     
Loading...

Input

20548

Expected Output

202