All submissions

Compress Interleaved Bits Reverse Bit Spreading

Code

// 32. Compress Interleaved Bits Reverse Bit Spreading
// In the previous problem, we interleaved an 8-bit number into a 16-bit value by inserting 0s between each bit. Now your task is to:

// Reverse the interleaving process
// Extract only the bits from even-numbered positions in a 16-bit number
// Reconstruct the original 8-bit value

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

uint8_t compress_bits(uint16_t val)
{
    // Your logic here
    uint8_t result = 0;
    uint8_t temp = 0; 
    int k=1; 
    while (val)
    {
        temp = (val & 1U);
        
        if (temp){
             
            result = result + k*temp;

        }
        k=2*k; 
        val >>= 2; 
    }

    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