Ask Question
10 February, 14:58

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);

+1
Answers (1)
  1. 10 February, 15:47
    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 and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers