Bit Spreading Interleave Bits with Zeros

Code

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

uint16_t spread_bits(uint8_t val) {
    unsigned char i;
    unsigned int result =0x0000,k;
   for(i=0;i<=7;i++){ //to access all 8 bits of val
     k = (val>>i) & 1; //save value of ith bit of val in k
    result = result | (k<<(2*i)); //add the k value in result at 2i position
   }


    return result;
}

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

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

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

202

Expected Output

20548