×

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

Switch Case with Enum in Java

From some conditions, the java switch statement executes one statement. Similar to the If-Else-If ladder statement, this can be Byte, short, int, long, enum, string, and some wrapper types like...

4 minutes read.

Interface in Java

  Interface in Java In Java, the interface is just like a class that has only static constants and abstract methods. It is used to achieve polymorphism so that it can also...

6 minutes read.

Java String replaceAll() method

Java String replaceAll() method returns a String replacing all the sequence of characters matching regular expression i.e regex and replacement string. Syntax: public String replaceAll(String regex, String replacement) Parameters: regex : regular expression replacement :...

1 minute read.

Process and Thread in Java

Process It is a program that is running on your computer. The process is a heavy-weight, which takes memory separately to other processes. The process may be a small background task like spell-checker; it...

11 minutes read.

JDBC Architecture

JDBC: JDBC stands for Java Database Connectivity. Sun Microsystems has a specification called JDBC. JDBC is a Java API (Application Programming Interface) that enables users to interact or communicate with...

4 minutes read.

Knapsack problem in Java

We have a collection of items in the knapsack problem. Every object has a weight and a value. These things should go in a knapsack. But there is a weight...

3 minutes read.

Java Worker Class

What is a Worker? A service which executes Work - flow and Activities is referred to as a Worker. On user-controlled hosts, workers are defined and put into action. The Worker...

3 minutes read.

String Coding Interview Questions in Java

What is String in Java?In Java, a String is a Class that is defined in the java.lang package. It isn't a basic data type like int or long. Character Strings...

5 minutes read.

Java Extends vs Implements

Java: We known that the java is a pure object oriented programming language. Java programming language consists of many features such as portable, plat form independence, secured, robust, simple, architecture neutral,...

4 minutes read.

Lazy Propagation in Segment Tree in Java

The topic of segment trees in Java is continued by the topic of sluggish propagation in segment trees. It is suggested that readers first read through the section tree topic....

4 minutes read.

Java Char Keyword

The java char keyword is a data type which is defined as character data type.Char keyword belongs to a primitive data type where the data types are classified into primitive...

4 minutes read.

Java Boolean logicalXor() Method

The logicalXor() method of Java Boolean class returns the result of implementing logical XOR operation on the specified Boolean operands. Syntax: public static boolean logicalXor (boolean a, boolean b) Parameters: The parameters ‘a’ and...

2 minutes read.

Narcissistic Number in Java

A Narcissistic number is made up of digits that have been added together and raised to powers equal to the number of digits in the original number. In those other...

3 minutes read.

Java Math IEEEremainder() Method

The IEEEremainder() method of Math class calculates the remainder as prescribed by the IEEE754 standard. This method simply returns the remainder when f1 (dividend) is divided by f2 (divisor). Syntax: public static...

2 minutes read.

Multithreading Program in Java

Multithreading Program in Java: Before discussing multithreading, it is important to discuss threads. Threads are the most fundamental part of a process. A process can have one or more threads....

4 minutes read.

Java Program to Add Digits Until the Number Becomes a Single Digit Number

We will develop Java programme that increase a number's digit count in order to reduce it to one. The issue is also known as the digit root problem. Example Consider the case where...

3 minutes read.

Java Integer doubleValue() method

The doubleValue() method of Integer class returns a double value for this Integer after a widening primitive conversion. Syntax public double doubleValue() Parameters NA Specified by This method is specified by doubleValue in class Number Return Value This...

1 minute read.

Java String intern() method

Java String intern() method returns canonical representation for the String Object. syntax: public String intern() Return: It returns a interned String that to be a pool of unique String. Java String intern() Example 1 public class...

2 minutes read.

Fall through in Java

In this article, you will be acknowledged about the fall through condition in Java along with an example program. Fall through It is a situation where there isn't a break statement in...

3 minutes read.

Difference Between in Java and C++

FeatureC++JavaDefinitionC++ is a general programming language created by Bjarne Stroustrup as an extension of c language    Java is class-based, object-based, and designed to have as few implementation dependencies as...

4 minutes read.