68. Union Extract Bytes from a 32-bit Value

Back To All Submissions
Previous Submission
Next Submission

Code

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

typedef union {
    uint32_t value;
    uint8_t bytes[4];
} Register;
Register reg;
// Write logic here to extract bytes using union
void print_bytes(uint32_t input) {
    reg.value = input;
    // reg.bytes[0] = input& 0x00000011;
    // reg.bytes[1] = input& 0x00001100;
    // reg.bytes[2] = input& 0x00110000;
    // reg.bytes[3] = input& 0x11000000; 
    for(int i = 0; i <4;i++){
        printf("%d ", reg.bytes[i]);
    }
    // Your code here
}

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

Solving Approach

 

 

 

Was this helpful?
Upvote
Downvote