Skip to main content

Toggle Bit

Learn Toggle Bit (Bit Manipulation, Data Structures & Algorithms) with worked examples, diagrams and interview questions. Free on JavaSchoolLab.

Flipping a Specific Bit To TOGGLE bit i (flip 0&rarr;1 or 1&rarr;0): XOR the number with a mask that has ONLY bit i set - num ^ (1 << i) . XOR-ing with 0 leaves a bit unchanged; XOR-ing with 1 flips it - exactly the self-inverse property of XOR.

More lessons in Bit Manipulation