CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > Java > Bitwise and Shift Operator in Java

Java Tutorial

  • Introduction
    • What is Java
    • History Of Java
    • Install Java
    • What is JVM
    • JDK vs JRE vs JVM
    • Java Bytecode
    • OOP vs POP
    • Compile and Run Java
  • Tokens, Expressions and Control Structures
    • Primitive data types
    • Integers
    • Floating Points
    • Characters
    • Booleans
    • User Defined Data Type
    • Declarations
    • Constants
    • Identifiers
    • Literals
    • Type Conversion and Casting
    • Variables
    • Default Variable Initialization
    • Command Line Arguments
    • Arrays of Primitive Types
    • Comment Syntax
    • Garbage Collection
    • Expressions
    • Operators
    • Arithmetic Operator
    • Bitwise and Shift Operator
    • Comparison or Relational Operators
    • Logical Operators
    • Assignment Operators
    • Ternary Operator
    • Increment and Decrement Operator
    • Control Statements
  • OOP Concepts
    • Class and Object
    • Create Class Instance
    • Method
    • Abstraction
    • Encapsulation
    • this keyword
    • Constructor
    • Pass by Value
    • Access Modifier/Control
    • Polymorphism
    • Method Overloading vs Method Overriding
    • Recursion
    • Nested and Inner Class
  • Inheritance and Packaging
    • Inheritance
    • extends Keyword
    • super Keyword
    • Object Class
    • Abstract class
    • Final Class
    • Java Package
    • Interface
  • Handling Error/ Exceptions
    • What is Exception
    • Exception Handling Keywords
    • Common Java Errors
    • User Defined Exception
    • Throwing and re-throwing Exception
    • finally Block
  • Strings
    • Java String Tutorial
  • Threads
    • Introduction
    • Create Thread
    • Thread Lifecycle
    • Thread Priority
    • Thread Synchronization
    • Inner Thread Communication
    • Thread Deadlock
  • IO and Streams
    • java.io Package
    • Files and Directories
    • Byte Stream
    • Character Stream
    • Console Input and Output
    • Serializable and Deserializable
  • Core Packages
    • java.lang Package
    • Math
    • Wrapper Classes
    • java.lang.Number
    • Double
    • Float
    • Integers
    • java.lang.Byte
    • java.lang.Short
    • java.lang.Long
    • java.lang.Character
    • java.lang.Boolean
    • java.util package
    • Vector Class
    • Stack Class
    • Dictionary Class
    • Hashtable
    • Enumeration or Enum
    • Generate Random Number
  • Holding Collection of Data
    • Arrays
    • Map
    • List
    • Set
    • Collection Interface
    • Collections Class
    • ArrayList
    • HashSet
    • TreeSet
    • Comparator
  • Java Bean
    • What is Java Bean
    • Advantages and Disadvantages of Java Bean
    • Java Beans API
    • Introspection
    • Java Bean Properties
    • Bound and Constrained Properties
    • BeanInfo Interface
    • Customizers
    • Java Beans Persistence
    • BeanDescriptor
  • Home

Bitwise and Shift Operator in Java

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Last updated: July 1, 2026 · 4 min read · 0 Comments
Share: in X

Bitwise and shift operator in Java are used to perform low-level bit manipulation on integers. These operators work on individual bits of integer values and are often used in scenarios where we need to perform operations at the binary level for example low-level hardware.

Table of Contents

  • Bitwise AND operator (&)
  • Bitwise OR operator (|)
  • Bitwise XOR operator (^)
  • Bitwise NOT operator (~)
  • Left shift operator (<<)
  • Right shift operator (>>)
  • Unsigned right shift operator (>>>)

Bitwise AND operator (&)

The bitwise AND operator compares each bit of two integer operands and produces a new integer with a 1-bit only if both corresponding bits in the operands are 1.

Example:

class Test {
    public static void main(String[] args) {
        int a = 5;  // Binary: 0101
        int b = 3;  // Binary: 0011
        int result = a & b;  // Binary: 0001 (Decimal: 1)
        System.out.println(result);
    }
}

