Ask Question
12 September, 17:38

Suppose we number the bytes in a w-bit word from 0 (least significant) to w/8 - 1 (most significant). Write a code for the following C function, which will return an unsigned value in which byte i of argument x has been replaced by byte b:

unsigned replace_byte (unsigned x, int i, unsigned char b);

+3
Answers (1)
  1. 12 September, 18:02
    0
    Answer and Explanation:

    / / header file

    #include

    / / function to replace value

    unsigned replace_byte (unsigned x, int i, unsigned char b) {

    char * p = (char*) (&x);

    p[i] = b;

    return x;

    }

    int main () {

    / / declare variables

    unsigned n = 0x12345678;

    unsigned r1, r2;

    / / display result

    printf ("%x/n%x/n", replace_byte (n, 2,0xAB), replace_byte (n, 0,0xAB));

    }

    output

    12ab5678

    123456ab
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Suppose we number the bytes in a w-bit word from 0 (least significant) to w/8 - 1 (most significant). Write a code for the following C ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers