×

Types of Bitwise Operators in Java

In this tutorial, we will learn about the various types or kinds of bitwise operators in java. Before proceeding to bitwise operators, let us know what is meant by the term operator. An operator is a symbol meant to perform a specific operation.

The following six types of bitwise operators are there in Java:

  • Bitwise AND ( & )
  • Bitwise exclusive OR (^)
  • Bitwise inclusive OR (|)
  • Bitwise Compliment ( ~ )
  • Bitwise left Shift Operator ( << )
  • Bitwise right Shift Operator ( >> )

Now, let us try to grasp the concept of each of the above bitwise operators.

Bitwise AND Operator

It is a binary operator denoted by the symbol &. If and only if both bits are 1, it returns 1otherwise it returns 0.

Implementation:

Let us now understand the working of bitwise AND operator through a java program.

Example 1:

public class BitwiseAndExample1   
{   
public static void main(String[] args)   
{   
int a = 6, b = 3;   
System.out.println("a & b = " + (a & b));   
}  
}  

Output

Types of Bitwise Operators in Java

Explanation: 6 = 0110 and 3 = 0011. Therefore, 6 & 3 = 0010 which is equal to 2

Example 2:

public class BitwiseAndExample2   
{   
public static void main(String[] args)   
{   
int a = 4, b = 2;   
System.out.println("a & b = " + (a & b));   
}  
}

Output:

Types of Bitwise Operators in Java

Explanation: 4 = 0100 and 2 = 0010. Therefore, 4 &2 = 0000 which is equal to 0

Bitwise Exclusive OR (^)

It is another binary operator which is denoted by the symbol ^ (caret). If both the bits are the same, it returns 0otherwise it returns 1.

Implementation:

Let us now understand the working of bitwise OR operator through a java program.

Example1:

public class BitwiseXorExample1   
{   
public static void main(String[] args)   
{   
int a = 6, b = 3;   


System.out.println("a ^ b = " + (a ^ b));   
}  
}

Output:

Types of Bitwise Operators in Java

Explanation: 6 = 0110 and 3 = 0011. Therefore, 6^3 = 0101 which is equal to 5

Example2:

public class BitwiseXorExample1   
{   
public static void main(String[] args)   
{   
int a = 4, b = 2;   


System.out.println("a ^ b = " + (a ^ b));   
}  
}

Output:

Types of Bitwise Operators in Java

Explanation: 4 = 0100 and 2 = 0010. Therefore, 4 ^ 2 = 0110 which is equal to 6

Bitwise Inclusive OR (|)

It is another binary operator denoted by the symbol | (pipe). If either of the bit is 1, it returns the value 1otherwise it returns 0.

Implementation:

Let us see a java program to understand the working of the Bitwise inclusive OR operator.

Example 1:

public class BitwiseInclusiveOrExample1  
{   
public static void main(String[] args)   
{   
int a = 6, b = 3;   


System.out.println("a | b = " + (a | b));   
}  
}  

Output:

Types of Bitwise Operators in Java

Explanation: 6 = 0110 and 3 = 0011. Therefore, 6 | 3 = 0111 which is equal to 7

Example2:

public class BitwiseInclusiveOrExample2  
{   
public static void main(String[] args)   
{   
int a = 4, b = 2;   


System.out.println("a | b = " + (a | b));   
}  
}  

Output:

Types of Bitwise Operators in Java

Explanation: 4 = 0100 and 2 = 0010. Therefore, 4 | 2 = 0110 which is equal to 6

Bitwise Complement (~)

It is a unary operator which means it takes only 1 operand into account.It is denoted by the symbol ~ (tilde). The inverse or complement of the bit is returned by this operator. It thus changes every 0 to 1 and every 1 to 0.

Implementation:

Let us understand the working of a bitwise complement operator in a Java program.

Example 1:

public class BitwiseComplimentExample1  
{   
public static void main(String[] args)   
{   
int a = 6;   


System.out.println("~a = " + (~a));   
}  
}

Output:

Types of Bitwise Operators in Java

Example 2:

public class BitwiseComplimentExample1  
{   
public static void main(String[] args)   
{   
int a = 4;   


System.out.println("~a = " + (~a));   
}  
}

Output:

Types of Bitwise Operators in Java

Bitwise Shift Operators

