All submissions

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 temp;
    temp.value=input;
    for(int i=0;i<4;i++)
    {
        printf("%d ",(int)temp.bytes[i]);
    }
}

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

Solving Approach

1:  Declare a variable of Register like Register temp.

2: Access 32 bit variable using dot operator as mentioned below.

  temp.value=input;

3: Use a for loop and access 8 bytes in each iteration as mentioned above because there are four 8bit variables which combines to 32 bits.

 

 

Loading...

Input

305419896

Expected Output

120 86 52 18