Java Bitwise Operators Example Tutorial With Code Snippets

Robert Jul 13, 2026

Java's bitwise operators are instrumental in manipulating data at a binary level, offering powerful capabilities for tasks like system programming, network programming, and bit manipulation. Understanding these operators can significantly enhance your coding efficiency and problem-solving skills.

Java Programming Cheat Sheet
Java Programming Cheat Sheet

In this article, we delve into the world of Java bitwise operators, providing a comprehensive guide with clear examples to help you master these essential tools.

Bitwise Operators: Java
Bitwise Operators: Java

Bitwise Operators Overview

The Java programming language provides six bitwise operators that perform operations on binary representations of integers. These operators are AND, OR, XOR, NOT, LEFT SHIFT, and RIGHT SHIFT.

Bitwise Operator
Bitwise Operator

Java bitwise operators follow a specific precedence rule, where bitwise NOT (~) has the highest precedence, followed by bitwise shift operators, and then bitwise AND, XOR, and OR, which have the same precedence level as the arithmetic operators.

Bitwise AND (&)

Part 8: Bitwise Operators
Part 8: Bitwise Operators

The bitwise AND operator (&) takes two binary numbers as operands and performs the AND operation on every bit of two numbers. The result of AND is 1 only if both bits are 1, otherwise it is 0.

Example: ```java int a = 13; //binary 1101 int b = 7; //binary 0111 int c = a & b; //binary 0101 (5) ```

Bitwise OR (|)

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

The bitwise OR operator (|) takes two binary numbers as operands and performs the OR operation on every bit of two numbers. The result of OR is 1 if any of the two bits is 1.

Example: ```java int a = 13; //binary 1101 int b = 7; //binary 0111 int c = a | b; //binary 1111 (15) ```

Bitwise XOR (^)

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

The bitwise XOR operator (^) takes two binary numbers as operands and performs the XOR operation on every bit of two numbers. The result of XOR is 1 if the two bits are different. If the two bits are the same, the result is 0.

Example: ```java int a = 13; //binary 1101 int b = 7; //binary 0111 int c = a ^ b; //binary 1010 (10) ```

the bitwise operators list is shown
the bitwise operators list is shown
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 👈
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
Logical Operators in Java: Think Smarter, Code Better ☕🚀
Logical Operators in Java: Think Smarter, Code Better ☕🚀
Java Operators – Made Simple!
Java Operators – Made Simple!
Unary Operator in Java: Types, Examples
Unary Operator in Java: Types, Examples
[2020] - Web Technologies - Java Script - Operators Bitwise operators Practice
[2020] - Web Technologies - Java Script - Operators Bitwise operators Practice
three types of operators in jawa, with the caption's description below
three types of operators in jawa, with the caption's description below
Bitwise Operators in Java with Examples
Bitwise Operators in Java with Examples
a book cover with the words assignment operators in action
a book cover with the words assignment operators in action
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 for comparison operators true or false?
the cover for comparison operators true or false?
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 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
Java Cheat Sheet: Operators, Scanner & User Input ☕
Java Cheat Sheet: Operators, Scanner & User Input ☕
Python Bitwise Operators - A proper guide with syntax and examples
Python Bitwise Operators - A proper guide with syntax and examples
! Logical Operators !
! Logical Operators !
Operators Summary in Java ☕
Operators Summary in Java ☕

Bitwise NOT (~)

The bitwise NOT operator (~) takes one binary number as an operand and performs the NOT operation on every bit of that number. The result of NOT is 1 if the bit is 0, and 0 if the bit is 1.

Example: ```java int a = 13; //binary 1101 int c = ~a; //binary 10100 -> 10101 (complemented and 1 added) (-14) ```

Bitwise Shift Operators (<<, >>)

The bitwise left shift operator (<<) shifts all the bits of a number to the left by a specified number of times. The right side operand is the number of places to shift. The vacated bits on the right are filled with zeros. The bitwise right shift operator (>>) shifts all the bits of a number to the right by a specified number of times. The right side operand is the number of places to shift. The vacated bits on the left are filled with the leftmost bit value (sign bit).

Example: ```java int a = 13; //binary 1101 int c1 = a << 2; //binary 110100 (52) int c2 = a >> 2; //binary 0011 (3) ```

Mastering Java bitwise operators can greatly improve your coding skills and provide you with a toolset for optimizing your code and handling complex binary manipulation tasks. It is a core aspect of the language that every developer should be familiar with. Happy coding!