Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. [Solved] Why Do Computer Counts from Zero.

[Solved] Why Do Computer Counts from Zero.

Scheduled Pinned Locked Moved The Weird and The Wonderful
comdata-structuresperformance
26 Posts 13 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kburman6
    wrote on last edited by
    #1

    Link to the article For More detail explanation(PDF) Counting arrays from 0 simplifies the computation of the memory address of each element. If an array is stored at a given position in memory (it’s called the address) the position of each element can be computed as

    element(n) = address + n * size_of_the_element
    //If you consider the first element the first, the computation becomes

    element(n) = address + (n-1) * size_of_the_element
    //Not a huge difference but it adds an unnecessary subtraction for each access.

    M C K J D 6 Replies Last reply
    0
    • K kburman6

      Link to the article For More detail explanation(PDF) Counting arrays from 0 simplifies the computation of the memory address of each element. If an array is stored at a given position in memory (it’s called the address) the position of each element can be computed as

      element(n) = address + n * size_of_the_element
      //If you consider the first element the first, the computation becomes

      element(n) = address + (n-1) * size_of_the_element
      //Not a huge difference but it adds an unnecessary subtraction for each access.

      M Offline
      M Offline
      Marco Bertschi
      wrote on last edited by
      #2

      kburman6 wrote:

      Not a huge difference but it adds an unnecessary subtraction for each access.

      And because unnecessary calcs are slowing down the computers' speed it was just logical to do it as simple as possible. Developer's course at my school, lesson 1 (or two, however; It was at the very beginning when they explained it to us).

      cheers Marco Bertschi


      Software Developer Twitter | Facebook | Articles


      You have absolutely no idea how glad I am that I have no idea at all. - OriginalGriff

      T K D 3 Replies Last reply
      0
      • M Marco Bertschi

        kburman6 wrote:

        Not a huge difference but it adds an unnecessary subtraction for each access.

        And because unnecessary calcs are slowing down the computers' speed it was just logical to do it as simple as possible. Developer's course at my school, lesson 1 (or two, however; It was at the very beginning when they explained it to us).

        cheers Marco Bertschi


        Software Developer Twitter | Facebook | Articles


        You have absolutely no idea how glad I am that I have no idea at all. - OriginalGriff

        T Offline
        T Offline
        Thomas Daniels
        wrote on last edited by
        #3

        Marco Bertschi wrote:

        lesson 1 0

        FTFY!

        The quick red ProgramFOX jumps right over the Lazy<Dog>. My latest article: Understand how bitwise operators work (C# and VB.NET examples) My group: C# Programmers Group

        1 Reply Last reply
        0
        • M Marco Bertschi

          kburman6 wrote:

          Not a huge difference but it adds an unnecessary subtraction for each access.

          And because unnecessary calcs are slowing down the computers' speed it was just logical to do it as simple as possible. Developer's course at my school, lesson 1 (or two, however; It was at the very beginning when they explained it to us).

          cheers Marco Bertschi


          Software Developer Twitter | Facebook | Articles


          You have absolutely no idea how glad I am that I have no idea at all. - OriginalGriff

          D Offline
          D Offline
          dan sh
          wrote on last edited by
          #4

          But for lesser mortals like me, who did Mechanical engineering and still ended up here, this is news. Besides, the first book I ever read about computers was to learn C++ programming. At that time, I had no clue that the black screen where I type code is console. I had to literally ask someone to open that black screen for me so I can code. I was probably wrong with my approach but I am still better than lots of those urgentzz codezz plzzz types I guess.

          "Bastards encourage idiots to use Oracle Forms, Web Forms, Access and a number of other dinky web publishing tolls.", Mycroft Holmes[^]

          K M 2 Replies Last reply
          0
          • M Marco Bertschi

            kburman6 wrote:

            Not a huge difference but it adds an unnecessary subtraction for each access.

            And because unnecessary calcs are slowing down the computers' speed it was just logical to do it as simple as possible. Developer's course at my school, lesson 1 (or two, however; It was at the very beginning when they explained it to us).

            cheers Marco Bertschi


            Software Developer Twitter | Facebook | Articles


            You have absolutely no idea how glad I am that I have no idea at all. - OriginalGriff

            K Offline
            K Offline
            kburman6
            wrote on last edited by
            #5

            It is just pointless to add one more calculation step without any reason. :)

            M OriginalGriffO 2 Replies Last reply
            0
            • D dan sh

              But for lesser mortals like me, who did Mechanical engineering and still ended up here, this is news. Besides, the first book I ever read about computers was to learn C++ programming. At that time, I had no clue that the black screen where I type code is console. I had to literally ask someone to open that black screen for me so I can code. I was probably wrong with my approach but I am still better than lots of those urgentzz codezz plzzz types I guess.

              "Bastards encourage idiots to use Oracle Forms, Web Forms, Access and a number of other dinky web publishing tolls.", Mycroft Holmes[^]

              K Offline
              K Offline
              kburman6
              wrote on last edited by
              #6

              From you article Adding Post-commit Hook to SVN Source Control It seems like you are much better than other. :laugh:

              1 Reply Last reply
              0
              • D dan sh

                But for lesser mortals like me, who did Mechanical engineering and still ended up here, this is news. Besides, the first book I ever read about computers was to learn C++ programming. At that time, I had no clue that the black screen where I type code is console. I had to literally ask someone to open that black screen for me so I can code. I was probably wrong with my approach but I am still better than lots of those urgentzz codezz plzzz types I guess.

                "Bastards encourage idiots to use Oracle Forms, Web Forms, Access and a number of other dinky web publishing tolls.", Mycroft Holmes[^]

                M Offline
                M Offline
                Marco Bertschi
                wrote on last edited by
                #7

                d@nish wrote:

                I was probably wrong with my approach but I am still better than lots of those urgentzz codezz plzzz types I guess.

                You learnt coding, diddn't you? From that point of view your approach wasn't that wrong :-D . Furthermore I understand if this is news for auto-didacts. And I am not surprised if anyone says that this is news for him, had to recall it myself first too.

                cheers Marco Bertschi


                Software Developer Twitter | Facebook | Articles


                You have absolutely no idea how glad I am that I have no idea at all. - OriginalGriff

                1 Reply Last reply
                0
                • K kburman6

                  It is just pointless to add one more calculation step without any reason. :)

                  M Offline
                  M Offline
                  Marco Bertschi
                  wrote on last edited by
                  #8

                  Exactly. And everything which is pointless leads to time-consume which is to be avoided.

                  cheers Marco Bertschi


                  Software Developer Twitter | Facebook | Articles


                  You have absolutely no idea how glad I am that I have no idea at all. - OriginalGriff

                  K 1 Reply Last reply
                  0
                  • M Marco Bertschi

                    Exactly. And everything which is pointless leads to time-consume which is to be avoided.

                    cheers Marco Bertschi


                    Software Developer Twitter | Facebook | Articles


                    You have absolutely no idea how glad I am that I have no idea at all. - OriginalGriff

                    K Offline
                    K Offline
                    kburman6
                    wrote on last edited by
                    #9

                    :thumbsup:

                    1 Reply Last reply
                    0
                    • K kburman6

                      It is just pointless to add one more calculation step without any reason. :)

                      OriginalGriffO Offline
                      OriginalGriffO Offline
                      OriginalGriff
                      wrote on last edited by
                      #10

                      Oh yes. Little tiny wasted instructions add up fast when you do them as often as array accesses. And remember, when this was decided, even HUGE computers ran at much, much, lower speeds - an IBM 360/195 could execute 16,000,000 instructions per second (at best), compared to the around 6,000,000,000 that your PC is (probably) capable of executing - it's difficult to be sure, due to multiple cores, pipelines, and caches, all of which were a lot smaller or nonexistent in those days. And remember, a 360/195 cost about £3,000,000 back in 1982, when beer was £0.30 per pint...so you didn't want to waste anything! :laugh:

                      The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)

                      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                      E P 2 Replies Last reply
                      0
                      • K kburman6

                        Link to the article For More detail explanation(PDF) Counting arrays from 0 simplifies the computation of the memory address of each element. If an array is stored at a given position in memory (it’s called the address) the position of each element can be computed as

                        element(n) = address + n * size_of_the_element
                        //If you consider the first element the first, the computation becomes

                        element(n) = address + (n-1) * size_of_the_element
                        //Not a huge difference but it adds an unnecessary subtraction for each access.

                        C Offline
                        C Offline
                        Clifford Nelson
                        wrote on last edited by
                        #11

                        That may have made sense in the 1960's, but not really anymore. A simple addition and possibly a shift takes very time relative to all the other processing happening in a computer.

                        P 1 Reply Last reply
                        0
                        • K kburman6

                          Link to the article For More detail explanation(PDF) Counting arrays from 0 simplifies the computation of the memory address of each element. If an array is stored at a given position in memory (it’s called the address) the position of each element can be computed as

                          element(n) = address + n * size_of_the_element
                          //If you consider the first element the first, the computation becomes

                          element(n) = address + (n-1) * size_of_the_element
                          //Not a huge difference but it adds an unnecessary subtraction for each access.

                          K Offline
                          K Offline
                          Keith Barrow
                          wrote on last edited by
                          #12

                          I thought it was to save electrons.:~

                          “Education is not the piling on of learning, information, data, facts, skills, or abilities - that's training or instruction - but is rather making visible what is hidden as a seed”
                          “One of the greatest problems of our time is that many are schooled but few are educated”

                          Sir Thomas More (1478 – 1535)

                          K 1 Reply Last reply
                          0
                          • K Keith Barrow

                            I thought it was to save electrons.:~

                            “Education is not the piling on of learning, information, data, facts, skills, or abilities - that's training or instruction - but is rather making visible what is hidden as a seed”
                            “One of the greatest problems of our time is that many are schooled but few are educated”

                            Sir Thomas More (1478 – 1535)

                            K Offline
                            K Offline
                            kburman6
                            wrote on last edited by
                            #13

                            I thought that it was to save Dark enrgy used in between the two calculation steps. :laugh: Just Joking. :-\

                            1 Reply Last reply
                            0
                            • K kburman6

                              Link to the article For More detail explanation(PDF) Counting arrays from 0 simplifies the computation of the memory address of each element. If an array is stored at a given position in memory (it’s called the address) the position of each element can be computed as

                              element(n) = address + n * size_of_the_element
                              //If you consider the first element the first, the computation becomes

                              element(n) = address + (n-1) * size_of_the_element
                              //Not a huge difference but it adds an unnecessary subtraction for each access.

                              J Offline
                              J Offline
                              jsc42
                              wrote on last edited by
                              #14

                              Actually, it doesn't simplify the calculation of the address. It just aids the ability to interchangeably address the whole array and the address of its first element (e.g. in C, &array and &array[0] both give the same address). In languages like FORTRAN (from 1957 to current day) which start indices from 1, the formula is

                              element(n) = (address_of_array - size_of_the_element) + n * size_of_the_element

                              which is just as simple as starting from 0 as (address_of_array - size_of_the_element) is a compile time constant value. Considering the millions of errors over the decades that starting from zero has perpetuated (e.g. forgetting to add one to the bounds when declaring the array or getting the end point wrong in loops or wasting space by deliberately skipping element 0), doing the compile time calculation would have been a small price to pay.

                              S 1 Reply Last reply
                              0
                              • OriginalGriffO OriginalGriff

                                Oh yes. Little tiny wasted instructions add up fast when you do them as often as array accesses. And remember, when this was decided, even HUGE computers ran at much, much, lower speeds - an IBM 360/195 could execute 16,000,000 instructions per second (at best), compared to the around 6,000,000,000 that your PC is (probably) capable of executing - it's difficult to be sure, due to multiple cores, pipelines, and caches, all of which were a lot smaller or nonexistent in those days. And remember, a 360/195 cost about £3,000,000 back in 1982, when beer was £0.30 per pint...so you didn't want to waste anything! :laugh:

                                The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)

                                E Offline
                                E Offline
                                Erik Rude
                                wrote on last edited by
                                #15

                                Imagine you'd bought the beer instead :) - You wouldn't be able to count to zero let alone from zero

                                1 Reply Last reply
                                0
                                • J jsc42

                                  Actually, it doesn't simplify the calculation of the address. It just aids the ability to interchangeably address the whole array and the address of its first element (e.g. in C, &array and &array[0] both give the same address). In languages like FORTRAN (from 1957 to current day) which start indices from 1, the formula is

                                  element(n) = (address_of_array - size_of_the_element) + n * size_of_the_element

                                  which is just as simple as starting from 0 as (address_of_array - size_of_the_element) is a compile time constant value. Considering the millions of errors over the decades that starting from zero has perpetuated (e.g. forgetting to add one to the bounds when declaring the array or getting the end point wrong in loops or wasting space by deliberately skipping element 0), doing the compile time calculation would have been a small price to pay.

                                  S Offline
                                  S Offline
                                  svella
                                  wrote on last edited by
                                  #16

                                  jsc42 wrote:

                                  element(n) = (address_of_array - size_of_the_element) + n * size_of_the_element

                                  which is just as simple as starting from 0 as (address_of_array - size_of_the_element) is a compile time constant value.

                                  (address_of_array - size_of_the_element) is not a compile time constant value in any but the simplest of cases, i.e. where the array is at a fixed memory location every time the program is run, which is almost never.

                                  J 1 Reply Last reply
                                  0
                                  • C Clifford Nelson

                                    That may have made sense in the 1960's, but not really anymore. A simple addition and possibly a shift takes very time relative to all the other processing happening in a computer.

                                    P Offline
                                    P Offline
                                    PIEBALDconsult
                                    wrote on last edited by
                                    #17

                                    You don't know what else the computer is doing so don't assume that there are plenty of cycles. If every app wastes 10% of the "extra" cycles it doesn't take long to bog the whole thing down.

                                    C 1 Reply Last reply
                                    0
                                    • OriginalGriffO OriginalGriff

                                      Oh yes. Little tiny wasted instructions add up fast when you do them as often as array accesses. And remember, when this was decided, even HUGE computers ran at much, much, lower speeds - an IBM 360/195 could execute 16,000,000 instructions per second (at best), compared to the around 6,000,000,000 that your PC is (probably) capable of executing - it's difficult to be sure, due to multiple cores, pipelines, and caches, all of which were a lot smaller or nonexistent in those days. And remember, a 360/195 cost about £3,000,000 back in 1982, when beer was £0.30 per pint...so you didn't want to waste anything! :laugh:

                                      The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)

                                      P Offline
                                      P Offline
                                      PIEBALDconsult
                                      wrote on last edited by
                                      #18

                                      OriginalGriff wrote:

                                      wasted instructions add up fast

                                      Which is why I detest animated widgets. X|

                                      1 Reply Last reply
                                      0
                                      • S svella

                                        jsc42 wrote:

                                        element(n) = (address_of_array - size_of_the_element) + n * size_of_the_element

                                        which is just as simple as starting from 0 as (address_of_array - size_of_the_element) is a compile time constant value.

                                        (address_of_array - size_of_the_element) is not a compile time constant value in any but the simplest of cases, i.e. where the array is at a fixed memory location every time the program is run, which is almost never.

                                        J Offline
                                        J Offline
                                        jsc42
                                        wrote on last edited by
                                        #19

                                        You are correct! What I meant (and I admit that the language that I used was far from rigorous) was that the relative offset is unvarying. If, for example, the offset of the start of the memory allocated for the array is 100 from a location (relative addressing / stack frame relative / absolute / whatever) and each element takes up 4 address locations (e.g. bytes), then all that the compiler does for 1-based indices is to use 96 + n * 4 (because 100 - 4 = 96) instead of 100 +(n - 1) * 4 in all of its address calculations for the array; this is as simple as using 100 + n * 4 for 0-based indices. This can be extended for arrays with specifiable lower bounds e.g. in this example if lwb is the lower bound, the 0-based index offset would be 100 + (n - lwb) * 4 [or more optimally (100 - lwb * 4) + n * 4] or for 1-based index offset would be (100 - (lwb + 1) * 4) + n * 4 = (96 - lwb * 4) + n * 4.

                                        S 1 Reply Last reply
                                        0
                                        • K kburman6

                                          Link to the article For More detail explanation(PDF) Counting arrays from 0 simplifies the computation of the memory address of each element. If an array is stored at a given position in memory (it’s called the address) the position of each element can be computed as

                                          element(n) = address + n * size_of_the_element
                                          //If you consider the first element the first, the computation becomes

                                          element(n) = address + (n-1) * size_of_the_element
                                          //Not a huge difference but it adds an unnecessary subtraction for each access.

                                          D Offline
                                          D Offline
                                          DarthDana
                                          wrote on last edited by
                                          #20

                                          Also - The first positive number in any number system is zero. Another way to state it: All positive number systems start with zero.

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups