What's up with zero indexing?

Chaotic42

Lifer
Jun 15, 2001
34,026
1,182
126
I don't understand it. Why is my first hard drive (hd0)? Why do some arrays call the first useful entry [0]? Why is my first CD drive scd0?

Counting starts at 1. I've never seen anyone, outside of the computing industry, write 0 as the first number. I've also never seen anyone count using 0 as the first number.

***

That's not 2 asterisks, it's 3. The last one is the third, not the second.

Just curious, as it doesn't make much sense to me.
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
I've never seen anyone, outside of the computing industry, write 0 as the first number.

Well this is IN the computer industry! I don't know the exact reason for starting at zero, but it makes sense. I guess it's just a matter of tradition. BTW, in linux, it's hda1, hda2, etc for hard drives, what is hd0?
 

Kilrsat

Golden Member
Jul 16, 2001
1,072
0
0
Because 0000 = 1 didn't make any sense.

let's say I want to represent 4 options:

00
01
10
11

Looks nice, I get away with only using 2 bits.

Now, what if we don't start at 0?
01
10
11

Uh oh, I ran out of options, so to represent 4 options without using 00, I need:
001
010
011
100

I just wasted a bit because I didn't use a potential option. Wasteful programming wasn't always an option like it is now. There was a point where using only the absolute minimum number of bits was what was needed. Hence all the logic foundations start at 0.
 

kranky

Elite Member
Oct 9, 1999
21,019
156
106
If you didn't start counting at zero, how could you have 0-day warez? ;)

Like Kilrsat said, you don't waste a value of zero. It's a big factor in why so many programming bugs are "off-by-one" errors.
 

glugglug

Diamond Member
Jun 9, 2002
5,340
1
81
It comes from doing pointer math.

Lets say you have an array of 10 ints in C, called x.

The memory location pointed to by x is your first array element. This is *(x+0) or x[0]
If this was referred to as x[1], the nice tricks where you can add to a pointer to change the index you're looking at wouldn't make sense.
x[5] is worked out by adding 5*sizeof(int) to pointer x and dereferencing it. If x[5] was the 5th element rather than the 6th, it would instead need to be evaluated by adding (5-1)*sizeof(int) to x then dereferencing. Rather than having to subtract 1 to correct the calculated addresses of array elements the arrays start at 0 instead of 1.

I know the above isn't exactly clear, can't figure out how to word it better right now.
 

Descartes

Lifer
Oct 10, 1999
13,968
2
0
The threshold of a given number base is always the base- 1. In other words, the maximum value for a single digit in base 10 in 9, 1 for base 2, etc.. The first digit is of course 0. If you started at 1, you wouldn't be able to permute enough representations for the entire base. If you have a base 5 and started at 1, you'd have a max value of 6. That doesn't make much sense, does it?

It's the same thing with indexes.
 

Chaotic42

Lifer
Jun 15, 2001
34,026
1,182
126
Originally posted by: BingBongWongFooey
I've never seen anyone, outside of the computing industry, write 0 as the first number.

Well this is IN the computer industry! I don't know the exact reason for starting at zero, but it makes sense. I guess it's just a matter of tradition. BTW, in linux, it's hda1, hda2, etc for hard drives, what is hd0?

hd0 is GRUB's name for my first hard drive.

Reading through the other posts, I see now why they start at 0. Thanks guys!