Why do the arrays index and other sequences start at zero?

Asked

Viewed 1,573 times

14

Why the array does not start with 1? There is some technical reason to have adopted 0?

1 answer

16


It has a mathematical basis. If the first natural number is zero, why computers should do it differently?

You may be thinking, "in math matrices begin at 1". Yes, but this runs away from the pattern that mathematics itself has set upon a more fundamental concept that begins with zero.

Nothing stops a person starting his matrix with zero index, it is almost a fluke that in school we learn to start with 1. Of course, it is a universal convention of mathematics, but that does not bring bad consequences if it is not adopted.

In computers it was possible to correct this and make the use more linear.

But there’s one more mathematical reason:

to a b base, the first b^N non-negative integer with exponent N is represented binarially by N digits in all situations, if starting with 0.

As demonstrated by a publication in the EIT.

Using base 2 which is the fundamental number of computation: 2^3 = 8. So the eighth number is:

  • 8 (binary: 1000) If you start counting from 1, you need 4 digits to represent exponent 3
  • 7 (binary: 111) if starting from 0, you need 3 digits to represent exponent 3

So computers, before languages, were built like this. I explain.

The memory has 2 N positions, where N is the number of bits supported. If you started from 1 to calculate positions you would have to add 1 to reach all positions, or do N + 1 to represent all possible positions, or make the last address inaccessible. Bad solutions. Starting with 0 solves this issue in a simple and elegant way.

If you think about it it makes sense that languages are like this because computers were built starting everything from 0.

Not all languages use this form. Some even allow each sequence to determine which number you want to start with. What makes it easy to create bugs when some sequences with different boundaries interact with each other. These languages are usually less efficient because there is an impedance between it and the computer.

C was one that used this form, and most languages follow what C does, even to facilitate interoperability with the open language of computing. C was created to be a Assembly portable and more readable. Had to do close to how the computer works.

Starting at 0 was important because the array actually is the address where the sequence starts, and the indexes are calculated based on that address (address + index * element size), so array[0] is array + 0. If I started at 1 I’d have to:

  • or the compiler enters a 1 subtraction in the index to find the desired element,
  • or leave a space without data in the first element.

Both are bad solutions, even more so in the old less powerful computers. That alone is a good reason to start with 0.

In C arrays are usually just pointers. And it is not known the size of it beforehand. So when will sweep the array, in general, do not need to do "size minus 1". In C it makes more sense to start from 0.

Setback

There’s a article by Edsger Dijkstra* who questions this. He thinks this option was a mistake.

Dijkstra talks about aesthetics. He considers it "prettier" to start with 1 and demonstrates why. Other people find it more beautiful to start from the 0. Aesthetics is based on opinions.


*Yes, one of the greatest computers of all time did not use computers :)

This response was very much based on a reply community wiki of OS.

  • 2
    • Let’s face it, whoever has an spelling like this doesn’t have to do paper on a computer/typewriter
  • Is zero natural or not? Some links (in English) that explain that it can be a matter of taste/subject easier to explain with or without, or ISO 80000-2: https://www.youtube.com/watch?v=7yXeX7ccq9Y&feature=youtu.be&t=250 https://math.stackexchange.com/questions/283/is-0-a-natural-number https://en.wikipedia.org/wiki/Natural_number

Browser other questions tagged

You are not signed in. Login or sign up in order to post.