Compress Interleaved Bits Reverse Bit Spreading

Code

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

uint8_t compress_bits(uint16_t x) {
    x&=0x5555;
    x = (x | (x >> 1)) & 0x3333; 
    x = (x | (x >> 2)) & 0x0F0F; 
    x = (x | (x >> 4)) & 0x00FF;
    return x;
}

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