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 out = 0;
    int out_bit = 0;

    // even positions: 0, 2, 4, ..., 14
    for (int pos = 0; pos < 16; pos += 2) {
        uint8_t bit = (val >> pos) & 1u;  // get bit at position pos
        out |= bit << out_bit;            // place it at b_out_bit
        out_bit++;
    }

    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