All submissions

Modify Bytes in a 32-bit Value Using Union

Code

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

typedef union {
    uint32_t value;
    uint8_t bytes[4];
} Register;

// 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) {
    /*Clear byte[0] FF00*/
    input &= ~0xFF00;
    /*Clear byte[1] */
    input &= ~0xFF0000;

    /*Set new value to byte[0]*/
    input |= (b1 << 8);
    /*Set new value to byte[1]*/
    input |= (b2 << 16);

    printf("%lu", input);
}

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

 

 

 

Loading...

Input

305419896 170 187

Expected Output

314288760