Bitwise operators work on the individual bits of a number rather than its value as a whole. They're the foundation of flags, masks and low-level optimisation, and a bitwise calculator lets you watch them work bit by bit.
On this page
Truth tables
| a | b | a AND b | a OR b | a XOR b |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 |
| 0 | 1 | 0 | 1 | 1 |
| 1 | 0 | 0 | 1 | 1 |
| 1 | 1 | 1 | 1 | 0 |
NOT flips every bit; XNOR is the negation of XOR (1 when the bits match).
Worked examples
Using 12 (1100) and 10 (1010):
1100 (12) 1100 (12) 1100 (12)
AND 1010 (10) OR 1010 (10) XOR 1010 (10)
---- ---- ----
1000 = 8 1110 = 14 0110 = 6
12 AND 10 = 8, 12 OR 10 = 14, 12 XOR 10 = 6.
Bitwise operators in code
The truth tables above show what each operator does in the abstract. Here is the same pair, 12 and 10, run through AND, OR, XOR, NOT, and a left and right shift, in four languages. Every number below was run, not guessed.
Python
a = 12
b = 10
print(a & b) # AND
print(a | b) # OR
print(a ^ b) # XOR
print(~a) # NOT
print(a << 1) # left shift
print(a >> 1) # right shift
Output, in order: 8, 14, 6, -13, 24, 6.
JavaScript
const a = 12;
const b = 10;
console.log(a & b); // AND
console.log(a | b); // OR
console.log(a ^ b); // XOR
console.log(~a); // NOT
console.log(a << 1); // left shift
console.log(a >> 1); // right shift
Output, in order: 8, 14, 6, -13, 24, 6, run through Node.
C
#include <stdio.h>
int main(void) {
int a = 12, b = 10;
printf("%d\n", a & b); /* AND */
printf("%d\n", a | b); /* OR */
printf("%d\n", a ^ b); /* XOR */
printf("%d\n", ~a); /* NOT */
printf("%d\n", a << 1); /* left shift */
printf("%d\n", a >> 1); /* right shift */
return 0;
}
Output, in order: 8, 14, 6, -13, 24, 6, on the 32-bit two's complement int every common compiler uses.
Java
public class Bitwise {
public static void main(String[] args) {
int a = 12, b = 10;
System.out.println(a & b); // AND
System.out.println(a | b); // OR
System.out.println(a ^ b); // XOR
System.out.println(~a); // NOT
System.out.println(a << 1); // left shift
System.out.println(a >> 1); // right shift
}
}
Output, in order: 8, 14, 6, -13, 24, 6. Java's int is defined as a 32-bit signed two's complement number, so this never changes between machines.
NOT is the one that surprises people. It does not turn 1100 into 0011: it flips every bit, including the one that marks the sign, so the result reads as negative. NOT 12 is -13 in all four languages, not 3. Python stores whole numbers at any size rather than a fixed 32 bits, but it still defines NOT as -x-1 for the same reason, so ~12 comes out to -13 there too. The two's complement walkthrough works through why, bit by bit. Try it: Not(12d) in BASE-N mode → lands on -13.
A left shift moves every bit one place up and fills the gap with a 0, the same as multiplying by 2: 12 << 1 is 24. A right shift does the opposite, the same as dividing by 2 and dropping the remainder: 12 >> 1 is 6. WideMath's BASE-N mode has no shift key, so these two stay in the code snippets above rather than the calculator.
Bit masking
A mask is a second number chosen for its bit pattern, not its value. Pairing a mask with AND or OR covers three of the four classic bit idioms: test, set and clear. The fourth, toggle, gets its own section next because XOR works differently from the other three.
Is bit 2 (the 4s place) on in 12? 1100 AND 0100 = 0100. That is not zero, so yes. In code, a & 4 with a = 12 is 4; a result of 0 would mean the bit was off.
Turn on bit 0 (the 1s place) in 10, whatever it currently is: 1010 OR 0001 = 1011, which is 11. In code, b | 1 with b = 10 is 11.
Turn off bit 3 (the 8s place) in 12 and leave the rest alone: AND with the complement of the mask. 1100 AND 0111 = 0100, which is 4. In code that complement comes from NOT: a & ~8 with a = 12 is 4.
Toggling bits
Toggle is XOR with a mask: any bit that is 1 in the mask flips in the target, and every other bit stays put. XOR is its own opposite, so toggling the same bit twice always returns the original number, which is why it fits a switch you flip from more than one place in a program.
Flip the low bit of 12: 1100 XOR 0001 = 1101, which is 13. In code, a ^ 1 with a = 12 is 13. Run 13 ^ 1 and it is 12 again: the toggle undoes itself.
XOR tricks
XOR has two well known uses beyond masking, and both lean on the same two facts: a number XOR itself is 0, and a number XOR 0 is unchanged.
a, b = 5, 9
a ^= b
b ^= a
a ^= b
print(a, b) # 9 5
Tested: it prints 9 5, the values swapped with no third variable.
Caution: skip this outside of a genuine size-critical loop or an interview answer. It breaks if a and b are the same variable or the same array slot, and a plain temporary variable reads more clearly while compiling to much the same instructions on any modern compiler.
nums = [4, 1, 2, 1, 2]
unique = 0
for n in nums:
unique ^= n
print(unique) # 4
Tested: it prints 4. XOR every value together and each pair cancels itself out, so whatever is left is the one value without a partner. The same chain works on the calculator: 4d xor 1d xor 2d xor 1d xor 2d in BASE-N mode → also lands on 4.
Caution: this only finds the answer when every other value appears an exact even number of times. Reach for a plain count or a set when that is not guaranteed.
On the calculator
Press MODE, choose BASE-N, then SHIFT and 3 to open the logical-operations menu: 1 for AND, 2 for OR, 3 for XOR, 4 for XNOR, 5 for Not(, or 6 for Neg( (two's complement). A number keeps its base no matter which display is active: type 10d for decimal, 1010b for binary, 1Fh for hex or 17o for octal, then switch to BIN to watch the result bit by bit. To build the operands first, see the decimal to binary conversion guide, and for the whole mode the binary, octal & hex calculator pillar guide.
Frequently asked questions
What do AND, OR and XOR do?
AND gives 1 only when both bits are 1. OR gives 1 when at least one bit is 1. XOR gives 1 when the bits differ. All three compare two numbers bit by bit.
What is bit masking?
Bit masking means using a second number, the mask, together with a bitwise operator to work on specific bits. AND with a mask tests or clears bits, OR with a mask sets bits, and XOR with a mask toggles bits.
How do I run bitwise operations on the calculator?
Press MODE, choose BASE-N, then SHIFT and 3 for the logical-operations menu: AND, OR, XOR, XNOR or Not(. Switch to BIN to see the result bit by bit.
What does XOR do?
XOR compares two bits and gives 1 when they differ and 0 when they match. On whole numbers it does this place by place, so 12 XOR 10 is 6 (1100 XOR 1010 is 0110). XOR is its own opposite: applying it twice with the same value returns the original number.
What is the difference between & and &&?
& is the bitwise AND used in C, Java, JavaScript and Python: it compares two numbers bit by bit, so 12 & 10 is 8. && is logical AND: it compares two true or false conditions and stops checking as soon as the first one is false. Mixing them up still compiles, which is what makes the bug easy to miss.
Why does NOT give a negative number?
Bitwise NOT flips every bit, including the one that marks the sign in a signed integer. Flipping all the bits of 12 turns that sign bit on, so the result reads as negative: NOT 12 is -13 in Python, JavaScript, C and Java alike. The two's complement walkthrough works through why, bit by bit.
What is bit masking used for in real code?
Three common uses: packing several true or false flags into one integer instead of several variables, reading hardware or network fields that are only a few bits wide, and checking or changing permission bits. All three come down to the same moves: test, set, clear and toggle a bit.
Sources
The language behavior and the swap trick above, checked against their own documentation:
- MDN Web Docs: Bitwise AND (JavaScript)
- Python documentation: binary bitwise operations
- The Java Language Specification: primitive types (int is a 32 bit signed two's complement integer)
- cppreference: C bitwise operators
- Wikipedia: Two's complement
- Wikipedia: XOR swap algorithm
See the bits flip
Try 12 XOR 10 in BASE-N mode and switch to binary to watch it happen.
Calculate 12d xor 10d in BASE-N mode →