Why is the index of arrays and other sequences starting at zero?

8

Why array does not start with 1. Is there any technical reason to have adopted 0?

    
asked by anonymous 13.02.2017 / 15:10

1 answer

9

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

You may be thinking: "Mathematics in matrices begins with 1". Yes, but that runs out of the standard that mathematics itself has established upon a more fundamental concept that begins with zero.

Nothing prevents a person from starting their matrix with zero index, it is almost a coincidence 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.

On computers, this could be fixed and made more linear.

But there is one more mathematical reason:

  

For a base b, the first b^N nonnegative integer with an exponent N is represented binary by N digits in all situations, if it starts with 0.

As a publication in the IETF demonstrates.

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 you start counting from 0, you need 3 digits to represent exponent 3

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

Memory has 2 ^ N positions, where N is the number of bits supported. If you start from 1 to calculate the positions you would have to add 1 to reach all positions, or do N + 1 to be able to represent all possible positions, or leave the last address unreachable. Bad solutions. Starting at 0 addresses this issue simply and elegantly.

If you think well it makes sense that languages are so because computers were built starting from zero.

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

C was one that used this form, and large parts of the languages follow what C does, even to facilitate interoperability with the lingua franca of computing. C was created to be a portable and more readable Assembly . It had to do close to how the computer works.

Starting at 0 was important because the array is actually the address where the sequence begins, and indexes are calculated based on that address (address + index * element size), then array[0] is array + 0 . If it started from 1 would have:

  • or the compiler enters a subtraction of 1 in the index to find the desired element,
  • or leave a blank space on the first element.

Both are bad solutions, especially on older, less powerful computers. This in itself is already a good reason to start with 0.

In C arrays are usually just pointers . And one does not know the size of it beforehand. So when you are going to scan the array you generally do not need to do "size minus 1". In C it makes more sense to start from 0.

Contrariety

There is a article by Edsger Dijkstra * question this. He thinks this option was a mistake.

The Dijkstra talks about aesthetics. He considers it "most beautiful" to begin with 1 and demonstrates why. Other people find it more beautiful to start with. Aesthetics is opinion based.

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

This response was heavily based on a community wiki response .

    
13.02.2017 / 15:10