#programming #cheatsheet # [[Epistemic status]] #shower-thought #to-digest # Related - [[Algorithm cheatsheet]] # TODO > [!TODO] TODO > put together a cheatsheet of the fundamental mental models to code lowest complexity algorithms using bitwise hacks # Bitwise hacks - Set union A | B - Set intersection A & B - Set subtraction A & ~B - Set negation ALL_BITS ^ A or ~A - Set bit A |= 1 << bit - Clear bit A &= ~(1 << bit) - Test bit (A & 1 << bit) != 0 - Extract last bit A&-A or A&~(A-1) or x^(x&(x-1)) - Remove last bit A&(A-1) - Get all 1-bits ~0 ` # External links