I understand it is possible to declare enum's with int values as follows:
enum PT:UInt32 {
case Fast = 1
case Normal = 2
case Slow = 3
}
However, I want to indicate which "bit" position in a binary value each element belongs to, so for example:
enum PT:UInt32 {
case Fast = 1 << 10
case Normal = 2 << 10
case Slow = 3 << 10
}
This makes clear that the options fall into a certain binary position in an integer. But the compiler won't allow it of course. Is there a way to work around it?
I know we could represent the above like this:
enum PT:UInt32 {
case Fast = 1024 // 1 << 10
case Normal = 2048 // 2 << 10
case Slow = 3072 // 3 << 10
}
But this solution is clearly not ideal. It makes the code really hard to read. Especially when we are dealing with larger numbers. You would need to add documentation in the code to explain what these numbers are.
For those who dont understand why someone would do this, it is done in the case where tens or hundreds of millions of items need to held in memory at the same time. Int values that don't need a whole byte can be collapsed down to parts of a byte.