Bit Spreading Interleave Bits with Zeros

Code

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

uint16_t spread_bits(uint8_t val) 
{
    uint16_t temp=0,bit=0,i,j;
    for(i=0,j=0;i<=15;i++)
    {
        if(i==0)
        {
            bit=(val>>j)&1;
            if(bit==1)
            {
                temp |=(1<<j);
            }
            else
            {
                temp &=~(1<<j);
            }
            j++;
        }
        else if(i%2!=0)
        {
            temp &=~(1<<i);
        }
        else if(i%2==0)
        {
            bit=(val>>j)&1;
            if(bit==1)
            {
                temp |=(1<<i);
            }
            else
            {
                temp &=~(1<<i);
            }
            j++;
        }
    }
    return temp;
}

int main() {
    uint8_t val;
    scanf("%hhu", &val);
    uint16_t result = spread_bits(val);
    printf("%d",result);
    return 0;
}

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

202

Expected Output

20548