All submissions

Bit Spreading Interleave Bits with Zeros

i)Code

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

uint16_t spread_bits(uint8_t val) {
    // Your logic here
    uint16_t result = 0;
    for(int i=0;i<8;i++)
    {
        if((val>>i)&0x01)
        {
            result |= (1<<(2*i));
        }
    }
    return result;
}

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

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

Solving Approach

  1. Loop through each bit from 0 to 7 in the input byte

  2. for each bit, shift it to new position: (1<<(2*i))(even position only)
  3. use bitwise OR to accumulate the result into a 16-bit variable result |= (1<<(2*i))

 

 

Loading...

Input

202

Expected Output

20548