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 result=0;
    for(int i=0;i<8;i++){
        if(val>>(2*i)&1){
            result|=(1<<(i));
        }
    }
    return result;
}

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

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

Solving Approach

 

 

There is an error in the question I feel, It says to reconstruct the output in b0,b1,b2 ..... form but the answer is MSB first , then LSB i.e  b7,b6,b5...b0. 

Upvote
Downvote
Loading...

Input

20548

Expected Output

202