#include <stdio.h> #include <stdint.h> uint16_t spread_bits(uint8_t val) { uint16_t result = 0x0; uint16_t target = 0x0; for (unsigned i = 0; i < 8; i++) { target = (val >> i) & 1U; result |= (target << (2 * i)); } return result; } int main() { uint8_t val; scanf("%hhu", &val); uint16_t result = spread_bits(val); printf("%u", result); return 0; }
Extract the target bit by shifting it down to bit position 0 (e.g., >> i).
>> i
Then add it to the result (by OR-ing) after shifting it to the target position (doubled).
Test Cases
Test Results
Input
202
Expected Output
20548