Compress Interleaved Bits Reverse Bit Spreading

Code

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

uint8_t compress_bits(uint16_t val) {
    uint8_t result = 0;
    uint8_t pos = 0;

    while (val && pos < 8) {
        result |= (val & 1) << pos;// đưa về đúng vị trí
        val >>= 2;   // bỏ 1 bit lẻ giữa các bit chẵn
        pos++;
    }

    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