Java bitwise operators examples – BitWise Operators in Java with Example Programs | Java Bitwise Operators Syntax, Symbols, Truth Tables

Java bitwise operators examples: In this tutorial, we will discuss the Bitwise Operators in Java with Examples. Bitwise Operators are used in general to manipulate the individual bits of a number. You can use these Java BitWise Operators with any kind of Integral Types such as Char, int, Short, etc., and can’t be applied to double and float. Learn all the Java Bitwise Operators in detail and how each of them works.

Read More: Java Map Interface with Example

Bitwise Operators

Explain bitwise operators in java with example: Bitwise Operators usually work on the binary digits of input values. You can apply these to several integer types such as long, short, char, int, and byte. Bitwise Operators work on the binary equivalent of decimal numbers and then perform the given operation on them bit by bit.

Now that you are aware of the Bitwise Operators let us understand how they work by looking at an example.

Types of Bitwise Operators in Java

Below is the table listing all the 7 BitWise Operators along with symbols, descriptions. Learn how to use these Java Bitwise operators as well.

BitWise Operators in Java with Example 1

Bitwise AND Operator (&)

This operator returns 1 if both the operands are 1 or else it returns 0.

Check out the below truth table for understanding the Bitwise AND Operator. Let us consider two operands A and B that can only take the Binary Values 1 or 0.

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

Bitwise AND Operator Example

Bitwise AND Operator Example

Output:

x & y = 8

Bitwise OR Operator (|)

This operator returns 1 if either of the bits in the operand is 1, else it returns 0.

Below is the Truth Table for Bitwise OR Operator and you can learn the demonstration of the Bitwise OR Operator. Here A, B are Two Operands on which Bitwise OR Operation is performed.

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

Bitwise OR Operator with Example

Bitwise OR Operator Example

Bitwise Complement Operator(~)

This operator inverts all of the bits of its operands. It is denoted by the symbol ~. However, this Bitwise Complement Operator in Java works with a single operand only unlike others.

Bitwise Complement Operator Example

public class BitwiseComplimentExample
{
public static void main(String[] args)
{
int x = 2;
// bitwise compliment // ~0010= 1101 = -3
System.out.println("~x = " + (~x));
}
}

Output:

~x = -3

Have a look at the below example to understand the same.

0 1 0 1

↓ ↓ ↓ ↓

1 0 1 0

Note: Remember that the Bitwise Complement of an Integer N is the same as -(N+1).

1’s Complement can be simply obtained by doing the negation of inputs i.e. if we have 1 the 1’s complement of it is 0 and if 1 is the input 1’s complement would be 0.

2’s Complement:

2’s Complement of a Number can be found by simply adding 1 to the result of 1’s complement.

24 in Binary  = 00011000

1’s Complement of 24 = 11100111

2’s Complement of 24 = 11100111

+1

——–––––

11101000

——–––––

Bitwise Exclusive OR Operator (^)

This operator returns 1 if the corresponding bits are different, else it returns 0. If both the operators are 0 or if both of them are 1 then the result is 0.

Check out the below truth table to understand the Bitwise Exclusive OR Operator clearly. Let A and B be two operands that take the binary values i.e. 0 or 1.

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

Bitwise XOR Operator Example

Bitwise XOR Operator Example

Output:

x ^ y = 1

Do Check: Vector in Java with Example

Bitwise Shift Operators in Java

There are three different types of Shift Operators in Java and they are in the below fashion. Each of them is explained clearly with their respective truth tables. They are as such

  • Bitwise Shift Left Operator (<<)
  • Bitwise Shift Right Operator(>>)
  • Shift Right Zero Fill Operator(>>>)

The syntax for the Shift Right Operators is as such

value <operator> <number_of_times>

Bitwise Shift Left Operator (<<)

This operator shifts the bits of the number to the left and fills 0 in the void spaces that are left as a result. Left Shift Operator shifts all the bits towards the left by a certain number of bits specified and is denoted using the symbol <<.

Example:

If we Perform 1 Bit Left Shift Operation each individual bit is shifted by 1 bit. The Leftmost bit is discarded and the rightmost bit remains vacant and filled with 0s.

1    1    0   0
↙ ↙ ↙ ↙
Discarded 1 ←   1000←0(Replacement Bit)

Bitwise Left Shift Operator Example

Bitwise Left Shift Operator Example

Bitwise Shift Right Operator(>>)

This operator shifts the bits of the number to the right and fills 0 in the void spaces that are left as a result. It is denoted using the symbol >>. If we shift any number to the right least significant bit or rightmost digit is discarded and the most significant position(leftmost bit) is filled with the respective sign bit.
In Shift Right Operators there are two types namely

Signed Right Shift Operator: In this case, bits of the number are filled to the right and the void spaces are filled with the sign bit i.e. 1 for a negative number and 0 for a positive number. The leftmost bit depends on the initial number sign. Signed Right Shift is denoted using the symbol >>.

Example 1:
a = 10
a>>1 = 5 

Example 2:
a = -10 
a>>1 = -5
We store the sign bit.

Unsigned Right Shift Operator: Shifts the numbers of the bits to the right and fills 0 on the voids left as a result. The leftmost bit is set to 0. Unsigned Shift is represented using the symbol >>>.

Example 1:
a = 10
a>>>1 = 5

Example 2:
a = -10 
a>>>1 = 2147483643
Doesn't  preserve the sign bit.

Shift Right Zero Fill Operator(>>>)

This operator shifts the bits of the number to the right and fills 0 in the void spaces that are left as a result. The leftmost bit is set to be 0.

Bitwise Operators in Java Example Program

The following is a sample program that demonstrates all the Bitwise Operators. You can simply copy-paste the following program in your Java Compilers and run the same as it is.

public class BitwiseOperator {
public static void main(String[] args)
{
//Initial values
int a = 6;
int b = 7;

// bitwise and
// 0110 & 0111=0110 = 6
System.out.println("a&b = " + (a & b));

// bitwise or
// 0110 | 0111=0111 = 7
System.out.println("a|b = " + (a | b));

// bitwise xor
// 0110 ^ 0111=0001 = 1
System.out.println("a^b = " + (a ^ b));

// bitwise and
// ~0110=1001
// will give 2's complement of 1001 = -7
System.out.println("~a = " + ~a);

// bitwise left shift
System.out.println("a << 2 = " +(a << 2)); // bitwise right shift System.out.println("a >> 2 = " +(a >> 2));

// bitwise shift right zero fill
System.out.println("b >>> 2 = " +(b >>> 2));
}

}

Output:

The following Output is obtained for the above program.

BitWise Operators in Java with Example 2