#include <stdio.h>
#include <stdint.h>
uint16_t spread_bits(uint8_t val) {
// Your logic here
uint16_t res = val;
/*
volatile char i;
for(i=0;i<8;i++){
if(val&(1<<i)){
res |= (1<<(2*i));
}
}
return res;*/
res = (res|(res<<4)) & 0x0F0F;
res = (res|(res<<2)) & 0x3333;
res = (res|(res<<1)) & 0x5555;
return res;
}
int main() {
uint8_t val;
scanf("%hhu", &val);
uint16_t result = spread_bits(val);
printf("%u", result);
return 0;
}
res = (res|(res<<4)) & 0x0F0F;
This shifts the bits by 4 places and then keeps it in an order such that 4 bits appear together.
res = (res|(res<<2)) & 0x3333;
This shifts the bits by 2 places and then keeps it in an order such that 2 bits appear together.
res = (res|(res<<1)) & 0x5555;
This shift the bits by 1 and then keeps in the order we need to attain 1 bit followed by 0.
Input
202
Expected Output
20548