Master Bitwise Operators in Java with Practical Examples

Robert Jul 13, 2026

In Java, bitwise operators are essential to manipulating binary representations of integer types. They enable accomplishments like efficient data manipulation, data compression, and network protocol implementation, among others. Here, we'll delve into Java's bitwise operators, their functionalities, and furnished examples to illustrate their usage.

Bitwise Operators: Java
Bitwise Operators: Java

Java provides six bitwise operators - AND (&), OR (~), XOR (^), NOT (!), left shift (<<), and right shift (>>). They perform operations on bits, the fundamental units of digital information, binary digits that can be either 0 or 1. Let's explore each operator with respective examples.

Bitwise Operator
Bitwise Operator

Unary Operators

Unary operators require a single operand and perform operations like arithmetic negation or logical complement. We have two unary operators in Java: NOT (!) and Bitwise NOT ( inversion-operation).

Part 8: Bitwise Operators
Part 8: Bitwise Operators

1. The NOT (!) operator performs logical negation on boolean data types, and bitwise complement for integer types.

Example: ```java int x = 15; // Binary representation: 1111 System.out.println(!x); // prints 'false' System.out.println(!(x & 8)); // prints 'true' - bitwise AND operation ```

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

2. The Bitwise NOT (~ operator) inverts each bit, so every 0 becomes 1 and every 1 becomes 0.

Bitwise AND (&)

The Bitwise AND operation (&) interprets the binary representations of two integers. It yields 1 only if both corresponding bits are 1; otherwise, the result is 0.

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

Example: ```java int x = 60; // Binary representation: 111100 int y = 13; // Binary representation: 00001101 System.out.println(x & y); // prints '1000' which is 8 in decimal ```

Bitwise OR (|)

The Bitwise OR operation (|) evaluates to 1 if either bit is 1, resulting in a binary number with 1s in all set positions.

[2020] - Web Technologies - Java Script - Operators Bitwise operators Practice
[2020] - Web Technologies - Java Script - Operators Bitwise operators Practice

Example: ```java int x = 60; // Binary representation: 111100 int y = 13; // Binary representation: 00001101 System.out.println(x | y); // prints '111111' which is 63 in decimal ```

Bitwise XOR (^)

List of Java Operators with Examples | Complete Guide for Beginners
List of Java Operators with Examples | Complete Guide for Beginners
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
👉 Operators in java 👈
👉 Operators in java 👈
Relational Operators in Java beginner friendly and easy to learn 🤩
Relational Operators in Java beginner friendly and easy to learn 🤩
Bitwise Operators in Java with Examples
Bitwise Operators in Java with Examples
Python Bitwise Operators - A proper guide with syntax and examples
Python Bitwise Operators - A proper guide with syntax and examples
Java Programming Cheat Sheet
Java Programming Cheat Sheet
a book cover with the words'local operators - making decision'in white letters
a book cover with the words'local operators - making decision'in white letters
Bitwise Operators
Bitwise Operators
Unary Operator in Java: Types, Examples
Unary Operator in Java: Types, Examples
Java Operators – Made Simple!
Java Operators – Made Simple!
Logical Operators in Java: Think Smarter, Code Better ☕🚀
Logical Operators in Java: Think Smarter, Code Better ☕🚀
a book cover with the words assignment operators in action
a book cover with the words assignment operators in action
three types of operators in jawa, with the caption's description below
three types of operators in jawa, with the caption's description below
the cover for comparison operators true or false?
the cover for comparison operators true or false?
Bitwise Operators in Python
Bitwise Operators in Python
A complete guide for Bitwise operators in numpy
A complete guide for Bitwise operators in numpy
Using bitwise operators in JavaScript
Using bitwise operators in JavaScript
the cover of an instruction manual on how to use arrhmetric operators - do the math
the cover of an instruction manual on how to use arrhmetric operators - do the math
the cover of an ebook with text that reads, increment & decrement operators
the cover of an ebook with text that reads, increment & decrement operators

The Bitwise XOR operation (^) results in 1 if the corresponding bits of two integers are different, yielding 0 if they're identical.

Example: ```java int x = 60; // Binary representation: 111100 int y = 13; // Binary representation: 00001101 System.out.println(x ^ y); // prints '1001' which is 9 in decimal ```

Left Shift (<<)

The Left Shift operation (<<) moves the bits of the number to the left by specified places, filling on the right with zeros.

Example: ```java int x = 60; // Binary representation: 111100 System.out.println(x << 2); // prints '100100' which is 36 in decimal ```

Right Shift (>>)

The Right Shift operation (>>) Move the bits of the number to the right by specified places. Binary numbers are represented using 32 bits, so moving bits to the right fills up with zeros.

Example: ```java int x = 60; // Binary representation: 111100 System.out.println(x >> 2); // prints '11110' which is 48 in decimal ```

By mastering these bitwise operators, you can exponentially enhance Java programs' efficiency and precision. Grab a pack of peanuts (/reference to a binary joke ;-)), crack Java's nut, and continue exploring Java programming!