1
$\begingroup$

So I've been trying to understand endianess for the past couple of days but I'm not sure if I'm overthinking this or not and I don't have anyone I can ask to confirm things.

Here is how I look at things.. Say you have a defined value which represents the amount of pebbles there are in my city.

Decimal: ‭1193046‬

Hex: 0x123456

Binary: 0001 0010 0011 0100 0101 0110‬

These all carry the same weight but they're represented in various different ways, they all have one thing in common and that's that the most significant number is located all the way to the left due to how positional notation works. ‭ Which means that the most significant byte would be 0001 0010 and this has nothing to do with endianess afaik.

Now say we create an int variable and store the decimal representation in it, that means that the computer will allocate 4 bytes of space in memory.

Unassigned allocated memory

0 (0000 0000) - 1 (0000 0000) - 2 (0000 0000) - 3 (0000 0000)

What I'm about to explain next is based off of the fact that I think it's safe to assume that a CPU always reads left to right, no matter endianess, is that something that's safe to assume? Because endianess only dictates how the CPU decides to store the bytes from a given value, right?

This is a more visual representation of what I think is going on

ALLOCATE 4 Bytes
PLACE Bytes (LE/BE)

LE 0 (0000 0000) - 1 (0001 0010) - 2 (0011 0100‬) - 3 (0101 0110)

BE 0 (0000 0000) - 1 (0101 0110) - 2 (0011 0100‬) - 3 (0001 0010)

My theory is strongly based off of the fact that I think that the lowest memory address is always to the left, and that the CPU reads/writes bytes from left to right no matter it's architecture, however when it stores a value that's greater than 1 byte, it stores the most significant byte first or last based on it's endianess.

Am I super wrong here or do I kind of have a slight idea of what's going on?

$\endgroup$
3
  • 1
    $\begingroup$ Left and right are not appropriate terms to discuss storage order. Prefer decreasing/increasing addresses, together with least/most significant $\endgroup$
    – user16034
    Feb 6, 2023 at 15:40
  • $\begingroup$ "a CPU always reads left to right": this is immaterial, a CPU reads all bits at a time. $\endgroup$
    – user16034
    Feb 6, 2023 at 15:47
  • $\begingroup$ Your first example is not representative. Common processors support data types with a size that is a power of 2, so no data of length 24 bits will be written/read as a whole. $\endgroup$
    – user16034
    Feb 6, 2023 at 15:51

3 Answers 3

1
$\begingroup$

Memory is first split into bytes. Bytes are 8 or more bits with 256 or more values. (Octets are 8-bit bytes. Bytes can be larger). Bytes can only be read or written completely.

Programming languages and computer hardware use integers larger than 8 bits. For example 64 bit integers. You need eight 8-bit bytes to store a 64 bit integer.

So to read a 64 bit integer at address x, you must read eight bytes at addresses x to x+7. And these bytes must be combined to form a 64 bit value. If the bytes are named a, b, c, d, e, f, and g, then the value is a + b0x100 + . . . + g0x100000000000000.

There are 8! ways a to g could be assigned to these eight memory location. One way is called bigendian, one is called littleendian, and the others have no names and are not used in practice.

$\endgroup$
0
$\begingroup$

The 32-bit number is split into 4 8-bit groups (bytes):

00000000 00010010 00110100 01010110
hex: 00     12       34       56

In little-endian order 56 is stored at the lowest address, then 34, then 12, then 00. In big-endian order 00 is stored at the lowest address, then 12, then 34, then 56. So when you read them with the lowest address at the left, in little-endian you see 56 34 12 00 and in big-endian you see 00 12 34 56.

CPUs don't know about left or right, just lower and higher addresses. Lower address at the left just happens to be the way us humans write memory dumps, and MSB at the left just happens to be the way us humans write numbers.

$\endgroup$
0
$\begingroup$

It is important to know that endianness applies to multi-byte primitive data types. These occupy 2 (short), 4 (int, float) or 8 bytes (long log, double) and are stored as a whole.

There are two obvious ways to store these bytes: by increasing (little-endian) or decreasing weights (big-endian). The qualifier refers to the weight of the first byte (by increasing addresses).

Endiannes does not apply to data types such as strings or byte buffers, and the bytes themselves are not "reversed".

$\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.