If I debug this C code:
unsigned int i = 0xFF;
Will the debugger show me in memory 0x00FF if it's a little endian system and 0xFF00 if it's a big endian system?
If I debug this C code:
unsigned int i = 0xFF;
Will the debugger show me in memory 0x00FF if it's a little endian system and 0xFF00 if it's a big endian system?
If you view the raw contents of memory around the address &i
then yes, you will be able to tell the difference. But it's going to be 00 00 00 ff
for big endian and ff 00 00 00
for little endian, assuming sizeof(unsigned int)
is 4 and CHAR_BIT
(number of bits per byte) is 8 -- in the question you have them swapped.
You can also detect endianness programmatically (i.e. you can make a program that prints out the endianness of the architecture it runs on) through one of several tricks; for example, see Detecting endianness programmatically in a C++ program (solutions apply to C too).
If:
char
s.unsigned int
sYou would see this at &i
if it's little-endian:
ff 00 ?? ?? ?? ?? ?? ??
and this if it's big-endian:
00 ff ?? ?? ?? ?? ?? ??
The question marks simply represent the bytes following i
in memory.
Note that:
unsigned int
. It's up to the implementation.CHAR_BIT * sizeof (unsigned int)
.If instead your machine uses 32-bit unsigned char
, you would see:
ff 00 00 00 ?? ?? ?? ??
in the little-endian case, and on a big-endian machine you would see:
00 00 00 ff ?? ?? ?? ??