Java Bitwise Operator Examples: Master AND OR XOR Operations Quickly

Robert Jul 13, 2026

Bitwise operators in Java are fundamental to low-level programming, enabling bit-by-bit manipulation of integers. They are binary operators that operate on bits, providing operations like AND, OR, XOR, and NOT, among others. Mastery of these operators helps enhance code efficiency, particularly in algorithms dealing with bits and bytes.

Bitwise Operator
Bitwise Operator

The Java programming language provides six types of bitwise operators. Let's delve into each, explaining their function and providing illustrative examples.

Bitwise Operators: Java
Bitwise Operators: Java

Binary Operators

Binary operators work on two operands - binary AND (&), OR (|), XOR (^), Left Shift (<<), and Right Shift (>>).

Java Programming Cheat Sheet
Java Programming Cheat Sheet

Here are examples of these operators:

Bitwise AND (&)

☕ Java Operators Cheat Sheet | Master Java Operators Easily 🚀
☕ Java Operators Cheat Sheet | Master Java Operators Easily 🚀

The '&' operator performs AND on bits of two integers. The result is 1 only if both bits are 1.

Example: ``` int a = 60, b = 13, c = 0; c = a & b; System.out.println(c); // Outputs 12 ``` This example uses the bitwise AND operator. The binary representation of 60 is '0011 1100' and for 13, it's '0000 1101'. So, the result '0000 1100' is 12.

Bitwise OR (|)

Part 8: Bitwise Operators
Part 8: Bitwise Operators

The '|' operator performs OR on bits of two integers. The result is 1 if any of the two bits is 1.

Example: ``` int a = 60, b = 13, c = 0; c = a | b; System.out.println(c); // Outputs 61 ``` Here, the binary representation of 60 is '0011 1100' and for 13, it's '0000 1101'. The result '0011 1101' is 61.

Unary Operator

the bitwise operators list is shown
the bitwise operators list is shown

Unary operators perform operation on single operand - Bitwise NOT (~), Unary Plus (+), and Unary Minus (-).

Bitwise NOT (~)

the book cover for bitwise operators for binny pros, which includes an image of
the book cover for bitwise operators for binny pros, which includes an image of
List of Java Operators with Examples | Complete Guide for Beginners
List of Java Operators with Examples | Complete Guide for Beginners
👉 Operators in java 👈
👉 Operators in java 👈
Unary Operator in Java: Types, Examples
Unary Operator in Java: Types, Examples
Logical Operators in Java: Think Smarter, Code Better ☕🚀
Logical Operators in Java: Think Smarter, Code Better ☕🚀
the cover for comparison operators true or false?
the cover for comparison operators true or false?
Python Bitwise Operators - A proper guide with syntax and examples
Python Bitwise Operators - A proper guide with syntax and examples
[2020] - Web Technologies - Java Script - Operators Bitwise operators Practice
[2020] - Web Technologies - Java Script - Operators Bitwise operators Practice
Using bitwise operators in JavaScript
Using bitwise operators in JavaScript
Operators in JAVA
Operators in JAVA
a poster with different types of writing and numbers on it, including the words'jwa beginner notes '
a poster with different types of writing and numbers on it, including the words'jwa beginner notes '
Java Operators – Made Simple!
Java Operators – Made Simple!
java array operations
java array operations
🚀 Master Java Arithmetic Operators | Java Basics for Beginners
🚀 Master Java Arithmetic Operators | Java Basics for Beginners
Java Cheatsheet
Java Cheatsheet
The OOP'S Concept in JAVA🤔💯✅👇
The OOP'S Concept in JAVA🤔💯✅👇
Bitwise Operators in C Programming
Bitwise Operators in C Programming
Operators Summary in Java ☕
Operators Summary in Java ☕
Java Project Ideas For Beginners
Java Project Ideas For Beginners
Java Homework Help – Step-by-Step Coding Solutions & Guides
Java Homework Help – Step-by-Step Coding Solutions & Guides

The '~' operator inverts all the bits in an integer, effectively adding one to the two's complement of the number.

Example: ``` int a = 60, b; b = ~a; System.out.println(b); // Outputs -61 ``` Here, '~60' signifies that all bits should be inverted, logically making it '-61'.

Remember, bitwise operators cast float or double numbers to int before using them as bitwise operators.

Shift Operators

Shift operators move bits of the number towards the left or right.

Left Shift (<<)

Shifts the bits of the number to the left.

Example: ``` int a = 60, b; b = a << 2; System.out.println(b); // Outputs 240 ``` This example shifts '60' to the left by 2 positions, resulting in '240'.

Right Shift (>>)

Shifts the bits of the number to the right.

Example: ``` int a = 60, b; b = a >> 2; System.out.println(b); // Outputs 15 ``` Here, '60' is shifted to the right by 2 positions, resulting in '15'.

Understanding bitwise operators empowers you to delve into system programming, enhance algorithmic efficiency, and aids in understanding low-level functioning of computer systems. Keep exploring and practicing to master these operators!