All submissions

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 result = 0;
    val = val & 0x5555;
    val = (val | val>>1) & 0x3333;
    val = (val | val>>2) & 0x0F0F;
    val = (val | val>>4) & 0x00FF;
    return (val);
    // for(int i=0;i<8;i++)
    // {
    //     result = result | (((val>>(i*2)) & 1U)<<i);
    // }
    // return result;

}

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

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

Solving Approach

 

 

 

Loading...

Input

20548

Expected Output

202