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 newval=0;
    for (int i=0; i<16;i=i+2){
        //newval =(newval<<1);
        newval|=((val&0x01)<<i/2);
        val=(val>>2);
    }
    return newval;
}

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

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

Solving Approach

Take the last bit and and then just right shift the value by 2. Then just append the value to the result by left shifting by i/2.

 

 

Upvote
Downvote
Loading...

Input

20548

Expected Output

202