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

Shift Operators in Java with Examples

By Chaitanya Singh | Filed Under: java

Shift Operators in Java

Shift operators are used to perform bit manipulation. In this guide, we will discuss various shift operators in java with the help of examples. Java supports following shift operators:

1. Signed Left Shift Operator (<<) Example

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 of number num 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)

Program:

public class JavaExample {
  public static void main(String[] args)
  {
    int x = 38, y = -38;

    //left shift by two positions, for the negative number
    //y, the sign is preserved in signed left shift
    System.out.println("x<<2: " + (x<<2));
    System.out.println("y<<2: " + (y<<2));
  }
}

Output:

x<<2: 152
y<<2: -152

2. Signed Right Shift Operator (>>) Example

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.

32    = 00100000 (In binary)
32>>2 = 00001000 (In binary) [Right shift by two bits]
      = 8 (In decimal)

Program:

public class JavaExample {
  public static void main(String[] args)
  {
    int x = 32, y = -32;

    //Right shift by two positions, for the negative number
    //y, the sign is preserved in signed right shift
    System.out.println("x>>2: " + (x>>2));
    System.out.println("y>>2: " + (y>>2));
  }
}

Output:

x>>2: 8
y>>2: -8

3. Unsigned Right Shift Operator (>>>) Example

In Unsigned right shift operator, the sign bit is not preserved, thus the result of right shift is different between signed and unsigned shifts.

public class JavaExample {
  public static void main(String[] args)
  {
    int x = 32, y = -32;

    //Unsigned right shift
    System.out.println("x>>>2: " + (x>>>2));
    System.out.println("y>>>2: " + (y>>>2));
  }
}

Output:

output unsigned right shift

4. Unsigned Left Shift Operator (<<<) Example

There is no unsigned left shift operator <<< in java, this is because the result of << and <<< would produce same result.

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
  • Bitwise Operators in Java with Examples
  • Ternary Operator in Java with Examples
❮ Operators in Java

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 – 2022 BeginnersBook . Privacy Policy . Sitemap