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) {
    //   printf("%d  %x %x %x ", ((input<<24)&0xff000000),
    //    ((input>>8)&0x0000ff00),
    // ((input<<8)&0x00ff0000),
    //   ((input>>24)&0x000000ff));
    // Your code here
    
Register reg;
    reg.value = input;

    // Print each byte
    printf("%d %d %d %d",
           reg.bytes[0], reg.bytes[1], reg.bytes[2], reg.bytes[3]);


}

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

Solving Approach

 

 

 

Loading...

Input

305419896

Expected Output

120 86 52 18