← Back to Home

Bitwise Operators

Bitwise operators in Java perform operations at the bit level. They are used for low-level programming, performance optimization, flags, masking, and systems-related logic. Although used less frequently in business applications, they are important for interviews and core understanding.

What Are Bitwise Operators?

  • Operate on individual bits of integral data types
  • Work with byte, short, int, long, and char
  • Not applicable to boolean (except & and | in logical context)

List of Bitwise Operators in Java

Operator Name Description
& Bitwise AND 1 if both bits are 1
| Bitwise OR 1 if any bit is 1
^ Bitwise XOR 1 if bits are different
~ Bitwise Complement Inverts bits
<< Left Shift Shifts bits left
>> Right Shift (Signed) Shifts bits right, preserves sign
>>> Unsigned Right Shift Shifts bits right, fills with 0

1. Bitwise AND (&)

  • Result bit is 1 only if both bits are 1
int a = 5;   // 0101
int b = 3;   // 0011
int c = a & b;  // 0001 = 1
          

2. Bitwise OR (|)

  • Result bit is 1 if any bit is 1
int a = 5;   // 0101
int b = 3;   // 0011
int c = a | b;  // 0111 = 7
          

3. Bitwise XOR (^)

  • Result bit is 1 if bits are different
int a = 5;   // 0101
int b = 3;   // 0011
int c = a ^ b;  // 0110 = 6
          

XOR Special Properties (Interview Favorite)

  • x ^ x = 0
  • x ^ 0 = x
  • Used for swapping values
int a = 5;
int b = 3;

a = a ^ b;
b = a ^ b;
a = a ^ b;
          

4. Bitwise Complement (~)

  • Inverts all bits
  • Produces two’s complement result
int a = 5;   // 00000101
int b = ~a;  // 11111010 = -6
          

Why -6?

Because ~x = -(x + 1)

5. Left Shift (<<)

  • Shifts bits to the left
  • Fills rightmost bits with 0
  • Equivalent to multiplying by 2n
int a = 5;   // 0101
int b = a << 1; // 1010 = 10
          

6. Right Shift (>>) — Signed

  • Shifts bits to the right
  • Preserves the sign bit
int a = -8;
int b = a >> 1;  // -4
          

7. Unsigned Right Shift (>>>)

  • Shifts bits to the right
  • Fills leftmost bits with 0
  • Treats number as unsigned
int a = -8;
int b = a >>> 1; // Large positive number
          

Bitwise Operators vs Logical Operators (Important)

Operator Type Short-Circuit
& Bitwise / Boolean ❌ No
| Bitwise / Boolean ❌ No
&& Logical ✅ Yes
|| Logical ✅ Yes

Common Use Cases

  • Flags and permissions
  • Masking bits
  • Performance optimizations
  • Cryptography basics
  • Low-level system logic

Common Beginner Mistakes

  • Confusing & with &&
  • Confusing | with ||
  • Ignoring sign bit behavior
  • Misusing shift operators
  • Overusing bitwise operators in business logic

Interview-Ready Answers

Short Answer

Bitwise operators perform operations on individual bits of integer data types.

Detailed Answer

Java bitwise operators such as &, |, ^, ~, <<, >>, and >>> operate at the binary level. They are used for masking, shifting, and low-level computations and differ from logical operators in behavior and use cases.

Key Takeaway

Bitwise operators are powerful but low-level. Understanding their behavior helps in performance tuning, system-level coding, and cracking interview questions.