Compress Interleaved Bits Reverse Bit Spreading

Code

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

uint8_t compress_bits(uint16_t val) {
    uint8_t tmp = 0;
    tmp |= val & 1; val >>= 1;
    tmp |= val & (1 << 1); val >>= 1;
    tmp |= val & (1 << 2); val >>= 1;
    tmp |= val & (1 << 3); val >>= 1;
    tmp |= val & (1 << 4); val >>= 1;
    tmp |= val & (1 << 5); val >>= 1;
    tmp |= val & (1 << 6); val >>= 1;
    tmp |= val & (1 << 7); val >>= 1;
    return tmp;
}

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