为什么在C中写入1000,000,000作为1000 * 1000 * 1000? [英] Why write 1,000,000,000 as 1000*1000*1000 in C?
问题描述
在由苹果创建的代码,有这一行:
In code created by Apple, there is this line:
CMTimeMakeWithSeconds( newDurationSeconds, 1000*1000*1000 )
有没有理由将 1,000,000,000
表示为 1000 * 1000 * 1000
?
为什么不 1000 ^ 3
那个问题?
推荐答案
以乘法方式声明常量的一个原因是提高可读性,而运行时性能是不受影响。
另外,表明作者正在考虑数字的乘法方式。
One reason to declare constants in a multiplicative way is to improve readability, while the run-time performance is not affected. Also, to indicate that the writer was thinking in a multiplicative manner about the number.
考虑一下:
double memoryBytes = 1024 * 1024 * 1024;
明显优于:
double memoryBytes = 1073741824;
因为后者乍一看不是1024的第三个力量。
as the latter doesn't look, at first glance, the third power of 1024.
正如Amin Negm-Awad所提到的, ^
运算符是二进制 XOR
。许多语言缺少内置的编译时取幂运算符,因此乘法。
As Amin Negm-Awad mentioned, the ^
operator is the binary XOR
. Many languages lack the built-in, compile-time exponentiation operator, hence the multiplication.
这篇关于为什么在C中写入1000,000,000作为1000 * 1000 * 1000?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!