4 ms·
So random question because I know I would get this wrong in an interview, but would a 0 based indexing system have fewer assembly language instructions to calcu
by rhacker 8y ago
So random question because I know I would get this wrong in an interview, but would a 0 based indexing system have fewer assembly language instructions to calculate a memory offset than a 1 based system?
- gizmo686 8y agoNaively, yes. In practice, you could treat them exactly the same and either decrement the physical pointer by 1 unit (so an index of 0 would underflow) or leave the first index unused or for metadata (eg. array length) There might be some other tricks you can play depending on your instruction set, but nothing comes to mind.
- ummonk 8y agoNope, not in most architectures. In a 0-based indexing system, the base pointer points to the first element of the array. In a 1-based indexing system, the base pointer points to the slot in memory that isone spot behind the first element in the array. This is how Julia (and Ada) does arbitrary indexing.