The following three types of shift operators are offered in Java:

  • Signed Left Shift Operator or Bitwise Left Shift Operator
  • Signed Right Shift Operator or Bitwise Right Shift Operator
  • Unsigned Right Shift Operator

Bitwise Left Shift Operators

The Left Shift operator is taken into account to shift the bits in the left direction as per the given no. of bits (n). It is denoted by a symbol <<. The shift operators can be used while dividing or multiplying any number by 2.It also conserves the leftmost bit (sign bit). It does not conserve the sign bit.

The following is the format or syntax to shift the bit:

variable << number of places to shift;   

For example, if a=15

  1. a <<2; //shifts 2 bits  
  2. a<< 3; //shifts 3 bits  
  3. a << 5; //shifts 5 bits 

Implementation:

Example 1:

Let us now look at some java programs to understand the concept of bitwise right shift operator.

public class SignedLeftShiftOperatorExample1
{   
public static void main(String args[])   
{   
int a = 15;   
System.out.println("a<<1 = " + (a << 1));   
}  
}

Output:

Types of Bitwise Operators in Java

Example 2:

public class SignedLeftShiftOperatorExample2   
{   
public static void main(String args[])   
{   
int a = 20;   
System.out.println("a<<1 = " + (a << 1));   
}  
}

Output:

Types of Bitwise Operators in Java

Bitwise Right Shift Operators

The rightShift operator are taken into account to shift the bits in theright direction as per the stated number of bits (n). A symbol >> is used to denote it. The shift operators can be used while dividing or multiplying any number by 2. It also conserves the leftmost bit (sign bit). If 0 is existing at the leftmost bit, it can be inferred that the number is positive. If 1 is present at the leftmost bit, it denotes the number is negative.

The following is the format or syntax to shift the bit :

variable >>number of places to shift;   

For example, if a=15

  1. a >>2; //shifts 2 bits  
  2. a>> 3; //shifts 3 bits   
  3. a >> 5; //shifts 5 bits

Implementation:

Example 1:

Let us now look at some java programs to understand the concept of bitwise right shift operator.

public class SignedLeftShiftOperatorExample1
{   
public static void main(String args[])   
{   
int a = 15;   
System.out.println("a<<1 = " + (a << 1));   
}  
}

Output:

Types of Bitwise Operators in Java

Explanation: In the number 15, the 0 bits are shifted in the leftward direction as per the given no. of bits. This result in a doubling of the given number which in 30. The extra bits are wasted.

Example 2:

public class SignedLeftShiftOperatorExample2   
{   
public static void main(String args[])   
{   
int a = 20;   
System.out.println("a<<1 = " + (a << 1));   
}  
}

Output:

Types of Bitwise Operators in Java

Explanation: In the number 20, the 0 bits are shifted in the leftward direction as per the given no. of bits. This results ina doubling of the given number results in40. The extra bits are wasted.

Unsigned Right Shift Operator

It shifts a zero at the leftmost position and fills zeroes. The symbol >>>is used to denote it. It is to be noted that the leftmost position after >> depends on the sign bit. It does not conserve the sign bit.

Example 1:

If a=10000000 and b=1, find a>>>b?

a >>> b = 10000000 >>>1 = 01000000

Example2:

If a=11100000 and b= 3, find a>>>b?

a >>> b = 11100000 >>>3 = 00011100

Example3:

If a=11111100 and b=4, find a>>>b?

a >>> b = 11111100 >>>4 = 00001111

The left operand value is moved in the right direction as per the number of bits stated by the right operand and the shifted bits are occupied up with zeros. Extra bits shifted off in the right direction are rejected.

Implementation:

Example 1:

Let us see a Java program to understand the usage ofunsigned right-shift operators.

public class SignedRightShiftOperatorExample1   
{   
public static void main(String args[])   
{   
int a = 15;   
System.out.println("a>>1 = " + (a >> 1));   
}  
}


Output:

Types of Bitwise Operators in Java

Explanation: In the number 15, the 0 bits are shifted in the right direction as per the given no. of bits. This result in exactly half of the given number which is 7.

Example 2:

public class SignedRightShiftOperatorExample1   
{   
public static void main(String args[])   
{   
int a = 20;   
System.out.println("a>>1 = " + (a >> 1));   
}  
}

Output:

Types of Bitwise Operators in Java

Explanation: In the number 20, the 0 bits are shifted in the right direction as per the given no. of bits. This result in exactly half of the given number which is 10.

Summary

In this tutorial, we touched on various types of bitwise operators in Java language. We saw each type of bitwise operator through multiple examples and programs.


Related Topics

Java Interface Keyword

An interface is also known as the blueprint in Java. It has constants of static values and methods of abstraction. The interface is a mechanism used by Java to declare...

3 minutes read.

Palindrome Partitioning problem in Java

In this article, you will be acknowledged about partitioning of the palindrome. It can be done in many ways. This article makes sure every method is discussed. Palindrome If a string remains...

6 minutes read.

Java 9 Interface Private Method

Java 9 provides us the facility to include private methods inside the java interface. In java 8 and earlier versions, we are supposed to use only constant variables and abstract...

3 minutes read.

Java Math getExponent() Method

The getExponent() method of Math class returns the unbiased exponent of the argument. Syntax: public static int getExponent (double d) Parameters: The parameter ‘d’ represents the double value. Return Value: The getExponent () method returns the...

1 minute read.

Java String isEmpty() method

Java String isEmpty() method checks whether current String is empty or not. Syntax: public boolean isEmpty() Returns It returns true, if length of String is 0 otherwise false. Java String isEmpty() method example 1    ...

1 minute read.

Bellman Ford Algorithm in Java

Numerous algorithms have been used in dynamic programming to determine the shortest path inside a graph. Among them are Floyd, all-pair shortest path problem, Breadth First Search, Depth First Search,...

6 minutes read.

Types of Statements in Java

In natural languages, statements and sentences are roughly equivalent. In general, statements are similar to valid English sentences. We will talk about a statement in Java and the different kinds...

11 minutes read.

Display List of TimeZone with GMT and UTC in Java

It is vital to establish the right TimeZone in Java code when working with dates for Daylight Saving Time. In this part, we will present the time zones with GMT. TimeZone Those...

5 minutes read.

Ordinal Number in Java

What is the Ordinal Number in Java? An object's or person's position or rank can be expressed mathematically using an ordinal number. Depending on the criteria used to establish the positions,...

3 minutes read.

CopyOnWriteArrayList in Java

The CopyOnWriteArrayList is used to implement the List Interface. It is the improved version of ArrayList. The operations like add, remove, set, update etc, these operations are done by creating...

5 minutes read.

Read JSON file in Java

 To know about reading a JSON file in Java first we must know about the JSON file. JSON: JSON is “JavaScript Object Notation”. The JSON is the simple format for storing and...

3 minutes read.

String Programs in Java

String Programs in Java: In Java, a String is an immutable object that represents a sequence of characters. For example, “Tutorial” is a string that consists of 8 characters: ‘T’,...

10 minutes read.

Creating a Custom Generic Class in Java

To indicate parameter types when creating generic classes, we utilize the <> symbol. The syntax used to generate objects of a generic class is as follows. // To create an instance...

4 minutes read.

IdentityHashMap in Java

The IdentityHashMap class is comparable to the HashMap class and is an AbstractMap implementation. However, when comparing the key, it uses reference equality rather than object equality (or values). Identity HashMap...

6 minutes read.

How to check version of java in Linux

Java is one of the most famous and thoroughly utilized programming tongues from one side of the world to the other. On the off chance that you are a Java...

2 minutes read.

Null Pointer Exception in Java

It is a runtime error exception. The null value is allocated to the object reference in this exception. We will explicitly throw this null pointer exception when the program wants...

3 minutes read.

Why to use Enum in Java?

In this article, you will be acknowledged about the Enum in java, its uses and mainly its purpose. Enum has various functionalities. Each of them will be discussed. Enum In a computer...

3 minutes read.

What is string in Java why it's immutable

String in Java Strings are a series of characters commonly used in Java language, which are considered objects in the Java. To create and manipulate strings, java will provide the String...

4 minutes read.

Highest precedence in Java

In Java, the operator is the first thing that springs to mind when discussing precedence. The order in which the operators in an expression are evaluated is controlled by a...

3 minutes read.

Number Pattern Programs in Java

Number Pattern Programs in Java: Number pattern programs are part of pattern programs. In the previous section, we have learned the approach to print the pattern program in Java. To...

6 minutes read.