In: Computer Science
/*
* 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;
}
#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.