VOOZH about

URL: https://www.geeksforgeeks.org/java/bitwise-operators-in-java/

⇱ Bitwise Operators in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Bitwise Operators in Java

Last Updated : 13 May, 2026

Bitwise operators in Java operate directly on the binary representation of integer values, performing operations bit by bit instead of on whole numbers. This makes them efficient for low-level processing and performance-critical tasks.

  • Work only with integral types like byte, short, int, long, and char
  • Used in system-level programming, optimization, and competitive coding
  • Help in fast bit manipulation such as masking, shifting, and toggling bits

Let us now look at each bitwise operator and understand its working with examples.

1. Bitwise AND (&)

This operator is a binary operator, denoted by '&.' It returns bit by bit AND of input values, i.e., if both bits are 1, it gives 1, else it shows 0. 

Example:

a = 5 = 0101 (In Binary)
b = 7 = 0111 (In Binary)

Bitwise AND Operation of 5 and 7
0101
& 0111
________
0101 = 5 (In decimal)

2. Bitwise OR (|) 

This operator is a binary operator, denoted by '|'. It returns bit by bit OR of input values, i.e., if either of the bits is 1, it gives 1, else it shows 0. 

Example:

a = 5 = 0101 (In Binary)
b = 7 = 0111 (In Binary)

Bitwise OR Operation of 5 and 7
0101
| 0111
________
0111 = 7 (In decimal)

3. Bitwise XOR (^) 

This operator is a binary operator, denoted by '^.' It returns bit by bit XOR of input values, i.e., if corresponding bits are different, it gives 1, else it shows 0. 

Example:

a = 5 = 0101 (In Binary)
b = 7 = 0111 (In Binary)

Bitwise XOR Operation of 5 and 7
0101
^ 0111
________
0010 = 2 (In decimal)

4. Bitwise Complement (~)

This operator is a unary operator, denoted by '~'. It inverts all the bits of the given number (0 becomes 1 and 1 becomes 0).

Important: In Java, int is a 32-bit signed integer.

Example:

a = 5

32-bit binary representation of 5:
00000000 00000000 00000000 00000101

After applying ~a (bitwise complement):
11111111 11111111 11111111 11111010

In decimal, this value is -6.
This is because in Java:

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

Explanation: The bitwise complement operator ~ in Java inverts all 32 bits of an integer.
For any integer N, the result of ~N is equal to:

~N = -(N + 1)

Therefore, ~5 = -(5 + 1) = -6.
Illustration: Here is a Java program demonstrating all bitwise operators.


Output
a&b = 5
a|b = 7
a^b = 2
~a = -6
a= 5

Example: Display Bitwise Operations in Binary Format


Output
 a= 0011
 b= 0110
 a|b= 0111
 a&b= 0010
 a^b= 0101
~a & b|a&~b= 0101
~a= 1100

Bit-Shift Operators (Shift Operators) 

Shift operators are used to shift the bits of a number left or right, thereby multiplying or dividing the number by two, respectively. They can be used when we have to multiply or divide a number by two. 

Syntax: 

number shift_op number_of_places_to_shift;

Types of Shift Operators

Shift Operators are further divided into 3 types. These are:

  • Signed Right shift operator (>>) : Shifts bits to the right and preserves the sign bit (MSB), filling left side with the sign bit.
  • Unsigned Right shift operator (>>>) : Shifts bits to the right and fills the left side with 0, ignoring the sign bit.
  • Left shift operator (<<) : Shifts bits to the left and fills the right side with 0, effectively multiplying the number by powers of 2.

Note: For more detail about the Shift Operators in Java, refer Shift Operator in Java.

Example: Program to Implement all Bitwise Operators in Java for User Input

Input:

Enter first number: 4
Enter second number: 8

Output:

Bitwise AND: 0
Bitwise OR: 12
Bitwise XOR: 12
Bitwise NOT: -5
Bitwise Left Shift: 16
Bitwise Right Shift: 1
Bitwise Unsigned Right Shift: 1

Explanation: This program prompts the user to enter two numbers, num1 and num2. It then performs the following bitwise operations using the &, |, ^, ~, <<, >>, and >>> operators:

Comparision table

OperatorNamePurpose
&Bitwise ANDReturns 1 only if both bits are 1
|Bitwise ORReturns 1 if at least one bit is 1
^Bitwise XORReturns 1 if both bits are different
~Bitwise NOTInverts all bits (0 ↔ 1)
<<Left ShiftShifts bits left and fills with 0
>>Right ShiftShifts bits right (keeps sign bit)
>>>Unsigned Right ShiftShifts bits right and fills with 0


Comment