Programs

# What is Bitwise Operator In Java? and It’s Classification (With Examples)

Java is one of the most predominant programming languages in India, commanding around 20 percent of the market share. A bitwise operator in Java is a symbol/notation that performs a specified operation on standalone bits, taken one at a time. It is used to manipulate individual bits of a binary number and can be used with a variety of integer types – char, int, long, short, byte.

A bitwise operator in Java can be broadly classified into 7 types:

 Bitwise Operator in Java Symbol Description Bitwise AND | Preserves a bit in the result if and only if it exists in both operands. Bitwise OR & Preserves a bit if it exists in either of the two operands. Bitwise XOR ^ Preserves a bit if it is present only in one operand but not both. Bitwise COMPLEMENT ~ Also known as 1’s complement. Just reverse the bits of the operand. Bitwise LEFT SHIFT << The bits in the left operand are shifted to the left by the number of places specified in the right operand. SIGNED RIGHT SHIFT >> The bits in the left operand are shifted to the right by the number of places specified in the right operand. UNSIGNED RIGHT SHIFT >>> Exactly like SIGNED RIGHT SHIFT plus shifted bits are filled with zero.

Let us now explore in detail each bitwise operator in Java:

### 1. OR (A | B)

Truth Table

 A B Output 0 0 0 0 1 1 1 0 1 1 1 1

Example

A = 1011 1110

B = 0011 0101

A | B = 1011 1111

Syntax

class Main {

public static void main(String[] args) {

int operand1 = 190, operand2 = 53, output;

// bitwise OR between 190 and 53

output = operand1 | operand2;

System.out.println(output);    // program prints 191

}

}

### 2. AND (A & B)

Truth Table

 A B Output 0 0 0 0 1 0 1 0 0 1 1 1

Example

A = 1011 1110

B = 0011 0101

A & B = 0011 0100

Syntax

class Main {

public static void main(String[] args) {

int operand1 = 190, operand2 = 53, output;

// bitwise OR between 190 and 53

output = operand1 | operand2;

System.out.println(output);    // program prints 52

}

}

1. INCLUSIVE OR / XOR (A ^ B)

Truth Table

 A B Output 0 0 0 0 1 1 1 0 1 1 1 0

Example

A = 1011 1110

B = 0011 0101

A ^ B = 1000 0011

Syntax

class Main {

public static void main(String[] args) {

int operand1 = 190, operand2 = 53, output;

// bitwise OR between 190 and 53

output = operand1 | operand2;

System.out.println(output);    // program prints 131

}

}

### 3. COMPLEMENT (A ~ B)

In decimal terms, the bitwise complement of a number A is – (A+1). However, in the binary domain, we need to first calculate its 1’s complement and then convert it into its 2’s complement to get the desired output.

1’s Complement – Just reverse the bits. For example:

A = 1011 1110 (190)

1’s Complement of A = 0100 0001

2’s Complement of A = 1’s Complement of A + 1 = 0100 0010

Syntax

class Main {

public static void main(String[] args) {

int operand4 = 190, output;

// bitwise complement of 190

output = ~operand4;

System.out.println(output);    // prints -191

}

}

1. LEFT SHIFT (A << 1)

Suppose A = 1011 1110

A << 1 = ?

This bitwise operator in Java makes all the bits shift to the left by 1, i.e., the output is 0111 1100. The left-most bit (most significant bit or MSB) is discarded, and a 0 is added to the rightmost empty place (least significant bit or LSB). This is done for every vacant place at the right.

Syntax

class Main {

public static void main(String[] args) {

int operand = 3; // binary – 11

// 1-bit left shift operation

int output= operand << 1;

System.out.println(output);    // prints 2

}

}

### 4. SIGNED RIGHT SHIFT (A >>2)

This bitwise operator in Java shifts the bit pattern to the right by the specified number of places. LSBs are discarded, and the MSBs are filled with the sign bit. 0 represents a positive sign, while 1 represents a negative sign.

Syntax

class Main {

public static void main(String[] args) {

int operand 1 = 2;

int operand2 = -2;

// 2 bit signed right shift

System.out.println(operand1 >> 2);    // prints 1

System.out.println(operand2 >> 2);    // prints -1

}

}

### 5. UNSIGNED RIGHT SHIFT (A >>>2)

The operation here is exactly similar to SIGNED RIGHT SHIFT except that the vacant left-most bits are replaced with 0 instead of the sign bits.

Syntax

class Main {

public static void main(String[] args) {

int operand  = 240; // binary number – 1111 0000

// 2 bit unsigned right shift

System.out.println(operand >>> 2);    // prints 60

}

}

Learn Software Engineering Courses online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs, or Masters Programs to fast-track your career.

## Conclusion

Keen to master Java and make a career out of it? Check out upGrad’s Software & Technology track. Enrol for the 13-month Executive PG Program in Full-Stack Development and get your hands dirty with real-world projects. Affiliated with the reputed IIIT Bangalore, you get the opportunity to learn 16 programming languages & tools, and 3 job interviews guaranteed.

## What are bitwise operators?

A bitwise operator operates on each bit of the operands and returns a result in the form of 1s and 0s. Most of the bitwise operators are used for making various masks, which can be use to make bit fields, change the size of a data type and align a data type. Bitwise operators are used to perform bit-by-bit operation on a binary number. It is used to be more efficient in bit level operations. We cannot use bitwise operator on variables of non-integer data types.

## What are the types of bitwise operators in Java?

In Java Language, apart from arithmetic operators, bitwise operators are also used. Although there are only a few bitwise operators, they can be extremely helpful. In Java there are six operators. They are: Bitwise AND, Bitwise OR, Bitwise XOR, Bitwise Left Shift, Bitwise Right Shift, and Ones Complement. These operators can be applied to the integer types byte, short, int, long, and char.

## What are the features of java programming language?

Java is a general-purpose object-oriented programming language. Java applications are typically compiled into bytecode, which is then executed on the Java Virtual Machine. Java is designed to run on a variety of platforms. It is also designed to be used in a distributed computing environment. A program written in the Java programming language is called a Java Application or a Java Application Environment. The Java programming language is a powerful and flexible language. Java applications are often larger and more complex than the average web application. The Java programming language is a general-purpose language. It can be used to create applications for any purpose. Features like multithreading, networking, security, and persistence make Java suitable for a wide variety of uses. 