🔍 Mastering Bitwise Operators in Java — A Developer’s Perspective

🔍 Mastering Bitwise Operators in Java — A Developer’s Perspective

As a developer diving deep into Java, I found myself confused between Logical Operators, and Bitwise Operators. Logical operators like && and || are easy to grasp — but bitwise ones like &, I, ^, ~, <<, >>, and >>>? Not so much.

So, I decided to break it all down and share it with fellow developers and learners like you. If you're looking to master Java's bitwise operators, this guide is for you. 💡


🧠 Logical vs Bitwise – What's the Difference?

Article content

🔸 1. Bitwise AND (&)

Compares each bit of two numbers. Returns 1 only if both bits are 1.

int a = 5;      // 0101
int b = 3;      // 0011
int result = a & b; // 0001 = 1
System.out.println(result); // 1        

🔸 2. Bitwise OR (|)

Returns 1 if at least one corresponding bit is 1.

int a = 5;      // 0101
int b = 3;      // 0011
int result = a | b; // 0111 = 7
System.out.println(result); // 7        

🔸 3. Bitwise XOR (^)

Returns 1 if the bits are different, otherwise 0.

int a = 5;      // 0101
int b = 3;      // 0011
int result = a ^ b; // 0110 = 6
System.out.println(result); // 6        

🔸 4. Bitwise Complement (~)

Invert all bits. This is a unary operator (works on a single operand).

int a = 5;           // 00000000 00000000 00000000 00000101
int result = ~a;     // 11111111 11111111 11111111 11111010
System.out.println(result); // -6        
💡 Java uses 2's complement to represent negative numbers — that’s why ~5 becomes -6.

🔸 5. Left Shift (<<)

Shifts bits to the left and fills zeros from the right.

int a = 5;      // 00000101
int result = a << 1; // 00001010 = 10
System.out.println(result); // 10        

🔸 6. Signed Right Shift (>>)

Shifts bits to the right and keeps the sign bit (MSB) unchanged.

int a = -8;     // 11111000 (in 8-bit, conceptually)
int result = a >> 2;
System.out.println(result); // -2        

🔸 7. Unsigned Right Shift (>>>)

Shifts bits to the right, but fills the leftmost bits with 0s, ignoring the sign.

int a = -8;
int result = a >>> 2;
System.out.println(result); // 1073741822        

⚠️ Bonus: Why Not Use Bitwise Operators on byte or short?

Java promotes smaller data types (byte, short, char) to int before applying bitwise operations. This behavior prevents data loss and ensures consistent operations across platforms.

byte a = 5;
byte b = 3;
// byte result = a & b; // ❌ Compile error
int result = a & b;     // ✅ Works
byte finalResult = (byte)(a & b); // ✅ After casting        
✅ Bitwise operations work with long — but use L suffix (10L) and ensure both operands are long.

🆚 Quick Comparison: Logical vs Bitwise Operators


Article content

🎯 Final Thoughts

Understanding bitwise operators might seem niche — but they’re super powerful in situations like:

  • Low-level programming
  • Performance optimizations
  • Cryptographic algorithms
  • Game development
  • Device driver logicIf you're serious about mastering Java, this knowledge will set you apart.


💬 What Do You Think?

If this article helped clarify the mystery of bitwise operators, drop a comment or reaction! If you have suggestions for another Java topic to simplify next, I’d love to hear your ideas.

🔗 Also published on Medium and Dev.to — check them out!


To view or add a comment, sign in

More articles by Sandaruwan Wadiyage

Others also viewed

Explore content categories