Why do the subscripts in computer languages 0
start 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
0
start from the beginning? Not from the1
beginning?
To find the answer to this question, we can first describe how the computer stores and calls data at the bottom.
- Take
python
thestr
data 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.
- 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.
- 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:
- 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.100
is determined that it is used to storesentence
this string, the computer will tell the memory that itsentence
is equalNo.100
to the first string of strings.
- Let's
sentence
assign a value so that we can better explain it later: - Because there
No.100
can only be one byte and only one letter can be stored,No.100
only the letter "h
" is actually stored in it , - The following
ello word!
characters of " " are stored inNo.101
~No.111
storage unit respectively.
After the above basic information is introduced, we can return to today's topic-the subscript issue.
- For memory, it
sentence
isNo.100
synonymous, so when you want to callsentence
, you are actually callingNo.100
the 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 fromNo.100
(sentence
) and continue to the terminator".
- 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;
- For example,
sentence[6]
equivalentNo.100+6
, that is,No.106
data on the storage unit, that isw
. - It
sentence[0]
is to fetchNo.100+0
the 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.