Reputation: 740
I have code using two constants, each describing the size of an array in different ways:
const ArraySize = 1024;
ArrayBits = 10; //2^10 = 1024 bits
How do I express one of these in terms of the other? The compiler will not allow use of Log2 or LdExp in constants.
Answers for any version of Delphi are OK.
Upvotes: 1
Views: 215
Reputation:
const
ArrayBits = 10;
ArraySize = 1 shl ArrayBits;
The value of a shl b is equal to a * 2^b, so 1 shl ArrayBits is equal to 2^ArrayBits.
Upvotes: 8
Reputation: 612894
Well, you could do this:
const
ArrayBits = 10;
ArraySize = 1 shl ArrayBits;
But I probably would shy away from that. It feels a little too obscure for me. In my opinion you should use arithmetic operators when you are performing arithmetic.
I'd probably leave your code as it is and add an assertion in the runtime code that the two constants were appropriately related. Document it with a comment as well.
Upvotes: 9