What Are Bitwise Operators?

No CS degree required — six operators, six truth tables, and your first real bitwise program

Most programmers learn arithmetic first: +, -, *, /. These operators treat numbers as whole values — 5 + 3 = 8, end of story. Bitwise operators work one level deeper. They operate on the individual 0s and 1s that make up a number. Instead of "what is 5 plus 3," a bitwise operator asks "what happens when I compare the first bit of 5 with the first bit of 3, the second with the second, and so on."

There are six fundamental bitwise operators. Four compare two numbers bit-by-bit (AND, OR, XOR, and shifts). One works on a single number (NOT). Use our free bitwise calculator to experiment as you read — enter any two numbers and see the bit-level results instantly.

The Six Bitwise Operators

AND (&)

The AND operator returns 1 only when both input bits are 1. Otherwise it returns 0. Think of it as the "both must agree" operator.

Bit ABit BA & B
000
010
100
111

Example: 5 & 3. 5 in binary is 0101, 3 is 0011. AND each column: 0&0=0, 1&0=0, 0&1=0, 1&1=1 → 0001 = 1. A bitwise AND calculator does this for any two numbers instantly.

OR (|)

The OR operator returns 1 when at least one input bit is 1. Only returns 0 when both are 0. This is the "any yes is yes" operator.

Example: 5 | 3. 0101 | 0011 = 0111 = 7. OR is used to set specific bits without touching others — a flag 0b0010 OR'd with 0b0100 gives 0b0110, combining both flags. Try our bitwise OR calculator to see this in action.

XOR (^)

XOR (exclusive OR) returns 1 when the two bits differ. If they're the same, it returns 0. This is the "spot the difference" operator. XOR has a magical property: applying it twice with the same value gives you back the original. (A ^ B) ^ B = A. This makes XOR the foundation of many encryption schemes and the classic "swap two variables without a temp" trick.

Example: 5 ^ 3. 0101 ^ 0011 = 0110 = 6. Use our bitwise XOR calculator to experiment.

NOT (~)

NOT is the only unary bitwise operator — it takes one number and flips every bit. 0 becomes 1, 1 becomes 0. In two's complement (how computers represent signed integers), ~x = -x - 1. So ~5 = -6 and ~0 = -1. This trips up beginners constantly because the result depends on how many bits your system uses — a 32-bit NOT of 5 gives a very different-looking number than an 8-bit NOT.

Left Shift (<<)

Left shift moves every bit to the left by a specified number of positions. Zeros fill in from the right. Each left shift by 1 multiplies the number by 2. 5 << 1 = 10, 5 << 2 = 20, 5 << 3 = 40. This is why 1 << n is the standard way to write 2n in code — it's faster than Math.pow(2, n) on most CPUs. Use our bit shift calculator to see shift operations with step-by-step binary output.

Right Shift (>>)

Right shift moves every bit to the right, dividing by 2 each time. Bits that fall off the right edge are discarded. The catch: there are two kinds. Logical right shift fills the left side with zeros. Arithmetic right shift copies the sign bit (leftmost bit) to preserve negative numbers. -8 >> 1 gives -4 with arithmetic shift but a large positive number with logical shift. JavaScript uses arithmetic shift (>>) and also has a separate unsigned right shift operator (>>>).

Why Should You Care About Bitwise Operations?

Three reasons. Performance: Bitwise operations execute in a single CPU clock cycle. Shifting is faster than multiplication, AND is faster than modulo for power-of-two checks. Memory: Eight boolean flags fit in one byte with bitmasks — versus eight bytes if you used individual variables. Game engines and embedded systems rely on this. Interviews: FAANG companies love bitwise interview questions. "Find the single number in an array where every other element appears twice" — solved in O(n) time and O(1) space with XOR.

Ready to go deeper? Try our bitwise calculator with real numbers, or read the complete bitwise operations guide.