All submissions

Bit Spreading Interleave Bits with Zeros

Code

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

uint16_t spread_bits(uint8_t val)
{
    uint16_t ans=0x0000; //Initially taking 16bit value which is initialized with 0
    int i;
    int j=0;
    for(i=0;i<15;i=i+2) //as we need to update at pos=0,2,4,6...Hence we are incrementing with 2
    {
        if(val&(1<<j)) //extracting each bit from input.If the bit is high, then set the bit at appropriate position in 16bit variable.
        {
          ans=ans|(1<<i);
        }
        j++;
    }  

    return ans;
}

int main() 
{
    uint8_t val;
    scanf("%hhu", &val);

    uint16_t result = spread_bits(val);
    printf("%u", result);
    return 0;
}

Solving Approach

 

 

 

Loading...

Input

202

Expected Output

20548