Skip to main content
Published on

Bitwise Operators in Java

Share:

Introduction

In the world of programming, we often encounter situations where direct manipulation of bits becomes crucial. In Java, bitwise operators provide that power.

Basic Concepts

Bitwise operators work directly at the bit level, making operations such as comparison, arithmetic, and sign changes more performance-efficient. However, before diving into the details of these operators, it is essential to understand the binary system:

  • In binary, we have only two digits: 0 (False) and 1 (True).
  • These digits can be combined and compared to create patterns that represent numeric values.

Bitwise Operators in Detail

& (Bitwise AND)

Compares each bit of the first operand to the corresponding bit of the second operand. If both bits are 1, the resulting bit will be 1. Otherwise, it will be 0.

| (Bitwise OR)

If any of the bits of the operand is 1, the resulting bit will be 1. Otherwise, it will be 0.

<< (Left Shift)

Shifts the bits of the operand to the left by the specified number of times. Introduces zeros on the right.

>> (Right Shift)

Shifts the bits of the operand to the right by the specified number of times. Preserves the sign bit on the left.

^ (Bitwise Exclusive OR)

Returns 1 for each position where the corresponding bits of the operands differ.

~ (Bitwise NOT)

Inverts all bits.

Example

package com.caffeinealgorithm.programaremjava;

public class BitwiseOperators {
  private int x = 60; // 00111100 < 01111000 < 11110000 | 00111100 > 00011110 > 00001111
  private int y = 13; // 00001101

  public void Run() {
    System.out.printf("Result of the & operator: %d\n", x & y); // 12 -> 00001100
    System.out.printf("Result of the | operator: %d\n", x | y); // 61 -> 00111101
    System.out.printf("Result of the << operator: %d\n", x << 2); // 240 -> 11110000
    System.out.printf("Result of the >> operator: %d", x >> 2); // 15 -> 00001111
  }
}

/*
  Result of the & operator: 12
  Result of the | operator: 61
  Result of the << operator: 240
  Result of the >> operator: 15
*/

Additional Example

Suppose we want to create a simple filter for an image, where we want to invert the colours. The inversion operation can be easily performed with the ~ operator.

int originalColor = 0xFFFFFF; // White in RGB format
int invertedColor = ~originalColor; // Result will be 0x000000, which is black
System.out.println("Inverted color: " + Integer.toHexString(invertedColor));

Advantages of Bitwise Operators

  1. Performance: Bitwise operations are extremely fast because they operate directly at the bit level.
  2. Flexibility: They allow a wide range of operations, from basic arithmetic to complex data manipulation.
  3. Diverse Applications: Used in various situations such as data compression, cryptography, hardware communication, and games.

Conclusion

Bitwise operators are powerful tools in Java that, when used correctly, can make code more efficient and flexible. A deep understanding of these operators can be the key to optimising certain aspects of programming, especially in tasks related to data processing at the binary level.

Happy coding!