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

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