#include <stdio.h> #include <stdint.h> uint8_t compress_bits(uint16_t val) { // Your logic here uint8_t res = 0; volatile uint8_t i; for(i=0;i<8;i++){ res |= ( (val&1) << i); val >>= 2; } return res; } int main() { uint16_t val; scanf("%hu", &val); uint8_t result = compress_bits(val); printf("%u", result); return 0; }
Declare a res variable
Loop 8 times
check if the lsb of val is 1 and shift it by i, set that bit according to the positon
right shift the value by 2.
return res
Test Cases
Test Results
Input
20548
Expected Output
202