39

I'm having troubles wrapping my head on the two. I understand how to represent something in big endian.

For example -12 is 1111 1111 1111 0100

But why is the little endian representation 1111 0100 1111 1111 instead of 0100 1111 1111 1111?

Clinton Jooooones
  • 887
  • 3
  • 12
  • 19

2 Answers2

27

Endianness is about byte address order. Little endian means the lower significant bytes get the lower addresses. Big endian means the other way around. So it's about the bytes (8-bit chunks) not nibbles (4-bit chunks). Most computers we use (there are a few exceptions) address bytes at the individual address level.

Taking the -12 example:

Little endian, in memory, would be:

000000: F4
000001: FF

Big endian, in memory, would be:

000000: FF
000001: F4
lurker
  • 56,987
  • 9
  • 69
  • 103
  • How does it behave on stack. If the stack grows downwards, will it effect the behaviour ? – Krrish Raj Apr 12 '16 at 11:12
  • @KrrishRaj no, the fact that it's a stack isn't relevant if you are following the "rules" of address order in the definition of endianness. The stack doesn't change what I indicate in my answer. A stack normally grows "down" (when you push a value on the stack, it goes to the next lower address). So as you push bytes on a stack, for example, they have lower addresses. If, for example, you want to push `FF F4` onto the stack (the value -12 in 16-bit integers), and it is to be little endian, then you would push `FF` first followed by `F4` since that would put `FF` at the higher address. – lurker Apr 12 '16 at 11:17
  • On internet intel is said to be little endian. When I examine the stack using gdb, it is organised into 4 byte groups like 44 43 42 41,48 47 46 45 etc (while address of chunks are shown as 41,45 ie. lowest address in group). When I push a string "abcd", I get values as 6463261. Here I am confused, if it is a little-endian machine then I should get 61626364 only. What am I getting wrong ? – Krrish Raj Apr 12 '16 at 11:37
  • @KrrishRaj How did you "push a string"? It depends upon what instructions you used. A string doesn't have "endianness". Numbers do. – lurker Apr 12 '16 at 11:46
  • I am using a simple c program and taking input using gets and storing it in a char array. – Krrish Raj Apr 12 '16 at 11:52
  • @KrrishRaj In C, a string is an array of characters. An array is stored with the first element in the lowest address, followed by the next element in the next highest address, etc. If you are using 8-bit characters, then the string `"abcd"` will be stored with `'a'` at the first address, then `'b'` at the very next address, etc, which is what you see. The endianness does not apply to single byte values. – lurker Apr 12 '16 at 11:55
26

Little endian is basically reversing the byte order for a multi byte value.

1111 1111 1111 0100 is a 2 byte value where 1111 1111 is the first byte and 1111 0100 is the second byte. In little endian, the second byte (or least significant byte) is read in first so the final representation is 1111 0100 1111 1111.

Lightsout
  • 3,454
  • 2
  • 36
  • 65