Why do all the subscripts in computer languages start from 0?

image.png

Why do the subscripts in computer languages 0start from the beginning?

  • With the popularization of the Internet, the phenomenon of pan-coding and agriculturalization of various types of work has become more and more obvious. For those who have not learned the basic theory of computers, there may always be a puzzle:
  • Why do the subscripts of data containers in computer languages 0start from the beginning? Not from the 1beginning?

To find the answer to this question, we can first describe how the computer stores and calls data at the bottom.

image.png

  • Take pythonthe strdata format in as an example:
  • In fact, there is no 字符串storage unit called " " in the chip . The memory is essentially a large chip, composed of countless storage units, arranged in sequence, and the computer numbers these boxes.

image.png

  • These storage units are very small, we call them bytes ( byte), usually only 8 bits ( bit).
  • How small is it? It can only represent a letter or number, and requires 2 storage units to represent a Chinese character.
  • Therefore, if you store an article, many such storage units will be used.

After talking about the data format, let's analyze a concept-storage method.

image.png

  • When we name a variable, the computer will tell the memory that a piece of data will be stored next and carry some basic information to the computer, and then the memory will wait for us to assign a value to this variable.
  • What does the computer tell memory when naming variables? Take string as an example:

image.png

  • The computer will first find a blank storage unit in the memory. Let us first assume that this is the 100th storage unit (referred to later No.100).
  • When it No.100is determined that it is used to store sentencethis string, the computer will tell the memory that it sentenceis equal No.100to the first string of strings.

image.png

  • Let's sentenceassign a value so that we can better explain it later:
  • Because there No.100can only be one byte and only one letter can be stored, No.100only the letter " h" is actually stored in it ,
  • The following ello word!characters of " " are stored in No.101~ No.111storage unit respectively.

After the above basic information is introduced, we can return to today's topic-the subscript issue.

  • For memory, it sentenceis No.100synonymous, so when you want to call sentence, you are actually calling No.100the string from the beginning
  • Here is a small explanation: In fact No.112, there will be a "terminator" in the position
  • Therefore sentence, when the computer obtains it, it can be understood that the computer sends an instruction to the memory: " Fetch characters from No.100( sentence) and continue to the terminator".

image.png

  • And when you use sentence[n]this format, the real instruction of the computer is to get the data on the " No.100( sentence) +  n" storage unit;

image.png

  • For example, sentence[6]equivalent No.100+6, that is, No.106data on the storage unit, that is w.
  • It sentence[0]is to fetch No.100+0the data.

Of course, Python simplifies a lot from the syntax, let us save a lot of extra steps. But in essence, the representation logic of subscripts is based on this underlying logic. One pass Belden, I hope this post can help you to relieve the confusion about subscripts.


  • Thank you for reading all the way to the end, part of the content is organized from the Internet, invaded and deleted
  • If this article is helpful to you, please remember to share or click on'I am watching' this article.
  • If you have any questions, errors or comments about the content of the article, please leave a message.
  • image.png


Guess you like

Origin blog.51cto.com/15069443/2576240