Union Extract Bytes from a 32-bit Value

Code

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

typedef union {
    uint32_t value;
    uint8_t bytes[4];
} Register;

// Write logic here to extract bytes using union
void print_bytes(uint32_t input) {
    // Your code here

    uint8_t b0 = (input >> 0) & 0XFF;
    uint8_t b1 = (input >> 8) & 0xFF;
    uint8_t b2 = (input >> 16) & 0xFF;
    uint8_t b3 = (input >> 24) & 0xFF;

    printf("%d ",b0);
    printf("%d ",b1);
    printf("%d ",b2);
    printf("%d ",b3);

}

int main() {
    uint32_t num;
    scanf("%u", &num);
    print_bytes(num);
    return 0;
}

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

305419896

Expected Output

120 86 52 18