What are the basic Bitwise Operators?
Updated Apr 28, 2026
Short answer
Bitwise operators perform operations directly on the binary representation of integers by comparing or modifying individual bits. The basic bitwise operators are AND (&), OR (|), XOR (^), NOT (~), left shift (<<), and right shift (>>). These operators are commonly used for optimization, masking, setting or clearing bits, and solving algorithmic problems involving binary data. | Operator | Name | Description | | | -------- | ----------- | --------------------------------------------- | ------------------------------------ | | & | AND | Sets bit to 1 only if both bits are 1 | | | | | OR | Sets bit to 1 if either bit is 1 | | ^ | XOR | Sets bit to 1 if bits are different | | | ~ | NOT | Flips all bits | | | << | Left Shift | Moves bits left, multiplying by powers of two | | | >> | Right Shift | Moves bits right, dividing by powers of two | |
Bitwise operators are usually very fast because they map closely to CPU instructions. However, they can make code harder to understand if used unnecessarily, so they should be applied when they improve clarity or performance.
Deep explanation
Computers store integers as binary numbers, which are sequences of bits (0 and 1). Bitwise operators allow programmers to manipulate these bits directly.
For example, consider two numbers:
A = 12 = 1100B = 10 = 1010Bitwise operations compare these bits position by position.
1. Bitwise AND (&)
The AND operator returns 1 only when both corresponding bits are 1. Otherwise, it returns 0.
Truth table:
0 & 0 = 00 & 1 = 01 & 0 = 01 & 1 = 1Example:
12 = 1100 10 = 1010------------& 1000Result:
12 & 10 = 8Common uses:
- Checking whether a specific bit is set.
- Creating bit masks.
- Checking if a number is even:
if ((number & 1) == 0) { // number is even}---
2. Bitwise OR (|)
The OR operator returns 1 if at least one of the corresponding bits is 1.
Truth table:
0 | 0 = 00 | 1 = 11 | 0 = 11 | 1 = 1Example:
12 = 1100 10 = 1010------------| 1110Result:
12 | 10 = 14Common uses:
- Setting specific bits to
1. - Combining flags or permissions.
---
3. Bitwise XOR (^)
The XOR (exclusive OR) operator returns 1 when the two bits are different.
Truth table:
0 ^ 0 = 00 ^ 1 = 11 ^ 0 = 11 ^ 1 = 0Example:
12 = 1100 10 = 1010------------^ 0110Result:
12 ^ 10 = 6Common uses:
- Toggling bits.
- Swapping values without a temporary variable.
- Finding the unique number in an array where every other number appears twice.
Example:
int a = 5;int b = 5;
int result = a ^ b; // result is 0A number XORed with itself always becomes 0.
---
4. Bitwise NOT (~)
The NOT operator flips every bit:
1becomes0.0becomes1.
Example:
A = 5
5 = 00000101~5 = 11111010The result depends on the integer representation used by the language. Most modern languages use two's complement representation for signed integers.
Common uses:
- Flipping bits.
- Creating masks by inverting bit patterns.
---
5. Left Shift (<<)
The left shift operator moves all bits to the left by a specified number of positions. Empty positions on the right are filled with 0.
Example:
5 = 00000101
5 << 1
= 00001010Result:
5 << 1 = 10A left shift by one position is equivalent to multiplying by 2 (for values where overflow does not occur).
General rule:
number << n = number * (2^n)Example:
5 << 3 = 5 * 8 = 40---
6. Right Shift (>>)
The right shift operator moves bits to the right. The behavior for empty positions depends on whether the language uses arithmetic or logical shifting.
Example:
20 = 00010100
20 >> 2
= 00000101Result:
20 >> 2 = 5A right shift by one position is approximately equivalent to dividing by 2.
General rule:
number >> n = number / (2^n)---
Real-world example
A common real-world use of bitwise operators is storing multiple boolean settings in a single integer. For example, an application might store user permissions using individual bits.
class Permissions { static final int READ = 1; // 0001 static final int WRITE = 2; // 0010 static final int DELETE = 4; // 0100
public static void main(String[] args) { int userPermissions = READ | WRITE;
// Check if user has WRITE permission if ((userPermissions & WRITE) != 0) { System.out.println("User can write"); }
// Add DELETE permission userPermissions = userPermissions | DELETE; }}Here:
READ = 0001WRITE = 0010
READ | WRITE = 0011The integer stores multiple permissions efficiently, and bitwise operations allow the program to add or check permissions quickly.
Common mistakes
- * Confusing bitwise operators (`&`, `|`) with logical operators (`&&`, `||`).
- * Forgetting that bitwise operations work on binary representations, not decimal digits.
- * Assuming right shift always fills empty bits with zeros
- some languages perform arithmetic right shifts for signed numbers.
- * Using bitwise tricks where normal arithmetic or boolean logic would make the code clearer.
- * Forgetting about integer overflow when shifting bits beyond the available bit width.
- * Assuming `~number` produces the mathematical negative of a number
- it only flips bits.
- * Ignoring operator precedence and writing complex bitwise expressions without parentheses.
Follow-up questions
- How is XOR useful for finding a unique element in an array?
- How do you set, clear, and toggle a specific bit using bitwise operators?
- What is the difference between logical shifting and arithmetic shifting?
- Why are bitwise operations considered efficient?
- How can you check whether the k-th bit of a number is set?
- What happens when you shift a number by more bits than its size?