enum States { LOADING /***/ = 0b0000, FAILED /****/ = 0b0001, LOADED /****/ = 0b1110, PLAYING /***/ = 0b0100, STOPPED /***/ = 0b1000 } const currentState: number = getStateSomehow(); /** We can use AND operation to compare the states **/ currentState & States.FAILED // ================ // /** We can use OR operation to check multiple states together - explanation right below **/ currentState & (States.STOPPED | States.FAILED) /** * ======================= * STOPPED : 0b 1 0 0 0 * FAILED : 0b 0 0 0 1 * ============ OR ======= * O1 : 0b 1 0 0 1 ( Stopped | Failed ) * CURRENT : 0b 0 1 0 0 ( Playing ) * ============ AND ====== * O2 : 0b 0 0 0 0 ( Same identifier as loading, but it doesn't mean it ) */ // ================ // /** * We can use XOR to assign a new state. This is actually a bit (pun intended) out of scope, * since our example does not allow having a "currentState" that is the combination of multiple * statuses at the same time. The scope of the article was to show how can be related states be * combined, but states can also be unrelated (so each can have its own unique identifer) and we * would still be able to benefit of these feature. * * But let's fake it and let's say that PLAYING, FAILED and STOPPED can be mixed up together. */ currentState = currentState ^ (States.STOPPED | STATES.FAILED) /** * ======================= * STOPPED : 0b 1 0 0 0 * FAILED : 0b 0 0 0 1 * ============ OR ======= * O1 : 0b 1 0 0 1 ( Stopped | Failed ) * CURRENT : 0b 0 1 0 0 ( Playing ) * ============ XOR ====== * O2 : 0b 1 1 0 1 ( Da faq is this?!) */