Flags
A Flags value is an efficient way of storing whether particular flags are on and off.
It is represented as a 32-bit integer, where each bit represents a flag.
To specify a Flags literal, use the 0b prefix followed by a binary number,
or 0x followed by a hexadecimal number.
// The 0b prefix creates flags from a binary number
let a = 0b100 // flag 3 is on
// The 0x prefix creates flags from a hexadecimal number
let b = 0x4 // equivalent to 0b100
Bitwise Operators
let x = 0b100 | 0b010 // bitwise OR - output is 0b110
let x = 0b100 & 0b110 // bitwise AND - output is 0b100
let x = 0b101 ^ 0b011 // bitwise XOR - output is 0b110
let x = 0b100 << 1 // left shift - output is 0b1000
let x = 0b100 >> 1 // right shift - output is 0b010
let x = ~0xff00ff00 // bitwise NOT - output is 0x00ff00ff
Arithmetic Operators
You can perform arithmetic operations on Flags values. Both values must be Flags.
let x = 0x01 + 0x02 // addition - output is 0x03
let x = 0x03 - 0x01 // subtraction - output is 0x02
let x = 0x02 * 0x03 // multiplication - output is 0x06
let x = 0x06 / 0x02 // division - output is 0x03
let x = 0x07 % 0x04 // modulo - output is 0x03
When performing arithmetic operations on Flags values, both values must be Flags - you cannot add a Flag to a Number for example. This is because they have different precision and range, and so Easel requires you to be explicit about what type you want to use. Consider using ToFlags or ToNumber to convert both operands to the same type before performing the operation.
Flag indices
You can use Flags as the index into an Array. They will be converted to a Number before being used as an index.
let values = ["zero", "one", "two", "three"]
let x = values[0b10] // x is "two"
values[0b11] = "THREE" // sets the value at index 3 to "THREE"