Why Computer start counting from 0 and Humans don't

Why Computer start counting from 0 and Humans don't

It's quite annoying how often I heard this joke even before I studied and how funny usually the response of participants of the talk were. It comes in all different shapes and forms, but normally involves someone asking the “jokester” to count to a certain number. Imagine the following scenario:


Person 1: Let's play a Game! We need 5 groups, so everyone repeat the number of the person before you and add 1, until you reach 5 and then start from the beginning.

So far so good, the group starts counting and dividing into groups. When the 5th number is reached this happens:

Thomas: 0! (surprisingly there is a second joke packed into this little number, if you use the German translation for Zero - Null = undefined, but more to that in a different post)


But what exactly happened?

The instruction to split into groups follows certain rules that are more or less explained at the beginning.

  • 5 Groups → Count up to 5
  • Add +1 to the number of the person before you

Some rules, based on experience and sheer logic are not explained:

  • Speak out Loud
  • Remember your number
  • Start from the beginning, where Beginning is equal to 1

This little condition is the reason for a bad joke that won't stop from existing and always only the computer enthusiasts find funny. For the everyday person the Beginning of Counting starts as 1, while 0 represents nothing.

Computers start counting from 0, whereby this describes the first index of memory address. The reason for this is mathematics.

Too fast? Have a look at the following image.

(very simple) representation of memory address with index starting at 0

We have numbers of 0-9 whereby this is the index or absolute position starting from 0. The number above starting from 22 and incrementing by 1 is the memory address. The computer can save information at memory address 22 and retrieve it by the same address again. Now imagine the computer want's to get access to the 6th element at address 227.

Don't get confused by the 6th element and the number 5. Remember we're starting from 0 and therefore the 6th element is actually the 5th if you would start from 1.

It uses the following formula:

(base address of the array) + (index of element * size of each element)

If we apply the formula to our current scenario with starting value 0:

22 + (5 ×1) = 27 ✅

Let's assume computers would start counting at 1. Have a look at the image below.

representation of memory address with index starting at 1

And now we apply the same formula from before:

22 + (6 × 1) = 28 ❌

We missed the index by 1.

Starting by 0 therefore simplifies the reference of addresses and the calculation for pointer. Pointers are interesting and unique itself, but too much for today's topic. Just think as them of things that represent memory address, and you can calculate with them like you used in school (add, subtract, multiply …).

If we transform our scenario into natural language we have to convert, as we start from index 0, but when someone asks us “how many values” are in the list we say 10, instead of 9 (highest index).

💡
Early programming languages FORTRAN and Cobol used 1-based indexing, but were later superset by C, embracing zero-based indexing for efficient memory addressing and pointer arithmetic.

Besides mathematics our natural language and byte code differs in the way of handling numbers itself. Our language mostly focuses on positional numbers (ordinal) first, second, and third. Computers see them as quantity (cardinal) one, two, three.

This is not a fixed law just think about the sentence: I have 5 apples. In terms of counting that's quantity, but we still start from one. “5 apples”: We say “I have 5 apples”, but we're implicitly thinking about a set where each apple has a position. There's an unspoken “first apple,” “second apple,” and so on. Before we dive into natural languages too much I'll sum up.

Conclusion:

  • Computer start counting from 0 as it is more efficient for memory address calculation and pointer arithmetic.
  • Natural language tends to start from 1 as we use more positional numbers.