Master Bitwise Operators in Java with Practical Examples

Robert Jul 13, 2026

In the realm of computer programming, bitwise operators play a pivotal role in manipulating and analyzing binary data at the lowest level. Java, as a versatile and powerful programming language, supports these essential operators, enabling developers to perform intricate tasks with efficiency and precision. Let's delve into the world of bitwise operators in Java, exploring their applications and demonstrating their use with practical examples.

Bitwise Operators: Java
Bitwise Operators: Java

Before we decipher the intricacies of bitwise operators in Java, let's quickly recap the binary numbering system and basic bitwise operations. In binary, numbers are represented using only two digits: 0 and 1. Bitwise operations are performed on the binary representations of integers, which involve manipulating individual bits.

Bitwise Operator
Bitwise Operator

Understanding Bitwise Operators in Java

Java provides several bitwise operators to perform operations on bits. These operators include AND, OR, XOR, NOT, left shift (<<), right shift (>>), and zero-fill right shift (>>>). Let's explore each of these operators with their corresponding examples.

Part 8: Bitwise Operators
Part 8: Bitwise Operators

To understand these operators better, consider two 4-bit binary numbers: A (1011) and B (1101). We'll use these numbers in our examples to demonstrate how each bitwise operator transforms the binary data.

Bitwise AND (&)

Java Programming Cheat Sheet
Java Programming Cheat Sheet

The Bitwise AND operator (&) takes two binary numbers and performs a logical operation on each bit. It returns 1 only when both bits are 1; otherwise, it returns 0. In our given example, A & B will result in 1011 & 1101, which equals 1001 or 9 in decimal.

BitsABA & B
15-12111
11-8010
7-4100
3-0111
Result (Decimal)9

Bitwise OR (|)

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

The Bitwise OR operator (|) returns 1 if any of the two corresponding bits is 1. In other words, the result is 1, if at least one of the bits in the corresponding positions of the two operands is 1. In our example, A | B equals 1111 or 15 in decimal.

BitsABA | B
15-12111
11-8011
7-4101
3-0111
Result (Decimal)15

Advanced Bitwise Operations

Python Bitwise Operators - A proper guide with syntax and examples
Python Bitwise Operators - A proper guide with syntax and examples

In addition to the basic bitwise operators, Java offers several advanced operators to perform more complex tasks. Let's explore these operators and their use cases with examples.

Before proceeding, it's essential to understand that the right shift (>>) and zero-fill right shift (>>>पूर्णิล) operators differ in how they handle signed and unsigned integers. In Java, if the leftmost bit (MSB) is 1, the number is negative. The right shift operator (>>) retains the sign of the number by filling the vacated bits with the sign bit. In contrast, the zero-fill right shift operator (>>>पूर्णि) fills the vacated bits with 0s, treating the number as an unsigned integer.

☕ Java Operators Cheat Sheet | Master Java Operators Easily 🚀
☕ Java Operators Cheat Sheet | Master Java Operators Easily 🚀
the bitwise operators list is shown
the bitwise operators list is shown
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
List of Java Operators with Examples | Complete Guide for Beginners
List of Java Operators with Examples | Complete Guide for Beginners
Java Operators – Made Simple!
Java Operators – Made Simple!
Relational Operators in Java beginner friendly and easy to learn 🤩
Relational Operators in Java beginner friendly and easy to learn 🤩
Bitwise Operators in Python
Bitwise Operators in Python
three types of operators in jawa, with the caption's description below
three types of operators in jawa, with the caption's description below
[2020] - Web Technologies - Java Script - Operators Bitwise operators Practice
[2020] - Web Technologies - Java Script - Operators Bitwise operators Practice
Unary Operator in Java: Types, Examples
Unary Operator in Java: Types, Examples
Bitwise Operators in Java with Examples
Bitwise Operators in Java with Examples
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
a book cover with the words assignment operators in action
a book cover with the words assignment operators in action
the cover for comparison operators true or false?
the cover for comparison operators true or false?
Bitwise Operators
Bitwise Operators
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 cover of what are operators in jawa?, which includes stars and text
the cover of what are operators in jawa?, which includes stars and text
the front cover of an operating manual with text that reads unary operators single operand power
the front cover of an operating manual with text that reads unary operators single operand power
A complete guide for Bitwise operators in numpy
A complete guide for Bitwise operators in numpy
Bitwise Operators in C Programming
Bitwise Operators in C Programming

Bitwise XOR (^)

The Bitwise XOR operator (^) returns 1 if the corresponding bits are different. If both bits are the same, it returns 0. XOR is particularly useful for swapping binary numbers and finding the odd occurrences in an array. In our example, A ^ B equals 0110, or 6 in decimal.

Example: Swapping two numbers without using a temporary variable.

  • A = 5 (101), B = 7 (111)
  • A = A ^ B = 110 (6)
  • B = A ^ B = 101 (5)
  • A = A ^ B = 010 (2)

Bitwise NOT (~)

The Bitwise NOT operator (~) inverts all the bits in the binary representation of a number. It takes one operand and returns its 2's complement. In our example, ~A equals 0010, or -10 - 1 = -11 in decimal (since we're dealing with 4-bit numbers, -11 is the 2's complement of our example number 9).

Left Shift (<<)

The left shift operator (<<) shifts all the bits of the number to the left by the specified number of positions, filling vacated bits with 0s. Each left shift operation shifts bits left by one position, effectively multiplying the number by 2. In our example, A << 2 equals 101100, or 44 in decimal.

Right Shift (>>)

The right shift operator (>>) moves all the bits of the number to the right by the specified number of positions, filling vacated bits with the sign bit (for signed integers) or 0s (for unsigned integers). Each right shift operation divides the number by 2.

BitsAA >> 1
15-1210
11-811
7-400
3-011
Result (Decimal)94

Zero-Fill Right Shift (>>>)

The zero-fill right shift operator (>>>पूर्णि) performs the same operation as the right shift operator (>>), except it fills vacated bits with 0s, treating the number as an unsigned integer. In our example, A >>> 1 equals 00111 (19 in decimal).

BitsAA >>> 1
15-1210
11-800
7-411
3-011
Result (Decimal)919

Bitwise operators in Java can significantly enhance your programming prowess by enabling you to manipulate and analyze binary data, allowing for more efficient and powerful solutions. Mastering these essential operators equips you with the necessary tools to tackle a variety of tasks, from optimizations and data manipulation to cryptography and error detection.

As you conclude your learning journey on bitwise operators in Java, remember that versatility and a comprehensive understanding of these operations empower you to approach problems from unique perspectives. Keep exploring and experimenting with these operators, and unlock the full potential of Java and its vast ecosystem. Happy coding!