DEV Community

Rajesh Bhola
Rajesh Bhola

Posted on

Java Bitwise Operators (&, |, ^, ~, !)

Bitwise operators are one of the most interesting topics in Java because they work directly on the binary representation (bits) of numbers.

They're widely used in:

  • Performance optimization
  • Flags and permissions
  • Encryption algorithms
  • Networking
  • Device drivers
  • Competitive programming
  • Interview questions

If bitwise operators have ever seemed confusing, this guide will make them easy to understand with simple binary examples.


What are Bitwise Operators?

Bitwise operators perform operations on the individual bits of a value instead of the entire number.

Java provides five bitwise-related operators:

Operator Name Works On
& Bitwise AND boolean & Integral
| Bitwise OR boolean & Integral
^ Bitwise XOR boolean & Integral
~ Bitwise Complement Integral only
! Logical NOT boolean only

The Three Main Bitwise Operators

Operator Name Rule
& AND Result is 1 only if both bits are 1
| OR Result is 1 if at least one bit is 1
^ XOR (Exclusive OR) Result is 1 only when both bits are different

Truth Table

For boolean values:

A B A & B A | B A ^ B
true true true true false
true false false true true
false true false true true
false false false false false

Rule #1: &, | and ^ Work on Both boolean and Integral Types

This is one of the unique features of Java.

These operators can be used with:

  • boolean
  • byte
  • short
  • int
  • long
  • char

Example with Booleans

public class Main {

