BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Bitwise Operators in Java with Examples

Last Updated: October 15, 2022 by Chaitanya Singh | Filed Under: java

Bitwise operators are used to perform bit-level operations. Let’s say you are performing an AND operation on two numbers (a & b), then these numbers are converted into binary numbers and then the AND operation is performed. Finally, the compiler returns decimal equivalent of the output binary number.

Bitwise Operators in Java

Bitwise Operators list in Java

1. Bitwise AND & Operator

In the following example, we are finding out the bitwise AND of two integers 6 and 10. In bitwise AND operation, numbers are compared bit by bit. The output bit of bitwise AND is 1, if the corresponding bits of two operands is 1. If either bit of an operand is 0, the output bit is 0.

6  = 00000110 (In Binary)
10 = 00001010 (In Binary)

Bitwise AND Operation of 6 and 10
  00000110
& 00001010
  ________
  00000010  = 2 (In decimal)

Java Program:

public class JavaExample {
  public static void main(String[] args) {
    int num1 = 6, num2 = 10;

    //Bitwise AND Operation on two integers
    System.out.println("Value of num1 & num2: "+ (num1 & num2));
  }
}

Output:

Value of num1 & num2: 2

2. Bitwise OR | Operator

In bitwise OR operator, the output bit is 0 if the corresponding bits of both the operands are 0. If either bit of an operand is 1, the output bit is 1. Let’s find the Bitwise OR of numbers 6 and 10

6  = 00000110 (In Binary)
10 = 00001010 (In Binary)

Bitwise OR Operation of 6 and 10
  00000110
| 00001010
  ________
  00001110  = 14 (In decimal)

Java Program:

public class JavaExample {
  public static void main(String[] args) {
    int num1 = 6, num2 = 10;

    //Bitwise OR of 6 and 10
    System.out.println("Value of num1|num2: "+ (num1|num2));
  }
}

Output:

Value of num1|num2: 14

3. Bitwise XOR ^ Operator

Bitwise XOR operator is also known as exclusive OR. The output bit of Bitwise XOR is 1 if the corresponding bits of two operands are opposite and the output bit is 0 if the corresponding bits are same.

6  = 00000110 (In Binary)
10 = 00001010 (In Binary)

Bitwise OR Operation of 6 and 10
  00000110
^ 00001010
  ________
  00001100  = 12 (In decimal)

Java Program:

public class JavaExample {
  public static void main(String[] args) {
    int num1 = 6, num2 = 10;

    //Bitwise Exclusive OR of numbers 6 and 10
    System.out.println("Value of num1^num2: "+ (num1^num2));
  }
}

Output:

Value of num1^num2: 12

4. Bitwise Complement ~ Operator

Bitwise complement is a unary operator . It works by changing 1’s to 0’s and 0’s to 1’s. It is denoted by ~ (tilde) symbol.

38 = 00100110 (In Binary)
Bitwise complement(~) of Integer 38: In complement, the bits are reversed 1's become 0's and 0's become 1's.
~ 00100110 
  ________
  11011001  = 217 (In decimal)

However, the compiler prints the 2’s complement of this number.

2's complement of 217 = 1's complement of 217 + 1
       = 1's of (11011001) + 1
       = 00100110 + 1
       = 00100111
       = -39 (2's complement are represented by negative sign)

Java Program:

public class JavaExample {
  public static void main(String[] args) {
    int num = 38;

    //displaying bitwise complement of num
    System.out.println("Bitwise complement of 38 is: "+(~num));
  }
}

Output:

Bitwise complement of 38 is: -39

5. Bitwise Shift Operators

Left Shift: Left shift operator is denoted by << symbol. It shifts all bits towards left by a certain number of specified bits, for example: num<<2 will shift the bits to the left by two positions. The bit positions that have been vacated by the left shift operator are filled with 0’s.

38     = 00100110 (In binary)
38<<2  = 10011000 (In binary) [Left shift by two bits]
       = 152 (In decimal)

Right Shift: Right shift operator is denoted by >> symbol. It shifts all bits towards right by certain number of specified bits. For example: num>>2 will shift the bits to the right by two positions. The bit positions that have been vacated by the right shift operator are filled with 0’s.

38    = 00100110 (In binary)
38>>2 = 00001001 (In binary) [Right shift by two bits]
      = 9 (In decimal)

Java Program:

public class JavaExample {
  public static void main(String[] args) {
    int num = 38;

    //Left shift
    System.out.println("num<<2: "+(num<<2));

    //Right shift
    System.out.println("num>>2: "+(num>>2));
  }
}

Output:

num<<2: 152 num>>2: 9

Recommended Posts

  • Arithmetic Operators in Java with Examples
  • Assignment Operators in Java with Examples
  • Unary Operators in Java with Examples
  • Logical Operators in Java with Examples
  • Relational Operators in Java with Examples
  • Ternary Operator in Java with Examples
  • Shift Operators in Java with Examples
❮ Operators in Java

Top Related Articles:

  1. Java Variables: Declaration, Scope, and Naming Conventions
  2. Logical Operators in Java with Examples
  3. Ternary Operator in Java with Examples
  4. Unary Operators in Java with Examples
  5. Can Static Methods be Overloaded or Overridden in Java?

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap