我的代码使用两个常量,每个常量以不同的方式描述数组的大小:
const ArraySize = 1024;
ArrayBits = 10; //2^10 = 1024 bits我如何用其中的一个来表达另一个呢?编译器不允许在常量中使用Log2或LdExp。
任何版本的Delphi的答案都是OK的。
发布于 2012-08-29 04:46:04
const
ArrayBits = 10;
ArraySize = 1 shl ArrayBits;shl b的值等于a* 2^b,因此1shl ArrayBits等于2^ArrayBits。
发布于 2012-08-29 04:46:59
好的,你可以这样做:
const
ArrayBits = 10;
ArraySize = 1 shl ArrayBits;但我可能会回避这一点。这对我来说有点太晦涩了。在我看来,当你执行算术运算时,你应该使用算术运算符。
我可能会让您的代码保持原样,并在运行时代码中添加一个断言,说明这两个常量是适当相关的。也可以在文档中添加注释。
https://stackoverflow.com/questions/12167245
复制相似问题