Compress Interleaved Bits Reverse Bit Spreading

Code

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

uint8_t compress_bits(uint16_t val) {
    uint8_t answer = 0;
    for(int i=8; i>=1; i--){
        answer <<= 1;
        answer |= ((val & 0x4000) >> 14);
        // printf("answer[%d]: %b\n", i, answer);
        val <<= 2;
    }
    return answer;
}

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