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
    Register out;
    // out.bytes[3]=input>>24 & 0xff;
    // out.bytes[2]=input>>16 & 0xff;
    // out.bytes[1]=input>>8 & 0xff;
    // out.bytes[0]=input>>0 & 0xff;
    out.value = input;
    for(int i=0;i<4;i++)
        printf("%d ",out.bytes[i]);
}

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