Bit Spreading Interleave Bits with Zeros

Code

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

// Function to spread bits of 8-bit value into even positions of 16-bit value
uint16_t spreadBits(uint8_t val) {
    uint16_t result = 0;
        for (int i = 0; i < 8; i++) {
                if (val & (1 << i)) {
                            result |= (1 << (i * 2));  // Place original bit at even positions
                                    }
                                        }
                                            return result;
                                            }

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

                                                        uint16_t spread = spreadBits(val);
                                                            printf("%u\n", spread);  // Print decimal value of spread bits

                                                                return 0;
                                                                }

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

202

Expected Output

20548