# What's the big deal? 0 vs 1 based indexing

**URL:** <https://discourse.julialang.org/t/whats-the-big-deal-0-vs-1-based-indexing/1102>\
**Category:** General Usage\
**Created:** [December 22, 2016, 2:20pm UTC](https://discourse.julialang.org/t/whats-the-big-deal-0-vs-1-based-indexing/1102 "2016-12-22T14:20:49Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![pyrex41](https://avatars.discourse-cdn.com/v4/letter/p/4bbf92/32.png) [@pyrex41](https://discourse.julialang.org/u/pyrex41)\
**Post date:** [December 22, 2016, 2:20pm UTC](https://discourse.julialang.org/t/whats-the-big-deal-0-vs-1-based-indexing/1102/1 "2016-12-22T14:20:49Z")

</div>

As an engineer coming from a non CS / EE background, I have no strong preference or use for 0 based indexing. In fact, 1 based indexing seems incredibly intuitive to me when I use Julia to work with data and manipulate arrays with loops, etc. for me, this aspect of Julia is a big plus. I find lots of hate though on the web for Julia on this point. Why? Is it just a question of familiarity / code comparability, or are their underlying reasons I am missing?

---

<div class="post-metadata">

**Author:** ![ndbecker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ndbecker/32/10829_2.png) [@ndbecker](https://discourse.julialang.org/u/ndbecker)\
**Post date:** [December 22, 2016, 2:36pm UTC](https://discourse.julialang.org/t/whats-the-big-deal-0-vs-1-based-indexing/1102/2 "2016-12-22T14:36:21Z")

</div>

I found since moving away from 1-based indexing (FORTRAN) I much less often needed to worry about +1 or -1 - I don’t think it’s entirely arbitrary. I use python/numpy, and it seems almost ideal. The only defect is:  
array[start:-end]  
There is a anomaly when end==0. [:-1] will give all but the last element, but [:-0] == [:0] is an empty array. This has occasionally bitten me (when end is a computed variable, and just happens to end up turning into -0).

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [December 22, 2016, 3:41pm UTC](https://discourse.julialang.org/t/whats-the-big-deal-0-vs-1-based-indexing/1102/3 "2016-12-22T15:41:44Z")

</div>

There is no big deal. [Julia has both, it makes 0-based or arbitrary a little more complex, if you want to support all ways. If you know you only want 1-based, just use 1:end, eitherwise eachindex(my\_array) is a little better.]

Programmers make a big dea lof this. All CPUs have a linear address space that starts at 0 [in the past, and on microcontrollers can be more complex…].

In ordinary code you never want to access location 0…, yes, you can also define start of arrays as 0.

This was mostly an issue for eary (simple) compilers. The extra implied -1 you have to do, can and will be optimized away. [Are there any exceptions to that? I think not. At least would be outside of loops, so not slow down.]

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [December 22, 2016, 4:36pm UTC](https://discourse.julialang.org/t/whats-the-big-deal-0-vs-1-based-indexing/1102/4 "2016-12-22T16:36:34Z")

</div>

1-based indexing is a big deal for same reason most other fad topics are a big deal: it’s such a simple idea that everyone can have an opinion on it, and everyone seems to think they can “help” by telling their personal experience about how this arbitrary choice has affected them at one time in their life.

---

<div class="post-metadata">

**Author:** ![ihnorton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihnorton/32/26_2.png) [@ihnorton](https://discourse.julialang.org/u/ihnorton)\
**Post date:** [December 22, 2016, 6:41pm UTC](https://discourse.julialang.org/t/whats-the-big-deal-0-vs-1-based-indexing/1102/5 "2016-12-22T18:41:23Z")

</div>

One interesting kernel, sometimes lost in the flamewars, is that the choice comes down to a preference for counting (1-based) versus offsets (0-based). Mathematically-focused languages understandably care most about counting, so the natural choice is 1-based (Fortran, Matlab, etc.). System languages dealing directly with memory tend to care most about offsets, so have usually been 0-based. Obviously there are counter-examples, but that’s the best explanation I’ve heard.

Also worth pointing out that many people have been working hard on abstractions for Julia that allow considerable flexibility in this area, see: [Generalizing AbstractArrays: opportunities and challenges](http://julialang.org/blog/2016/03/arrays-iteration)

---

<div class="post-metadata">

**Author:** ![Steven\_Sagaert](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steven_sagaert/32/29578_2.png) [@Steven\_Sagaert](https://discourse.julialang.org/u/Steven_Sagaert)\
**Post date:** [December 23, 2016, 10:05am UTC](https://discourse.julialang.org/t/whats-the-big-deal-0-vs-1-based-indexing/1102/6 "2016-12-23T10:05:38Z")

</div>

That’s exactly it.  
1-based indexing is actual indexing like in mathematics, while 0-based “indexing” isn’t indexing at all but pointer arithmetic. This comes from C where an array is just syntactic sugar for a pointer.

---

<div class="post-metadata">

**Author:** ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)\
**Post date:** [December 23, 2016, 4:18pm UTC](https://discourse.julialang.org/t/whats-the-big-deal-0-vs-1-based-indexing/1102/7 "2016-12-23T16:18:33Z")

</div>

Instead of flaming, what would be best IMO is to simply have the flexibility to handle arbitrary bases, and also arbitrary ordering, which would help a lot interfacing to C/C++/Java/etc. that all have 0-based, row-major memory layouts.  
Tim Holy has recently done some great stuff to add that flexibility, with OffsetArrays and PermutedDims, but I think it would be much nicer if that flexibility were integrated into the base Array types in Julia (as Fortran 90 and later have for arbitrary offsets).

---

<div class="post-metadata">

**Author:** ![waldyrious](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/waldyrious/32/80_2.png) [@waldyrious](https://discourse.julialang.org/u/waldyrious)\
**Post date:** [December 23, 2016, 4:58pm UTC](https://discourse.julialang.org/t/whats-the-big-deal-0-vs-1-based-indexing/1102/8 "2016-12-23T16:58:15Z")

</div>

> [@ihnorton](#):
>
> the choice comes down to a preference for counting (1-based) versus offsets (0-based)

This is a simple distinction but so easily overlooked, perhaps due to the terms used for these notions both in programming and in general language. I recently came across a post that explores this issue from a beginner’s point of view (i.e. someone who hasn’t considered this semantic distinction in mathematical terms, which was certainly my case). I think it explains the issue quite well: [Learning How to Count (Avoiding The Fencepost Problem) – BetterExplained](https://betterexplained.com/articles/learning-how-to-count-avoiding-the-fencepost-problem/). Wikipedia also describes the “fencepost problem” in [Off-by-one error - Wikipedia](https://en.wikipedia.org/wiki/Off-by-one_error#Fencepost_error).

---

<div class="post-metadata">

**Author:** ![ino76](https://avatars.discourse-cdn.com/v4/letter/i/9d8465/32.png) [@ino76](https://discourse.julialang.org/u/ino76)\
**Post date:** [May 9, 2019, 12:56pm UTC](https://discourse.julialang.org/t/whats-the-big-deal-0-vs-1-based-indexing/1102/9 "2019-05-09T12:56:01Z")

</div>

Exactly. I like it too.  
Personally I think that 0based indexing is one of many mistakes of programming. Because everyone in real world counts from one, 1based is intuitive and saves your intellectual capacity on more important things than doint (+1) constantly in your mind. 0based is more error prone …

---

<div class="post-metadata">

**Author:** ![ThePC007](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thepc007/32/22996_2.png) [@ThePC007](https://discourse.julialang.org/u/ThePC007)\
**Post date:** [March 16, 2021, 10:25pm UTC](https://discourse.julialang.org/t/whats-the-big-deal-0-vs-1-based-indexing/1102/10 "2021-03-16T22:25:45Z")

</div>

0-based indexing actually becomes very natural when you program for long enough and doesn’t use any intellectual capacity at all. In fact, when working with 1-based languages I have to constantly remind myself that they are 1-based and keep messing things up even when I’m actively thinking about it (talk about error-prone).

I think 0-based indexing makes quite a lot of sense in many scenarios, especially when dealing with algorithms and such (getting the root of an array-based binary tree works very easily by dividing by 2, but only on a 0-based array, spatial partitioning becomes easier as well, etc.), not to mention actually working with memory, where the index is really just an offset and adding a 1 to it would only serve to make things messy.

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [March 16, 2021, 11:10pm UTC](https://discourse.julialang.org/t/whats-the-big-deal-0-vs-1-based-indexing/1102/11 "2021-03-16T23:10:44Z")

</div>

It’s just a convention and there isn’t much difference either way. I agree with Dijkstra about 0-based indexing being (slightly) superior, but thankfully Julia allows indexing with an arbitrary base:

```julia
julia> using Polynomials

julia> p = Polynomial([3, 0, 2])
Polynomial(3 + 2*x^2)

julia> p[0]
3

```

---

<div class="post-metadata">

**Author:** ![Yifan\_Liu](https://avatars.discourse-cdn.com/v4/letter/y/4da419/32.png) [@Yifan\_Liu](https://discourse.julialang.org/u/Yifan_Liu)\
**Post date:** [March 17, 2021, 12:28am UTC](https://discourse.julialang.org/t/whats-the-big-deal-0-vs-1-based-indexing/1102/12 "2021-03-17T00:28:42Z")

</div>

I feel exactly the opposite. 0-based indexing is a huge mental burden to me.

---

<div class="post-metadata">

**Author:** ![spaceLem](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/spacelem/32/217628_2.png) [@spaceLem](https://discourse.julialang.org/u/spaceLem)\
**Post date:** [March 17, 2021, 1:00am UTC](https://discourse.julialang.org/t/whats-the-big-deal-0-vs-1-based-indexing/1102/13 "2021-03-17T01:00:44Z")

</div>

I found C++ slightly easier to work in when using 0-based indexing, but then I was using n-dim arrays that I was hand indexing.

Pretty much every other programming language I’ve encountered and worked in as a mathematician is 1-based (R, MatLab, Octave, Fortran, Maple, and Maxima) has provided tools for working smoothly with n-dim arrays, and with that in place I find 1-based much more natural and less to think about. The first item is 1, the 2nd 2, the n-th n… in my mind the 0th thing in a collection is when you have an empty set so the thing doesn’t actually exist.

I guess if you’re counting counts then you might need the number of times 0 happened, but then you might want the number of times a thing happened in the years 1967-2021, then you’re out of luck either way (except that Julia handily gives you a solution to that).

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [March 17, 2021, 7:04am UTC](https://discourse.julialang.org/t/whats-the-big-deal-0-vs-1-based-indexing/1102/14 "2021-03-17T07:04:13Z")

</div>

Hi folks. Thanks for the input, but this has been endlessly discussed, obviously isn’t going to change at this point and there’s no purpose in reviving a five year old thread.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [March 17, 2021, 7:04am UTC](https://discourse.julialang.org/t/whats-the-big-deal-0-vs-1-based-indexing/1102/15 "2021-03-17T07:04:17Z")

</div>


