Compress Interleaved Bits Reverse Bit Spreading

Code

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

uint8_t compress_bits(uint16_t val) {
    uint8_t a=0;
    /*val = (val & 0xFF00)>>8 | (val & 0x00FF)<<8;
    val = (val & 0xF0F0)>>4 | (val & 0x0F0F)<<4;
    val = (val & 0xCCCC)>>2 | (val & 0x3333)<<2;
    val = (val & 0xAAAA)>>1 | (val & 0x5555)<<1;*/
    for(int i=0;i<8;i++){
        a |= ((val>>(i*2))&1)<<i;
    }

    return a;
}

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