All submissions

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 res=0; int temp,pos,count;
    
    // count=8; pos = 0;
    // while(count!=0){
    //     if(pos%2!=0){
    //         temp = pos;
    //         if(val & (1<<pos))
    //             res |= (1<<(pos+1));
    //         }
    //     if(pos%2==0){
    //         if(val & (1<<pos))
    //             res |= (1<<pos);
    //     }  
    //     pos++; 
    //     count--;
    // }
    for(int i=0; i<8; i++)
    {
        if(val&(1<<i)){
            res|= (1<<(2*i));
        }
    }
     
    return res;
}

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

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

Solving Approach

Loop through the bits 8 times.

If ith bit is set high in val, then set result as high at (2*i)th position.

 

 

 

Loading...

Input

202

Expected Output

20548