Bitwise Operators in Java

Introduction

Bitwise operators in Java are used to perform operations at the binary level on integer types (byte, short, int, long). They manipulate individual bits of numbers, which is useful in tasks like low-level programming, performance optimization, and flag management. Java supports operators like AND (&), OR (|), XOR (^), Complement (~), and shift operators (<<, >>, >>>).


1. Bitwise AND (&)

Definition:
Performs AND operation on each corresponding bit of two numbers.

Example Code:

public class BitwiseAND {
public static void main(String[] args) {
int a = 5; // 0101 in binary
int b = 3; // 0011 in binary
int result = a & b;
System.out.println("Result of a & b: " + result); // 1 (0001)
}
}

Explanation:

  • Each bit is compared: 1 & 1 = 1, else 0.
  • Useful for masking bits.

2. Bitwise OR (|)

Definition:
Performs OR operation on each corresponding bit.

Example Code:

public class BitwiseOR {
public static void main(String[] args) {
int a = 5; // 0101
int b = 3; // 0011
int result = a | b;
System.out.println("Result of a | b: " + result); // 7 (0111)
}
}

Explanation:

  • Each bit: 1 | 0 = 1, 0 | 0 = 0.
  • Used to set specific bits.

3. Bitwise XOR (^)

Definition:
Performs exclusive OR: returns 1 if bits are different, 0 if the same.

Example Code:

public class BitwiseXOR {
public static void main(String[] args) {
int a = 5; // 0101
int b = 3; // 0011
int result = a ^ b;
System.out.println("Result of a ^ b: " + result); // 6 (0110)
}
}

Explanation:

  • XOR is useful for toggling bits or finding differences.

4. Bitwise Complement (~)

Definition:
Inverts all bits of the number.

Example Code:

public class BitwiseComplement {
public static void main(String[] args) {
int a = 5; // 0101
int result = ~a;
System.out.println("Result of ~a: " + result); // -6 (two's complement)
}
}

Explanation:

  • Converts 0 to 1 and 1 to 0.
  • Represents negative numbers in two’s complement form.

5. Shift Operators

Definition:
Shift operators move bits left or right.

  • Left Shift (<<): Shifts bits to the left, fills with 0.
  • Right Shift (>>): Shifts bits to the right, preserves sign.
  • Unsigned Right Shift (>>>): Shifts bits to the right, fills with 0.

Example Code:

public class BitwiseShift {
public static void main(String[] args) {
int a = 5; // 0101
System.out.println("a << 1: " + (a << 1)); // 10 (1010)
System.out.println("a >> 1: " + (a >> 1)); // 2 (0010)
System.out.println("a >>> 1: " + (a >>> 1)); // 2 (0010)
}
}

Explanation:

  • Left shift multiplies by 2, right shift divides by 2 (for positive numbers).
  • Useful in bit manipulation and optimization.

Summary Table

OperatorSymbolOperationExample
AND&Sets bit if both bits are 15 & 3 = 1
OR``Sets bit if any bit is 1
XOR^Sets bit if bits are different5 ^ 3 = 6
Complement~Inverts all bits~5 = -6
Left Shift<<Shifts bits left5 << 1 = 10
Right Shift>>Shifts bits right (signed)5 >> 1 = 2
Unsigned Right Shift>>>Shifts bits right (fills 0)5 >>> 1 = 2

Conclusion

Bitwise operators are powerful tools in Java for direct manipulation of bits. They are efficient and essential in scenarios like system programming, cryptography, and performance-critical code.

  • AND (&): Masking bits
  • OR (|): Setting bits
  • XOR (^): Toggling bits
  • Complement (~): Inverting bits
  • Shift operators (<<, >>, >>>): Efficient multiplication/division by powers of 2

Mastering these operators allows you to write optimized and low-level Java programs.


Leave a Reply

Your email address will not be published. Required fields are marked *


Macro Nepal Helper