Bit Spreading Interleave Bits with Zeros

Code

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

uint16_t spread_bits(uint8_t val) {
    // Your logic here
    uint16_t bit_spreaded_result = 0;
    for(int i =0 ; i< 8 ; i++)
    {
        uint8_t bit_val = 1& (val>>i); //Validating the values from MSB to LSB 
        bit_spreaded_result |=(bit_val << (2*i)); // Inserting the value in every alternate position
    }
    return bit_spreaded_result;
}

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

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

Solving Approach

 

i = 0 to 7

Extracting value of a bit = (val>>i) & 1;  // By checking and validating 
As we need to show the interleaved result 
Make use of a 2nd variable : interleaved_result = (bit<<(2*i))

 

Upvote
Downvote
Loading...

Input

202

Expected Output

20548