Bit Spreading Interleave Bits with Zeros

Code

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

uint16_t spread_bits(uint8_t val) {
    uint16_t res=0;
    int j=0, bit_status;
    
    for(int i=0; i<8; i++) {
        bit_status = val & 1;
        if(bit_status==1) {
            //If the original bit is 1 then SET
            res |= (1 << j++);
        }
        if(bit_status==0) {
            //If the original bit is 0 then CLEAR 
            res &= ~(1 << j++);
        }
        
        res &= ~(1 << j++); //Spread Bits
        val = val >> 1;


    }
    
    return res;
}

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