Skip to main content

AND / OR / XOR / NOT

Learn AND / OR / XOR / NOT (Bit Manipulation, Data Structures & Algorithms) with worked examples, diagrams and interview questions. Free on JavaSchoolLab.

The Four Core Bitwise Operators & (AND) - 1 only if BOTH bits are 1. | (OR) - 1 if EITHER bit is 1. ^ (XOR) - 1 if the bits are DIFFERENT. ~ (NOT) - flips every bit. These operate on EVERY bit of an int/long simultaneously, in a single CPU instruction - the reason bit tricks are often dramatically faster than equivalent arithmetic or conditional logic.

More lessons in Bit Manipulation