Java Bitwise Operators Explained: &, |, ^, ~ with Examples

0

Most Java programs work with values such as numbers, text, and objects. But sometimes you need to work closer to the binary representation of an integer—perhaps while handling flags, permissions, compact data, protocols, or low-level algorithms. This is where bitwise operators become useful.

Bitwise operators work directly with the individual bits of integral values. Instead of asking whether an entire number is greater or smaller, they operate on the binary digits that make up that number.

Why Bitwise Operators Matter

Imagine a building with eight light switches. Each switch can be either ON or OFF. Rather than storing eight separate boolean values, you could represent all eight states inside the bits of a single integer. Bitwise operators let Java inspect and modify those individual switches efficiently.

This idea appears in permission systems, feature flags, binary protocols, compression techniques, device communication, and other areas where compact representation matters.

Important: Bitwise operators work on the individual bits of integral types such as byte, short, int, long, and char. They are different from logical operators such as && and ||.

Bitwise Operators at a Glance

Operator Name Purpose Example
& Bitwise AND Sets a bit when both corresponding bits are 1 5 & 3
| Bitwise OR Sets a bit when either corresponding bit is 1 5 | 3
^ Bitwise XOR Sets a bit when corresponding bits are different 5 ^ 3
~ Bitwise complement Flips every bit ~5

Binary Representation

Before using bitwise operators comfortably, it helps to visualize numbers in binary.

5 = 0101
3 = 0011

Each position represents a bit. The rightmost bit represents 1, the next represents 2, then 4, then 8, and so on.

Binary:  0 1 0 1
Value:   8 4 2 1

Therefore, 0101 represents 5 because 4 + 1 = 5.

Bitwise AND (&)

The bitwise AND operator compares corresponding bits. A resulting bit becomes 1 only when both input bits are 1.

int a = 5;
int b = 3;

int result = a & b;

System.out.println(result);

In binary:

  0101
& 0011
------
  0001

The binary result 0001 represents 1, so the program prints 1.

Bitwise AND Truth Table

Bit A Bit B A & B
0 0 0
0 1 0
1 0 0
1 1 1

A useful mental model is: AND keeps only the bits that both values have turned on.

Bitwise OR (|)

The bitwise OR operator produces 1 when either corresponding bit is 1.

int a = 5;
int b = 3;

int result = a | b;

System.out.println(result);

The binary calculation is:

  0101
| 0011
------
  0111

The result 0111 represents 7.

Bitwise OR Truth Table

Bit A Bit B A | B
0 0 0
0 1 1
1 0 1
1 1 1

Think of OR as combining enabled bits from both values.

Bitwise XOR (^)

The XOR, or exclusive OR, operator produces 1 when the corresponding bits are different. If both bits are the same, the result is 0.

int a = 5;
int b = 3;

int result = a ^ b;

System.out.println(result);

In binary:

  0101
^ 0011
------
  0110

The binary result 0110 represents 6.

XOR Truth Table

Bit A Bit B A ^ B
0 0 0
0 1 1
1 0 1
1 1 0

A good way to remember XOR is: different bits produce 1; matching bits produce 0.

Bitwise Complement (~)

The bitwise complement operator flips every bit. Every 0 becomes 1, and every 1 becomes 0.

int number = 5;

int result = ~number;

System.out.println(result);

For a Java int, the value is represented using 32 bits. The operation flips all 32 bits, which is why ~5 produces -6.

For signed integer values, a useful identity is:

~x = -(x + 1)

Therefore:

~5 = -(5 + 1)
   = -6

Bitwise Operators vs Logical Operators

A common beginner mistake is assuming that & and && mean the same thing. They do not.

Operator Category Typical Use Short-Circuiting
& Bitwise AND Manipulating bits No
&& Logical AND Combining boolean conditions Yes
| Bitwise OR Manipulating bits No
|| Logical OR Combining boolean conditions Yes

Important: Use && and || for ordinary boolean conditions. Use &, |, and ^ when you intentionally want bitwise operations.

Bit Masks

One of the most practical uses of bitwise operators is the bit mask. A mask lets you focus on particular bits while ignoring the others.

Suppose the lowest bit represents a particular feature or permission:

int READ = 1;

int permissions = 5;

boolean canRead = (permissions & READ) != 0;

System.out.println(canRead);

The AND operation isolates the bit represented by READ. If that bit is present, the result is non-zero.

This technique is useful when several independent yes-or-no settings need to be packed into one integer.

Setting a Bit with OR

OR can be used to turn a particular bit on without disturbing the other bits.

int READ = 1;
int WRITE = 2;

int permissions = READ;

permissions = permissions | WRITE;

System.out.println(permissions);

The original value contains the READ bit. OR with the WRITE mask turns on the WRITE bit as well.

Clearing a Bit with AND and NOT

A common bit manipulation pattern clears a selected bit by combining AND with the complement of a mask.

int READ = 1;
int WRITE = 2;

int permissions = READ | WRITE;

permissions = permissions & ~WRITE;

System.out.println(permissions);

The ~WRITE mask contains zeros where the WRITE bit exists and ones elsewhere. AND therefore clears the WRITE bit while preserving the other bits.

Toggling a Bit with XOR

XOR is useful for toggling a bit. If the selected bit is 0, XOR changes it to 1. If it is already 1, XOR changes it to 0.

int FLAG = 4;

int value = 0;

value = value ^ FLAG;

System.out.println(value);

The selected flag becomes enabled. Applying the same XOR mask again toggles it back.

Common Beginner Mistakes

  • Confusing bitwise & with logical &&.
  • Confusing bitwise | with logical ||.
  • Forgetting that XOR produces 1 when bits are different.
  • Assuming ~x simply makes a positive number negative without considering the bit representation.
  • Using bitwise operations without first understanding the binary representation of the values.
  • Using bitwise code where a normal boolean expression would be much clearer.

Best Practices

  • Use bitwise operators when the problem genuinely involves individual bits or compact flags.
  • Use meaningful constants or masks instead of unexplained numeric values.
  • Add parentheses when combining bitwise operations with other operators.
  • Keep bit manipulation isolated in well-named methods when the logic is complex.
  • Do not replace readable boolean logic with bitwise tricks merely to make code shorter.
  • Remember that Java's signed integer representation affects operations involving the highest bit.

Interview Insights

Interviewers often test whether you understand the difference between logical and bitwise operators. A strong answer explains that logical operators work with boolean conditions, while bitwise operators manipulate individual bits of integral values.

Another common question is the result of ~5. The answer is -6, based on Java's signed two's-complement representation and the identity ~x = -(x + 1).

You may also be asked how to check whether a particular bit is set. The standard pattern is to AND the value with a mask and test whether the result is non-zero.

Quick Revision

Operator What It Does Example Typical Use
& Keeps bits that are 1 in both operands 5 & 3 → 1 Testing or clearing bits
| Sets bits that are 1 in either operand 5 | 3 → 7 Setting flags
^ Sets bits that differ 5 ^ 3 → 6 Toggling bits
~ Flips every bit ~5 → -6 Bit inversion and masks

Final Takeaway

Bitwise operators give Java developers direct control over the individual bits inside integral values. & is useful for testing and clearing bits, | for setting them, ^ for toggling them, and ~ for flipping them. You may not use these operators every day in ordinary business applications, but understanding them gives you a much deeper view of how Java represents and manipulates data—and becomes especially valuable when working with flags, protocols, systems programming, or performance-sensitive code.

Post a Comment

0Comments
Post a Comment (0)