Bit Spreading Interleave Bits with Zeros

Code

// 12. Bit Spreading Interleave Bits with Zeros
// In some protocols or hardware applications (e.g. graphic rendering, signal encoding),
// bit spreading or interleaving is used to insert 0s between the bits of a value
// for purposes like data alignment or transmission.

// Input: Register 8 bit 
// Output: Register 16 bit, bit original occupies even position(0,2,4)


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

uint16_t encoding_register(uint8_t reg){
    uint8_t arr[16]={0}; 
    uint8_t count1 = 0; 
    for(int i=0; i<16; i=i+2){
        arr[i] = (reg>>count1) & 0x01;
        count1++; 
    }
    
    uint16_t x=0; 
    for(int i=0; i<=14; i= i+2)
    {
        x = x | ( (uint16_t)arr[i] << i);
    }
    return x; 
}

int main()
{
    uint8_t reg; 
    scanf("%hhu",&reg); 
    printf("%hu",encoding_register(reg)); 
    return 0;
    // 10 10 00 00 10 00 10 00
} 

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

202

Expected Output

20548