At some point, I was working on a deep packet inspection and database for storing network statistics. For the database, I needed a lightweight big numbers library, and I rolled out my own. The library was implemented as a single C header file with a bunch of inline functions.

Big numbers were unsigned integers, represented as a contiguous array of bytes (unsigned char *ptr, int len) in little-endian byte order (ptr points to the least significant byte). In addition to the arithmetic functions that add, divide and multiply big numbers, I wrote a function that prints a big number. It contained the snippet below with two loops. The first loop was dividing a number by a base, storing remainders in an array, until the number became zero with no remainder. The second loop stringified the number:

static const char *digits = "0123456789ABCDEF";
do {
res = div_int(tmp, len, &base, sizeof(base), &rem, sizeof(rem));
buf[i++] = digits[rem];
} while (res > 0);
while (i--) {
fputc(buf[i], fp);

What caught my attention was the fact that the second loop needed to traverse the array of remainders from the end to the beginning - backwards!

We write numbers in big endian order

Thinking about this more, I realised that we write numbers in big endian order: from most significant to the least significant digit. And that is unnatural, because in order to read a big number, we need to:

  1. jump to the end of a number
  2. scan right-to-left, counting groups of 3 digits, to figure out the order (thousands, millions)
  3. then, knowing the order, we can say the number

I would say, the more natural way would be to use little endian order: say (and write) least significant digits first, and most significant last. Then, reading would not require the backward scan from right to left.

Why do we write numbers backwards?

I don't know why we do it that way. My speculation is that we use Arabic numbers, and Arabs write from right to left. So, apparently Arabs naturally use the little endian order for numbers. For us in Europe it is backwards.

An interesting observation is that in some countries (e.g. in Germany) they pronounce small two-digit numbers in little endian, like "42" as "2 and 40".

To contact: send us a message or ask on the developer forum.