/*
* Change the value pointed to by ptr byte-by-byte so that when returned as an
* integer the value is 351351.
*
* Hints: Recall that an int is 4 bytes and how little-endian works for
* multibyte memory storage. We suggest starting by converting 351351 into
* binary/hexadecimal.
*
* ALLOWED:
* Pointer operators: *, &
* Binary integer operators: -, +, *
* Shorthand operators based on the above: +=, *=, ++, --, etc.
* Unary integer operators: !
*
* DISALLOWED:
* Pointer operators: [] (Array Indexing Operator)
* Binary integer operators: &, &&, |, ||, <, >, <<, >>, ==, !=, ^, /, %
* Unary integer operators: ~, -
*/
I need to write a function following the requirements above. I can't use loops, or array indexing, and other stuff. I understand the endianness concept. So I have integer 351351. In hex it is 0x 00 05 5C 77
. So with the little endian machine, it would be stored as 77 5C 05 00
, right?
int endianExperiment(int * ptr) {
char * bytePtr;
// Your code here
// 0x 00 05 5C 77
int num = 0;
bytePtr = (char * ) ptr;
return num;
}
This is my function so far. I got the address of the pointer parameter in char (so only 2 bytes) then stored it in bytePtr. But I am stuck after. I tried several options, and nothing works. Please help.
Also, so with little-endian, the number is stored with a least significant digit at the lowest index in array. But to access it (address of the digit), is it still the lowest index? Or is it the index that contains the first byte? Where do I need to have pointer point to?