天天看點

程式設計語言為什麼從零開始計數_為什麼計算機從零開始計數?

程式設計語言為什麼從零開始計數_為什麼計算機從零開始計數?

程式設計語言為什麼從零開始計數

程式設計語言為什麼從零開始計數_為什麼計算機從零開始計數?

Counting from zero is a very common practice in many computer languages, but why? Read on as we explore the phenomenon and why it is so widespread.

從零開始計數是許多計算機語言中非常普遍的做法,但是為什麼呢? 在我們探索該現象以及其如此廣泛的原因時,請繼續閱讀。

Today’s Question & Answer session comes to us courtesy of SuperUser—a subdivision of Stack Exchange, a community-driven grouping of Q&A web sites.

今天的“問答”環節由SuperUser提供,它是Stack Exchange的一個分支,該社群是由社群驅動的Q&A網站分組。

問題 (The Question)

SuperUser reader DragonLord is curious about why most operating systems and programming languages count from zero. He writes:

超級使用者閱讀器DragonLord好奇為什麼大多數作業系統和程式設計語言都從零開始計數。 他寫:

Computers traditionally tally numerical values starting from zero. For example, arrays in C-based programming languages start from index zero.

傳統上,計算機計算從零開始的數值。 例如,基于C的程式設計語言中的數組從索引零開始。

What historical reasons exist for this, and what practical advantages does counting from zero have over counting from one?

這有什麼曆史原因,從零開始計數比從一開始計數有什麼實際優勢?

Why indeed? As widespread as the practice is, surely there are practical reasons for its implementation.

為什麼會這樣呢? 盡管這種做法很普遍,但肯定有實踐的理由。

答案 (The Answer)

SuperUser contributor Matteo offers the following insights:

超級使用者貢獻者Matteo提供以下見解:

Counting arrays from 0 simplifies the computation of the memory address of each element.

從0開始對數組進行計數簡化了每個元素的記憶體位址的計算。

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
           
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.

差别不大,但是每次通路都會添加不必要的減法。

Edited to add:

編輯添加:

  • The usage of the array index as an offset is not a requirement but just an habit. The offset of the first element could be hidden by the system and taken into consideration when allocating and referencing element.

    将數組索引用作偏移量不是必需的,而隻是一種習慣。 系統可以隐藏第一個元素的偏移量,并在配置設定和引用元素時将其考慮在内。

  • Dijkstra published a paper “Why numbering should start at zero” (pdf) where he explains why starting with 0 is a better choice. Starting at zero allows a better representation of ranges.

    Dijkstra發表了一篇論文“為什麼編号應該從零開始”( pdf ),他解釋了為什麼從0開始是更好的選擇。 從零開始可以更好地表示範圍。

If you’re looking to delve deeper into the answer, the Dijkstra paper is an informative read.

如果您想更深入地研究答案,Dijkstra的文章将為您提供豐富的參考資料。

Have something to add to the explanation? Sound off in the the comments. Want to read more answers from other tech-savvy Stack Exchange users? Check out the full discussion thread here.

有什麼補充說明嗎? 在評論中聽起來不對。 是否想從其他精通Stack Exchange的使用者那裡獲得更多答案? 在此處檢視完整的讨論線程 。

翻譯自: https://www.howtogeek.com/149225/why-do-computers-count-from-zero/

程式設計語言為什麼從零開始計數