Skip to main content

Bitwise Operators

Introduction

Bitwise operators in Java are used for performing operations on bits, which are the smallest unit of data in a computer. These operators perform bit-level operations which are not only faster but also save memory.

Java supports several bitwise operators, which can be applied to the integer types, long, int, short, char, and byte.

Bitwise Operators in Java

Java supports the following bitwise operators:

  1. Bitwise AND Operator (&)
  2. Bitwise OR Operator (|)
  3. Bitwise XOR Operator (^)
  4. Bitwise Complement Operator (~)
  5. Left Shift Operator (<<)
  6. Right Shift Operator (>>)
  7. Zero Fill Right Shift Operator (>>>)

Let's understand each one of them with examples.

Bitwise AND Operator (&)

The bitwise AND operator compares each bit of the first operand to the corresponding bit of the second operand. If both bits are 1, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0.

int a = 5; //binary: 0101
int b = 7; //binary: 0111
int result = a & b; //binary: 0101
System.out.println("a&b = " + result);

Output: a&b = 5

Bitwise OR Operator (|)

The bitwise OR operator compares each bit of the first operand to the corresponding bit of the second operand. If either bit is 1, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0.

int result = a | b; //binary: 0111
System.out.println("a|b = " + result);

Output: a|b = 7

Bitwise XOR Operator (^)

The bitwise XOR (exclusive OR) operator compares each bit of the first operand to the corresponding bit of the second operand. If the bits are not the same, the corresponding result bit is set to 1. Otherwise, it sets to 0.

int result = a ^ b; //binary: 0010
System.out.println("a^b = " + result);

Output: a^b = 2

Bitwise Complement Operator (~)

The bitwise complement operator is a unary operator (works on a single operand). It changes each bit to its opposite: 0 becomes 1, and 1 becomes 0.

int a = 5; //binary: 0101
int result = ~a; //binary: 1010
System.out.println("~a = " + result);

Output: ~a = -6

Left Shift Operator (<<)

The left shift operator shifts all of the bits in a value to the left a specified number of times. It has two operands. The left operand is a value to be shifted, and the right operand is the number of positions by which to shift.

int a = 5; //binary: 0101
int result = a << 2; //binary: 010100
System.out.println("a << 2 = " + result);

Output: a << 2 = 20

Right Shift Operator (>>)

The right shift operator shifts all bits in a value to the right a specified number of times. The left operand is a value to be shifted, and the right operand is the number of positions by which to shift.

int a = 20; //binary: 10100
int result = a >> 2; //binary: 101
System.out.println("a >> 2 = " + result);

Output: a >> 2 = 5

Zero Fill Right Shift Operator (>>>)

The zero-fill right shift operator behaves just like the >> operator, except that it fills the leftmost bits with zeros, regardless of the sign of the original value.

int a = -20; //binary: 11101100
int result = a >>> 2; //binary: 00111011
System.out.println("a >>> 2 = " + result);

Output: a >>> 2 = 1073741819

Conclusion

Bitwise operators are very powerful and can be used to perform a variety of operations quickly and efficiently. Understanding them can help you write more efficient code and better understand how data is manipulated at the lowest levels. Be sure to practice using these operators to become more familiar with them.