I have the follow:
public static final int LIMIT_ONE = 1;
public static final int TRADEABLE = (1 << 1);
public static final int SELLABLE = (1 << 2);
12414 in binary is:
Binary number: 1 1 0 0 0 0 0 1 1 1 1 1 1 0
-------------------------------------------------------
Bit positions: 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Look at which bits are 1. Those are the flags that are set in the bitmask, which is created by using the bitwise OR operator to combine the flags:
bitmask = TRADEABLE | SELLABLE | STORABLE | STORABLE_IN_WH | STORABLE_IN_LEGION_WH | BREAKABLE | BLACK_CLOUD_TRADERS | CAN_SPLIT;
To further explain this, STORABLE = (1 << 3);
means that STORABLE is equal to the number one (binary 1, falling only in bit position 0) shifted to the left by 3 places. Note that STORABLE = Math.pow(2, 3);
would be equivalent. Because none of the bits overlap among the flags, we can combine all of them into a single int and then split them apart later.
We can check for the existence of flags using the bitwise AND operator, which will return a non-zero value if the flag is set and a zero value if the flag is not set:
if(bitmask & TRADEABLE != 0) {
// This item can be traded
} else {
// This item cannot be traded
}
We can set, clear, or toggle flags like this:
bitmask |= TRADEABLE; // Sets the flag using bitwise OR
bitmask &= ~TRADEABLE; // Clears the flag using bitwise AND and NOT
bitmask ^= TRADEABLE; // Toggles the flag using bitwise XOR