    public static void main(String[] args) {

        System.out.println(true & false);
        System.out.println(true | false);
        System.out.println(true ^ false);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

false
true
true
Enter fullscreen mode Exit fullscreen mode

Example with Integers

public class Main {

    public static void main(String[] args) {

        System.out.println(4 & 5);
        System.out.println(4 | 5);
        System.out.println(4 ^ 5);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

4
5
1
Enter fullscreen mode Exit fullscreen mode

How Bitwise Operators Work

Let's first convert the numbers into binary.

4 = 100
5 = 101
Enter fullscreen mode Exit fullscreen mode

Bitwise AND (&)

   100
&  101
-------
   100
Enter fullscreen mode Exit fullscreen mode
100₂ = 4
Enter fullscreen mode Exit fullscreen mode

Result:

4
Enter fullscreen mode Exit fullscreen mode

Only the bits that are 1 in both numbers remain 1.


Bitwise OR (|)

   100
|  101
-------
   101
Enter fullscreen mode Exit fullscreen mode
101₂ = 5
Enter fullscreen mode Exit fullscreen mode

Result:

5
Enter fullscreen mode Exit fullscreen mode

A bit becomes 1 if either number has 1.


Bitwise XOR (^)

XOR means Exclusive OR.

A bit becomes 1 only when both bits are different.

   100
^  101
-------
   001
Enter fullscreen mode Exit fullscreen mode
001₂ = 1
Enter fullscreen mode Exit fullscreen mode

Result:

1
Enter fullscreen mode Exit fullscreen mode

More Integer Examples

Example 1

6 = 110
3 = 011
Enter fullscreen mode Exit fullscreen mode

AND

110
011
---
010 = 2
Enter fullscreen mode Exit fullscreen mode

OR

110
011
---
111 = 7
Enter fullscreen mode Exit fullscreen mode

XOR

110
011
---
101 = 5
Enter fullscreen mode Exit fullscreen mode

Java Code

System.out.println(6 & 3);   // 2
System.out.println(6 | 3);   // 7
System.out.println(6 ^ 3);   // 5
Enter fullscreen mode Exit fullscreen mode

Bitwise Complement (~)

The complement operator flips every bit.

0 → 1

1 → 0
Enter fullscreen mode Exit fullscreen mode

Rule

~ works only on integral types.

Valid

System.out.println(~4);
Enter fullscreen mode Exit fullscreen mode

Invalid

System.out.println(~true);
Enter fullscreen mode Exit fullscreen mode

Compile-time Error

operator ~ cannot be applied to boolean
Enter fullscreen mode Exit fullscreen mode

How ~4 Becomes -5

This is one of the most common interview questions.

Step 1

Represent 4 as a 32-bit integer.

00000000 00000000 00000000 00000100
Enter fullscreen mode Exit fullscreen mode

Step 2

Flip every bit.

11111111 11111111 11111111 11111011
Enter fullscreen mode Exit fullscreen mode

The leftmost bit is now 1, indicating a negative number in Java's two's complement representation.


Step 3

Find the Magnitude

Take the 2's complement:

Flip bits

00000000 00000000 00000000 00000100
Enter fullscreen mode Exit fullscreen mode

Add 1

00000000 00000000 00000000 00000101
Enter fullscreen mode Exit fullscreen mode
= 5
Enter fullscreen mode Exit fullscreen mode

Therefore,

~4 = -5
Enter fullscreen mode Exit fullscreen mode

Shortcut Formula

Instead of converting to binary every time, remember this simple formula:

~n = -(n + 1)
Enter fullscreen mode Exit fullscreen mode

Examples:

Expression Result
~4 -5
~0 -1
~10 -11
~-5 4
~-1 0

Verification:

~10

= -(10 + 1)

= -11
Enter fullscreen mode Exit fullscreen mode

Sign Bit Explained

For Java integral types, the Most Significant Bit (MSB) is the sign bit.

0 → Positive

1 → Negative
Enter fullscreen mode Exit fullscreen mode

Positive numbers are stored directly.

Negative numbers are stored in 2's complement form.


Boolean NOT Operator (!)

The ! operator is not a bitwise operator. It is the logical NOT operator and works only with boolean values.

System.out.println(!true);
System.out.println(!false);
Enter fullscreen mode Exit fullscreen mode

Output

false
true
Enter fullscreen mode Exit fullscreen mode

Invalid Example

System.out.println(!4);
Enter fullscreen mode Exit fullscreen mode

Compile-time Error

operator ! cannot be applied to int
Enter fullscreen mode Exit fullscreen mode

Difference Between ~ and !

Operator Works On
~ Integral types only
! boolean only

A common mistake is trying to use ~ with booleans or ! with integers. Java does not allow this.


Common Examples

public class Main {

    public static void main(String[] args) {

        // Boolean

        System.out.println(true & true);
        System.out.println(true | false);
        System.out.println(false ^ false);

        // Integer

        System.out.println(6 & 3);
        System.out.println(6 | 3);
        System.out.println(6 ^ 3);

        // Complement

        System.out.println(~10);
        System.out.println(~-10);

        // Logical NOT

        System.out.println(!true);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

true
true
false

2
7
5

-11
9

false
Enter fullscreen mode Exit fullscreen mode

Master Summary Table

Operator boolean Integral Types
&
|
^
~
!

Interview Tips

Q1. Which bitwise operators work with boolean values?

&, |, ^, !

~


Q2. Which operator flips every bit?

~
Enter fullscreen mode Exit fullscreen mode

Q3. What is the shortcut for calculating ~n?

~n = -(n + 1)
Enter fullscreen mode Exit fullscreen mode

Q4. Why does ~4 become -5?

Because ~ flips every bit, producing the two's complement representation of -5.


Q5. Can ! be used with integers?

No.

!10
Enter fullscreen mode Exit fullscreen mode

Results in a compile-time error.


Memory Tricks 🧠

& (AND)

All bits must be 1.


| (OR)

At least one bit must be 1.


^ (XOR)

Different bits produce 1.


~ (Complement)

Flip every bit.

Remember:

~n = -(n + 1)
Enter fullscreen mode Exit fullscreen mode

! (Logical NOT)

Reverse the boolean value.

true  → false

false → true
Enter fullscreen mode Exit fullscreen mode

Key Takeaways

  • Bitwise operators work directly on the binary representation of values.
  • &, |, and ^ work with both boolean and integral types.
  • ~ works only with integral types and flips every bit.
  • ! works only with boolean values and performs logical negation.
  • The shortcut ~n = -(n + 1) helps compute complement results quickly.
  • Understanding binary makes bitwise operators much easier to use and is essential for Java interviews.

Happy Coding!

Top comments (0)