Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

For my MSc thesis I'm programming some stuff for the Cell B.E. processor. It provides AltiVec vector instructions which allow quick parallel computation. It's great for getting nice speed ups, but it requires a lot of thought and bit twiddling from time to time.

It has two C "intrinsics" called vec_mule() and vec_mulo(). These take in two vectors of integer datatypes (char, short) for multiplication and produces one vector with items that are twice as big. char becomes short, short becomes long. To make room for these larger datatypes it only multiplies the even (vec_mule) or the odd (vec_mulo) items.

It's a nice construction, until you start to wonder which elements are even. Is "element 0" even, or is the "first element" odd? Should I really have to wonder about that?

I have never questioned the utter coolness of starting at index 0, until I encountered this. I do not know why we start at 0 if the rest of the world starts at 1, but it seems silly and without reason.




Well, the rest of the world also starts at 0:

1 sheep -> the number of sheep present when there is one

0 sheep -> the number of sheep present when there are none

Because you normally only enumerate things that are there it seems like you are starting at '1', but really you start relative to the situation where there are 'none'.

You can't have 'negative' presence of anything so it took a while longer to realize that there might be negative numbers too.

As for element 0 being odd or even, it's even.

So, it isn't silly and it has a reason.

0 is one of the biggest inventions in the history of mankind, programmers have given it it's natural place because we could not make our computers work if we didn't do that.

Imagine binary logic based on '1' and '2'....


Er, that's all worng. The question isn't number of, but index of. The index of the first element in a collection is 0. The index of the first sheep in a flock is (conventionally) 1. The total elements up to index n (inclusive) is n+1 -- the opposite parity -- which can be initially confusing.


An index is a label, a convenient way of finding something when you remember where you left it. You can start your indexing at any offset, you don't even need to use numbers (you could for instance look at an associative array where you store stuff based on a hash of some value).

index = position, label

The count is the number of occupied slots and is totally independent of the positions or labels.

Otherwise how would you count the number of elements in a sparse matrix ?


In this particular case though, where you have operations depending on evenness/oddness of some attribute related to position in a set (either position by index, or count) and the index starts with 0, there is absolutely confusion to be had over which implementation would be used.

Your logic is sound, but it's counter-intuitive to think of Obj[2] as a member of 'mulo', an odd number, "because 2 is really the 3rd element". A person implementing the system could be logical by your ideal (making 2 odd) or logical by the ideal of common sense and readability (making 2 even). Either could validly hold in a person's mind.

Given the ambiguity, I think it just shows that you shouldn't base operations on oddness/evenness, or should rigidly and obviously define them if you must use them.


> Well, the rest of the world also starts at 0

Yes, they do, when they have no elements. But if an array has an element 0, it actually has 1 element, not 0. Whereas if I have a sheep 1, I have 1 sheep. There is no sheep 0.

I think it would be good to note that I have nothing against 0 as a number. It's useful for many purposes including to indicate that a set actually has 0 elements, but I'm not so sure it's such a good idea to actually call the first element element 0.


You are confusing 'rank' with 'count'.


Not that I know of. Normal people and programmers do not count the same way.

        | last |
        | rank | count
  ------+------+-------
  sheep |  2   |   2
  sheep |  1   |   1
  sheep |  -   |   0
  ------+------+-------
  array |  1   |   2
  array |  0   |   1
  array |  -   |   0


Programmers are pretty normal people :)

When counting sheep in a field I'll count 1 sheep, 2 sheep etc.

When counting variables I'll say one variable, two variables and so on.

No difference there.

When placing items in an array I say this value goes in to slot '0', this will go into slot '1'. The slots are labelled with their index. And I will say there are now two values stored in the array, one at 'position' 0 and one at 'position' 1.

I think the whole problem disappears when you think of 'rank' as labels and count as the actual number of elements.

The first sheep is just a label you stick on a sheep to identify it, that makes it different from the other sheep.




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: