Modify Bytes in a 32-bit Value Using Union

Code

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

typedef union{
    uint32_t value;
    uint8_t byte[4];
}Reg;

void modify_reg32(Reg* register_, uint8_t b1, uint8_t b2){
    register_->byte[1] = b1; 
    register_->byte[2] = b2; 
}

int main(){
    Reg register_; 
    uint8_t b1, b2; 
    scanf("%u %hhu %hhu",&register_.value, &b1, &b2); 
    modify_reg32(&register_, b1, b2); 
    printf("%u", register_.value); 
    return 0;
} 

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

305419896 170 187

Expected Output

314288760