0
$\begingroup$

Address 0000 stores 0xde

0001 stores 0xad

0002 stores 0xbe

0003 stores 0xef

Treat the data stored as 2 16 bit integers, what are the two values stored?

I thought the answer would be 0xadde and 0xefbe but I was wrong. Why?

$\endgroup$
2
  • $\begingroup$ CS = Computer Science. Is this "science" in your opinion? $\endgroup$
    – gnasher729
    Sep 23, 2020 at 21:57
  • 1
    $\begingroup$ @gnasher729 This is a perfectly reasonable question about endianness, a basic concept in computer science. $\endgroup$ Sep 23, 2020 at 22:11

1 Answer 1

1
$\begingroup$

Let's program it and see what happens:

#include <stdio.h>

int main(int argc, char *argv[]) {
  unsigned char mem[4] = { 0xde, 0xad, 0xbe, 0xef };

  printf("%02hhx %02hhx %02hhx %02hhx\n", mem[0], mem[1], mem[2], mem[3]);
  printf("%04hx %04hx\n", *(unsigned short *)(mem), *(unsigned short *)(mem + 2));
}

Here is the output:

de ad be ef
adde efbe
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.