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:

TEXT
A = 12 = 1100
B = 10 = 1010

Bitwise 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:

TEXT
0 & 0 = 0
0 & 1 = 0
1 & 0 = 0
1 & 1 = 1

Example:

TEXT
12 = 1100
10 = 1010
------------
& 1000

Result:

TEXT
12 & 10 = 8

Common uses:

  • Checking whether a specific bit is set.
  • Creating bit masks.
  • Checking if a number is even:
Java
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:

TEXT
0 | 0 = 0
0 | 1 = 1
1 | 0 = 1
1 | 1 = 1

Example:

TEXT
12 = 1100
10 = 1010
------------
| 1110

Result:

TEXT
12 | 10 = 14

Common 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:

TEXT
0 ^ 0 = 0
0 ^ 1 = 1
1 ^ 0 = 1
1 ^ 1 = 0

Example:

TEXT
12 = 1100
10 = 1010
------------
^ 0110

Result:

TEXT
12 ^ 10 = 6

Common uses:

  • Toggling bits.
  • Swapping values without a temporary variable.
  • Finding the unique number in an array where every other number appears twice.

Example:

Java
int a = 5;
int b = 5;
int result = a ^ b; // result is 0

A number XORed with itself always becomes 0.

---

4. Bitwise NOT (~)

The NOT operator flips every bit:

  • 1 becomes 0.
  • 0 becomes 1.

Example:

TEXT
A = 5
5 = 00000101
~5 = 11111010

The 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:

TEXT
5 = 00000101
5 << 1
= 00001010

Result:

TEXT
5 << 1 = 10

A left shift by one position is equivalent to multiplying by 2 (for values where overflow does not occur).

General rule:

TEXT
number << n = number * (2^n)

Example:

TEXT
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:

TEXT
20 = 00010100
20 >> 2
= 00000101

Result:

TEXT
20 >> 2 = 5

A right shift by one position is approximately equivalent to dividing by 2.

General rule:

TEXT
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.

Java
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:

TEXT
READ = 0001
WRITE = 0010
READ | WRITE = 0011

The 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?

More Bit Manipulation interview questions

View all →