6 ms·
Because otherwise you would be wasting a perfectly good number for no reason, which means you need to use more bits to do the same thing. To write 4 numbers (i
by diego_sandoval 4y ago
Because otherwise you would be wasting a perfectly good number for no reason, which means you need to use more bits to do the same thing.
To write 4 numbers (including zero) you only need two bits
0: 00
1: 01
2: 10
3: 11
To write 4 numbers if you avoid using the number zero, you need three bits
1: 001
2: 010
3: 011
4: 100
If you extrapolate that a little bit, you'll realize that you'll need two bytes (1 Byte + 1 bit from another byte) to store the indices of an array with 2^8 elements, which is just dumb.
Some of you might be thinking: you don't need to store it the same way it's written, you can just substract 1 from whatever the user typed and convert it behind the scenes.
Yes, you could subtract 1, but then you would be making the whole system more complex, opaque, and unelegant, while hiding information from the programmer for no good reason.
- divbzero 4y agoThe same applies to counting in other bases too. For instance, in 1-indexed counting grids for kids, the last column always feels out of place. 0-indexed decimal grid: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 1-indexed decimal grid: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
- anigbrowl 4y agoAre you sure it doesn't just look that way because you are used to monospaced fonts? Ask a kid to show you their zeroth finger.
- marak830 4y agoI asked my son to show me zero fingers(after show me 2 fingers and 5 fingers), he closed his fists.
- anigbrowl 4y agoMy point exactly - there isn't an identifiable zeroth finger.
- gitonthescene 4y agoAnd? My solution? Let the kid grow up and learn.
- anigbrowl 4y ago
- pygy_ 4y agoAnecdotally, I taught my children numbers starting from zero with a similar routine, based on cardinality. They immediately got it. My kids' school also uses zero-based tables. "Natural" is a very loaded and ambiguous term, best avoided if you hope to have sane discussions.
- anigbrowl 4y agoTeaching kids about zero I have no issue with. I do not believe for a second that you taught them to count by holding up one finger for zero, two fingers for one etc. Reminder that we're specifically talking about array indexing.
- lokedhs 4y agoThe index is the distance away from the first element. So index 0 is the first element. Index 3 is 3 away from the first element, so the fourth element. It's the difference between ordinal and cardinal numbers. In other words, counting and indexing.
- TOGoS 4y agoAs a kid, I saw these things often on multiple-choice test sheets. The tens digit at the end of each row not matching that at the beginning always seemed awkward, and I wondered why they didn't just start at zero. This was long before I did any programming. The first year CE being "1" resulting in the new millennium starting at 2001 instead of 2000 also seemed idiotic, and the 1900s being referred to as "the 20th century" was something I always had to consciously compensate for. Numbering items starting at zero would have given us more elegant/less confusing ways to communicate those things. Zero-based indexing makes things inherently simpler because it matches the way we write numbers (and becomes much more noticeable once you have more than one digit). It's not just an optimization for computers.
- adolph 4y agoWell, given that year numbering system's start in Europe, zero wasn't as broadly accepted as other digits at the time. By AD 150, Ptolemy, influenced by Hipparchus and the Babylonians, was using a symbol for zero. https://en.wikipedia.org/wiki/0#History https://en.wikipedia.org/wiki/0#History To keep it confusing: the traditional proleptic Gregorian calendar (like the Julian calendar) does not have a year 0 and instead uses the ordinal numbers 1, 2, ... both for years AD and BC. Thus the traditional time line is 2 BC, 1 BC, AD 1, and AD 2. ISO 8601 uses astronomical year numbering which includes a year 0 and negative numbers before it. Thus the ISO 8601 time line is −0001, 0000, 0001, and 0002. https://en.wikipedia.org/wiki/Gregorian_calendar#Dual_dating https://en.wikipedia.org/wiki/Gregorian_calendar#Dual_dating
- karamanolev 4y agoCounting fingers (or other items) isn't a positional numeral system, so it's really apples to oranges. Also, "first" has a meaning of "nothing precedes it", which is separate from the indexing system", it shouldn't mean "at index 1", unless otherwise specified.
- polyamid23 4y agoThough the idea of an index as a position instead of an address is weird to me, you can indeed make that analogy. The "first" element of an array has the index zero, as in "zero elements precede it".
- IshKebab 4y ago> Ask a kid to show you their zeroth finger. They will get confused because every day counting got it wrong. In a perfect world we would count fingers from 0.
- talaketu 4y agocounting grid? What even are these?
- quickthrower2 4y agoNeither looks like the canonical choice to me. They are both just as good.
- deleted 4y ago[deleted]
- deleted 4y ago[deleted]
- woojoo666 4y agoI like how the article summarizes this idea: > These all point to one reason why 0-indexing might be preferred: it matches machine semantics more
- kergonath 4y agoBut then of course the reason why 1-indexing is better (or really, arbitrary indexing, which is even better) is that it matches human semantics better.
- jbaczuk 4y agoYeah, but then couldn't the compiler just swap the 1 with a 0 and 256 with 255?
- yarskegg 4y agoThat gets really dicey if you're storing the value of the index in a variable. Does the programmer need to know that if it's an INDEX they're storing that goes from 1-256 they can fit that into an 8-bit value because the compiler will magic away that last bit? Speaking of which, how will the compiler know which 1's are really 0's and which 256's are really 255's? Will it compile that 1+1=1 if I use that result as an index? Just having a 0th index is much simpler than all of that noise.
- Sirened 4y agonot really? It just changes how the compiler emits the x := *(addr + idx) operation where instead of doing something like ldr x, [addr, idx] it just does sub t1, idx, #1 ldr x, [addr, t1] If we're talking about C, accessing invalid/OOB indices is undefined and so if idx happens to be zero and unsigned, we'll overflow and hit something unexpected, which is fine.
- jbaczuk 4y agoThat's what I was thinking too, except that they have a point. You might end up using more bytes to store indexes as variables (if the index is 256 for example), than you would with zero indexing.
- twobitshifter 4y agoSomeone linked to your johnny decimal system earlier today. https://johnnydecimal.com/ https://johnnydecimal.com/ I liked the idea but I noticed that it didn’t seem to be 0 indexed. If I adopt this i’m definitely going for zero indexing.
- tmvst 4y agoZero is reserved for meta stuff: https://johnnydecimal.com/concepts/what-about-00-09/ https://johnnydecimal.com/concepts/what-about-00-09/
- nicoburns 4y agoYou could have a 1 based type that doesn’t include 0. Or make zero the max value.
- quickthrower2 4y agoFron the article: > It's not the reason you think. No, it's not that reason either. Indexing is a language level concern, it has nothing to do with the generated code. Unless you write machine code I guess.
- Tao3300 4y ago> Indexing is a language level concern IIRC, Visual Basic 6 took a why not both? approach to array indexing.