什么呢字符串 - '0'做(string是一个char) [英] what does string - '0' do (string is a char)
问题描述
这是什么呢
while(*string) {
i = (i << 3) + (i<<1) + (*string -'0');
string++;
}
在*字符串-'0
the *string -'0'
它删除字符值还是怎么了?
does it remove the character value or something?
推荐答案
此减去到字符串
指向字符的ASCII code 0
。因此, 0
- 0
为您提供 0
等和'9'
- 0
为您提供 9
。
This subtracts from the character to which string
is pointing the ASCII code of the character '0'
. So, '0'
- '0'
gives you 0
and so on and '9'
- '0'
gives you 9
.
整个循环基本上是心计手动的十进制整数的数值字符串中的字符串
点。
The entire loop is basically calculating "manually" the numerical value of the decimal integer in the string string
points to.
这是因为 I&LT;&LT; 3
等同于我* 8
和 I&LT;&LT; 1
等同于我* 2
和(I&LT;&LT; 3)+(I&LT;&LT; 1)
等同于我* 8 + I * 2
或我* 10
。
That's because i << 3
is equivalent to i * 8
and i << 1
is equivalent to i * 2
and (i << 3) + (i<<1)
is equivalent to i * 8 + i * 2
or i * 10
.
这篇关于什么呢字符串 - '0'做(string是一个char)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!