Question

In: Computer Science

/* * Change the value pointed to by ptr byte-by-byte so that when returned as an...

/*
* Change the value pointed to by ptr byte-by-byte so that when returned as an integer
* the value is 351351.
*
* Hint: Remember that an int is 4 bytes.
*
* Hint: Remember how little endian works for data storage, how is it different between an multiple bytes(int) and a single byte?
*
* Hint: It will be easiest to start convert 351351 into binary form and starting seeing how the endian works from there.
*
* ALLOWED:
* Pointer operators: *, &
* Binary integer operators: -, +, *
* Shorthand operators based on the above: ex. +=, *=, ++, --, etc.
* Unary integer operators: !
*
* DISALLOWED:
* Pointer operators: [] (Array Indexing Operator)
* Binary integer operators: &, &&, |, ||, <, >, <<, >>, ==, !=, ^, /, %
* Unary integer operators: ~, -
*/


int endianExperiment(int* ptr) {
char *bytePtr;
// Your code here
return *ptr;
}

Solutions

Expert Solution

#include <stdio.h>

/*
* Change the value pointed to by ptr byte-by-byte so that when returned as an integer
* the value is 351351.
*
* Hint: Remember that an int is 4 bytes.
*
* Hint: Remember how little endian works for data storage, how is it different between an multiple bytes(int) and a single byte?
*
* Hint: It will be easiest to start convert 351351 into binary form and starting seeing how the endian works from there.
*
* ALLOWED:
* Pointer operators: *, &
* Binary integer operators: -, +, *
* Shorthand operators based on the above: ex. +=, *=, ++, --, etc.
* Unary integer operators: !
*
* DISALLOWED:
* Pointer operators: [] (Array Indexing Operator)
* Binary integer operators: &, &&, |, ||, <, >, <<, >>, ==, !=, ^, /, %
* Unary integer operators: ~, -
*/


int endianExperiment(int* ptr) {
        char *bytePtr = (char *)ptr;

        // 351351 is 0x00055C77

        // In little endian, lower bytes stay at lower addresses
        *bytePtr = 0x77;
        bytePtr++;

        *bytePtr = 0x5C;
        bytePtr++;

        *bytePtr = 0x05;
        bytePtr++;
        
        *bytePtr = 0x00;

        // Your code here
        return *ptr;
}

int main(void) {
        int d = 0;

        printf("Value: %d\n", endianExperiment(&d));
        return 0;
}

**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.


Related Solutions

As pointed out in the U.K. parliament’s report on “Behaviour Change”, “Many of the goals to...
As pointed out in the U.K. parliament’s report on “Behaviour Change”, “Many of the goals to which governments aspire—such as bringing down levels of crime, reducing unemployment, increasing savings and meeting targets for carbon emissions—can be achieved only if people change their behaviour. Consequently, understanding how to change the behaviour of populations should be a concern for any government if it is to be successful.” Discuss, using examples. We are strongly influenced by what others do. Discuss how this might...
Derivation of the equation of change for the Reynolds stresses. At the end of S5.2 it was pointed out that there is an equation of change for the Reynolds stresses.
Derivation of the equation of change for the Reynolds stresses. At the end of S5.2 it was pointed out that there is an equation of change for the Reynolds stresses. This can be derived by (a) Multiplying the ith component of the vector form of Eq. 5.2-5 by vj and time smoothing, (b) Multiplying the jth component of the vector form of Eq. 5.2-5 by v’ and time smoothing, and (c) Adding the results of (a) and (b) Show that...
#include <stdlib.h> // rotate the values pointed to by three pointers // so values move from...
#include <stdlib.h> // rotate the values pointed to by three pointers // so values move from xp to yp, yp to zp and zp to xp void rotate(int *xp, int *yp, int *zp) { return; } // Write a function that returns 0 if x is 0, returns -1 // if x < 0, returns 1 if x > 0 // Your code must follow the Bit-Level Integer Coding Rules // on the textbook (located between hw 2.60 and 2.61)....
C program 1.// rotate the values pointed to by three pointers// so values move from xp...
C program 1.// rotate the values pointed to by three pointers// so values move from xp to yp, yp to zp and zp to xpvoid rotate(int *xp, int *yp, int *zp)​ { return;} 2.// Write a function that returns 0 if x is 0, returns -1// if x < 0, returns 1 if x > 0// Your code must follow the Bit-Level Integer Coding Rules// on the textbook (located between hw 2.60 and 2.61).// You can assume w = 32.//...
Question 1) Given the byte value: 0xad a) What is the equivalent decimal notation as an...
Question 1) Given the byte value: 0xad a) What is the equivalent decimal notation as an unsigned value? b_ What is the equivalent decimal notation as a signed value? Question 2) Below are integer values and the location where each is stored, which may be an address in memory or a register: Value    Location 0xc       0x130 0x82     0x134 0x5       %rdi 0x134    %rsi What are the values of the following operands? You may answer in decimal or hexadecimal, but if you...
DISCUSSION : We have pointed out that the value chain will indicate many support areas that...
DISCUSSION : We have pointed out that the value chain will indicate many support areas that could be candidates for outsourcing. These are not essential functions (accounting, human resources, training, support technology, facility maintenance, security, phone call center,etc.) for the company and perhaps could be or cheaper and better by an outside firm. a. Is this a case of "its just business" or is it unethical to outsource certain functions meaning you lay off long term employees that are specialized?...
When merchandise previously sold to a customer is returned, the journal entry is ...
When merchandise previously sold to a customer is returned, the journal entry is ...
When the migrants in the film are caught by immigration authorities and returned to El Salvador,...
When the migrants in the film are caught by immigration authorities and returned to El Salvador, what are some of the fears and dangerous associated with going back to their communities?
When the IRS defines value as "The amount at which property would change hands between a...
When the IRS defines value as "The amount at which property would change hands between a willing seller and a willing buyer when neither is acting under compulsion and when both have knowledge of the relevant facts" it is referring to the concept of "fair market value." a. True b. False Please give answer with detail explanation
the net realizable value of accounts receivable not change when you write off an uncollectible account...
the net realizable value of accounts receivable not change when you write off an uncollectible account using allowance method. Explain in detail.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT