69. Modify Bytes in a 32-bit Value Using Union

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 using union to modify byte[1] and byte[2]
void modify_and_print(uint32_t input, uint8_t b1, uint8_t b2) {
    // Your logic here
    reg.value = input;
    reg.bytes[1] = reg.bytes[1]& 0x00 | b1;
    reg.bytes[2] = reg.bytes[2] & 0x00 | b2;
    printf("%u", reg.value);
    // for(int i = 0; i <4;i++){
    //     printf("%d", reg.bytes[i]);
    // }
}

int main() {
    uint32_t num;
    uint8_t b1, b2;
    scanf("%u %hhu %hhu", &num, &b1, &b2);
    modify_and_print(num, b1, b2);
    return 0;
}

Solving Approach

 

 

 

Was this helpful?
Upvote
Downvote