Ad space (banner)
Java Lessons
Lesson 54 / 68

Bitwise Operators (AND, OR, XOR, shifts)

This lesson covers the basics of Java's bitwise operators — AND, OR, XOR, and shifts — so you understand the idea of manipulating a number bit by bit. It's for anyone searching "Java bitwise operator usage."

Bitwise operators treat a number as binary and compute bit by bit. There's & (AND), | (OR), ^ (XOR), and << (left shift), commonly used for things like managing permission flags and low-level operations.

The example shows the results of &, |, and ^ on the integers 12 and 10, plus the result of a shift operation. Thinking about it in binary makes each operation's meaning much clearer.

A common early mistake is confusing the logical operators &&/|| with the bitwise operators &/|. Use &&/|| for conditionals, and &/|/^ for bit-level operations — a distinction worth being conscious of.

In real projects, you won't reach for these often in typical app development, but they're valuable knowledge for performance-critical logic.

📖 Reference code
✍️ Your code
Type your code, then press "Run"

🧪 This site can't compile or run Java directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser — nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)