Bit Spreading Interleave Bits with Zeros

Code

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

uint16_t spread_bits(uint8_t val) {
    // Your logic here
    uint16_t result = val;


    //val = xy tách dữ liệu thành kiểu 0x0x0y
    result = (result|(result << 4))&0x0f0f;

  //0x0x0y => 0x0yx0x0z0m
    result = (result|(result << 2))&0x3333;

    result = (result| (result << 1))& 0x5555;

    return result;
}

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

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

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

202

Expected Output

20548