Output:

1

In the above example, a & b results in 1 because only the last bit is 1 in both a and b.

Bitwise OR operator (|)

The bitwise OR operator compares each bit of two integer operands and produces a new integer with a 1-bit if either of the corresponding bits in the operands is 1.

Example:

class Test {
    public static void main(String[] args) {
        int a = 5;  // Binary: 0101
        int b = 3;  // Binary: 0011
        int result = a | b;  // Binary: 0111 (Decimal: 7)
        System.out.println(result);
    }
}

Output:

7

In the above example, a | b results in 7 because the binary representation of a and b have their corresponding bits set to 1 at some positions, and these 1-bits are combined in the result.

Bitwise XOR operator (^)

The bitwise XOR (exclusive OR) operator compares each bit of two integer operands and produces a new integer with a 1-bit only if the corresponding bits in the operands differ.

Example:

class Test {
    public static void main(String[] args) {
        int a = 5;  // Binary: 0101
        int b = 3;  // Binary: 0011
        int result = a ^ b;  // Binary: 0110 (Decimal: 6)
        System.out.println(result);
    }
}

Output:

6

In the above example, a ^ b results in 6 because the binary representation of a and b have their corresponding bits different at some positions, and these differing bits are combined in the result.

Bitwise NOT operator (~)

The bitwise NOT operator performs a one’s complement operation, which flips each bit of the operand.

Example:

class Test {
    public static void main(String[] args) {
        int a = 5;   // Binary: 0101
        int result = ~a;  // Binary: 1010 (Decimal: -6)
        System.out.println(result);
    }
}

Output:

-6

In the above example, ~a results in -6. This happens because Java uses a two’s complement representation for negative numbers, and the bitwise NOT operation inverts all bits, including the sign bit, which results in a negative value.

Left shift operator (<<)

The left shift operator shifts the bits of the left-hand operand to the left by the number of positions specified on the right-hand side.

Example:

class Test {
    public static void main(String[] args) {
        int a = 5;       // Binary: 0101
        int shifted = a << 2;  // Binary: 010100 (Decimal: 20)
        System.out.println(shifted);
    }
}

Output:

20

In the above example, a << 2 shifts the bits of a two positions to the left, and the result is 20.

Right shift operator (>>)

The right shift operator shifts the bits of the left-hand operand to the right by the number of positions specified on the right-hand side. It performs arithmetic right shift, meaning the sign bit is replicated to preserve the sign of the number.

Example:

class Test {
    public static void main(String[] args) {
        int a = 40; // 101000 in binary
        int b = a >> 2; // 1010 in binary

        System.out.println(b); // 10
    }
}

Output:

10

In the above example, a >> 2 shifts the bits of a two positions to the right, and the result is -3.

Unsigned right shift operator (>>>)

The unsigned right shift operator shifts the bits of the left-hand operand to the right by the number of positions specified on the right-hand side. It performs logical right shift, and the leftmost positions are filled with zeros.

Example:

class Test {
    public static void main(String[] args) {
        int a = -10;       // Binary: 11111111111111111111111111110110
        int shifted = a >>> 2;  // Binary: 00111111111111111111111111111101 (Decimal: 1073741821)
        System.out.println(shifted);
    }
}

Output:

1073741821

In the above example, a >>> 2 shifts the bits of a two positions to the right, and the result is 1073741821.

Related Posts:

  • Operators in Java
  • Increment and Decrement Operator in Java
  • Integers In Java
  • == vs .equals() in Java: What’s the Difference?
  • How to Install Ollama on Windows, Linux, and macOS:…
  • Byte Stream in Java
Tags:javalanguage-fundamentals
Was this article helpful?
← Previous ArticleException Handling in Java
Next Article →How to clear cache from Ubuntu?

Leave a Comment Cancel reply

You must be logged in to post a comment.

Recent Posts

  • How to implement Passwordless Authentication in Spring Boot: A Step-by-Step Guide
  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Linkedin

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
© 2026 CoderSathi. All rights reserved. Privacy Policy · Sitemap