Compress Interleaved Bits Reverse Bit Spreading

Code

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

uint8_t compress_bits(uint16_t val) {
    // Your logic here
    val = ((val >> 1 | val << 15) & 0x3333) | (val & 0x1111);
    val = ((val >> 2 | val << 14) & 0x0D0D) | (val & 0x0303);
    val = ((val >> 4 | val << 12) & 0x00F0) | (val & 0x000F);
    val &= 0xFFFF;
    return val;
}

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