Ask Question
11 September, 03:43

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. 11 September, 07:35
    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?
Get 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 there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers