Bitwise Operator in Java

A bitwise operator in Java is an operator that performs operations on the individual bits (0 or 1) of integer operands.

Bitwise operators work only with integral data types, such as byte, short, int, long, and char. They cannot be applied to floating-point types like float and double.

In Java, numbers are internally represented using the binary number system (base 2), which consists of only two digits: 0 and 1. Bitwise operators directly manipulate these binary representations (bit patterns), making them useful for low-level programming and performance optimization.

Types of Bitwise Operators in Java


In Java, there are seven types of bitwise operators. They are listed in the table below.

OperatorMeaning
1. &bitwise AND (Binary)
2. |bitwise OR (Binary)
3. ^bitwise XOR (Binary)
4. ~bitwise NOT (Unary)
5. <<Left shift
6. >>Right shift
7. >>>Unsigned right shift

You can also see the classification of bitwise operators in Java in the figure below.

Types of bitwise operators in Java: AND, OR, XOR, NOT, Left shift. Right shift, and Unsigned right shift.

 

Bitwise AND Operator (&) in Java


The bitwise AND operator (&) in Java is used to perform a bitwise AND operation between two integral operands. It is represented by the symbol &, called an ampersand. It compares each bit of the left operand with the corresponding bit of the right operand and returns 1 only if both bits are 1; otherwise, it returns 0.

Let us consider the truth table given in the figure to understand the operation of the AND operator.
Truth table of AND operation in Java

What is Truth Table?

A truth table is a table that gives the relationship between input values and the corresponding output. In a bitwise AND operation, each bit of one operand is compared with the corresponding bit of another operand. The result is 1 only if both bits are 1; otherwise, the result is 0.
[blocksy-content-block id=”12371″]
From the truth table, as shown in the figure above, if both compared input bits are 1, the output is 1. Otherwise, the output is 0.

When we apply the AND operation to two binary numbers, the result is obtained by performing a bit-by-bit comparison. For example, on applying the AND operation to the individual bits of two numbers x and y, the result is obtained by comparing each corresponding bit.

x = 10 → 00001010
y = 11 → 00001011

Then:

x & y = 00001010, which is equal to 10 in decimal form. Let’s write a Java program to verify it.

Example 1: Bitwise AND Operation

public class BitwiseANDExample {
   public static void main(String[] args) 
   {
      int a = 10, b = 11;
      System.out.println("(10 & 11): " +(a & b));
   }
}

Output:

(10 & 11): 10

In this example:

  • We have defined two variables named a and b and assigned the values 10 and 11, respectively.
  • The binary representation of 10 is 00001010, and the binary representation of 11 is 00001011.
  • (a & b) performs an AND operation on each corresponding bit.
  • Bitwise AND (&) returns 1 only when both bits are 1. Therefore, the result of the AND operation between 00001010 and 00001011 is 00001010, which is 10 in the decimal number system.

Bitwise OR Operator ( | ) in Java


The bitwise OR operator (|) in Java is used to perform an OR operation on the bits of two integral operands. It is represented by the symbol |, called the pipe symbol.

In this operation, each bit of the first operand is compared with the corresponding bit of the second operand. The result is 1 if at least one of the corresponding bits is 1; otherwise, the result is 0.

Let us understand the truth table of the OR operator, as shown in the figure below, to understand the OR operation.Truth table of OR operation in Java

To understand the OR operation, consider the truth table. If both compared bits are 0, the output is 0. If at least one of the bits is 1, the output is 1.
[blocksy-content-block id=”12121″]
When applying the OR operation to the bits of x = 20 and y = 10, we get:

x = 20 → 00010100
y = 10 → 00001010
------------------
x | y = 00011110

The output bits 00011110 are nothing but 30 in decimal form. Let us write a program in Java to test the output.

Example 2: Bitwise OR Operation

public class BitwiseORExample {
   public static void main(String[] args) 
   {
      int a = 20, b = 10;
      System.out.println("(20 | 10): " + (a | b));
   }
}

Output:

(20 | 10): 30

In this example:

  • We have defined two variables named a and b and assigned the values 20 and 10, respectively.
  • The binary representation of value 20 is 00010100, and the binary representation of value 10 is 00001010.
  • (a | b) performs an OR operation on each corresponding bit.
  • Bitwise OR (|) returns 1 only when at least one bit between two bits is 1. Therefore, the result of the OR operation between 00010100 and 00001010 is 00011110, which is 30 in the decimal number system.

Bitwise XOR Operator (^)


The bitwise XOR (^) operator in Java performs an exclusive OR operation on the bits of two numbers or operands. It compares each bit of the first operand with the corresponding bit of the second operand. The result is 1 if the corresponding bits are different; otherwise, the result is 0.

The XOR operator is represented by the symbol ^, called a caret. To better understand the XOR operation, we can use its truth table.

Truth table of XOR operation in Java

To understand the bitwise XOR operation, consider its truth table shown in the figure above. The XOR operator compares each bit of two operands.

If the corresponding bits are different, the result is 1; otherwise, the result is 0. In other words, if two bits have the same value, the output is 0; if they are different, the output is 1.

For example:

x = 20 → 00010100
y = 10 → 00001010
------------------
x ^ y = 00011110 → 30 (decimal)

Thus, the result of the XOR operation is 30. Let us write a Java program to verify it.

Example 3: Bitwise XOR Operation

public class BitwiseXORExample {
   public static void main(String[] args) 
   {
      int a = 20, b = 10;
      System.out.println("(20 ^ 10): " +(a ^ b));
   }
}

Output:

(20 ^ 10): 30

In this example:

  • We have defined two variables named a and b and assigned the values 20 and 10 to them, respectively.
  • The binary representation of 20 is 00010100, and the binary representation of 10 is 00001010.
  • (a ^ b) performs an XOR operation on each corresponding bit.
  • Bitwise XOR (^) returns 1 only when both bits between two input bits are different. Therefore, the result of the XOR operation between 00010100 and 00001010 is 00011110, which is 30 in the decimal number system.

Bitwise NOT Operator (~)


The bitwise NOT operator (~) in Java is a unary operator that inverts all the bits of its operand. It converts all 1s to 0s and all 0s to 1s.

Since it flips each bit, it is also known as a bitwise complement or one’s complement operator. It operates on only one operand, which is why it is called a unary operator.

In Java, integers are represented using 2’s complement, so the result of the NOT operation may produce a negative value. The truth table of the bitwise NOT operator is:

A~A
01
10

Let’s take a simple example program, where we will perform all operations based on bitwise operators in Java.

Example 4: Operations Based on AND, OR, XOR, and NOT

public class BitwiseOperatorsEx {
   public static void main(String[] args) 
   {
      int a = 2, b = 10;
      System.out.println("(2 & 10): " + (a & b));
      System.out.println("(2 | 10): " + (a | b));
      System.out.println("(2 ^ 10): " + (a ^ b));
      System.out.println("~10: " + ~b);
   }
}

Output:

(2 & 10): 2
(2 | 10): 10
(2 ^ 10): 8
~10: -11

Practical Use Cases of Bitwise Operators in Java


Let us look at some simple real-time examples of bitwise operators in Java.

Example 5: Check Even or Odd Using Bitwise AND Operator

public class ANDEvenOddExample {
   public static void main(String[] args) {
      int num = 14;
      if ((num & 1) == 0)
         System.out.println(num + " is an even number.");
      else
         System.out.println(num + " is an odd number.");
   }
}

Output:

14 is an even number.

In this example:

  • The expression num & 1 checks the last bit (LSB) of the binary representation.
  • The binary representation of 14 is 00001110.
  • Since the last bit is 0, it is an even number.
  • If the LSB is 1, it is an odd number.

Example 6: Set a Specific Bit (Enable Feature)

public class ORSetBitExample {
   public static void main(String[] args) {
      int num = 8; // 1000
      int result = num | (1 << 1);
      System.out.println("After setting bit 1: " + result);
   }
}

Output:

After setting bit 1: 10

In this example:

  • We have defined a variable named num and assigned the value 8 to it.
  • The binary representation of 8 is 00001000.
  • The expression 1 << 1 means shift 1 left by 1 position. Hence, 00000001 << 1 is 00000010, which is 2 in the decimal number system.
  • The bitwise OR operation between 00001000 | 00000010 is 00001010, which is 10 in the decimal number system.

Example 7: Toggle All Bits Using NOT Operator

public class NOTToggleExample {
   public static void main(String[] args) {
      int num = 5;
      int result = ~num;
      System.out.println("After NOT operation: " + result);
   }
}

Output:

After NOT operation: -6

In this example:

  • The binary representation of the number 5 is 00000101.
  • The bitwise NOT operator flips bits to 11111010, which results in -6 due to 2’s complement.

Example 8: Swap Two Numbers Using Bitwise XOR Operator

public class XORSwapExample {
   public static void main(String[] args) {
      int a = 5, b = 10;
      a = a ^ b;
      b = a ^ b;
      a = a ^ b;

      System.out.println("After swapping:");
      System.out.println("a = " + a);
      System.out.println("b = " + b);
   }
}

Output:

After swapping:
a = 10
b = 5

In this example:

  • The initial value of variable a is 5, whose binary representation is 0101.
  • The initial value of variable b is 10, whose binary representation is 0101.
  • The first expression is a ^ b (5 ^ 10) = 0101 ^ 1010 = 1111 → 15 (in the decimal number system).
  • Since the result 15 is assigned to the variable a, the value of a is now 15, and b is 10.
  • The second expression is a ^ b (15 ^ 10) = 1111 ^ 1010 = 0101 → 5 (in the decimal number system).
  • Since the result 5 is assigned to the variable b, the value of b is now 5, and a is 15.
  • The third expression is a ^ b (15 ^ 5) = 1111 ^ 0101 = 1010 → 10 (in the decimal number system).
  • Now the new value of a is 10 and b is 5. Hence, after swapping, a = 10 and b = 5.

In this tutorial, you learned about bitwise operators in Java, such as AND, OR, NOT, and XOR, with the help of examples. Stay tuned with the next tutorial, where you will learn about shift operators in Java, such as left shift, right shift, and unsigned right shift.

DEEPAK GUPTA

DEEPAK GUPTA

Deepak Gupta is the Founder of Scientech Easy, a Full Stack Developer, and a passionate coding educator with 8+ years of professional experience in Java, Python, web development, and core computer science subjects. With strong expertise in full-stack development, he provides hands-on training in programming languages and in-demand technologies at the Scientech Easy Institute, Dhanbad.

He regularly publishes in-depth tutorials, practical coding examples, and high-quality learning resources for both beginners and working professionals. Every article is carefully researched, technically reviewed, and regularly updated to ensure accuracy, clarity, and real-world relevance, helping learners build job-ready skills with confidence.