Java Right Shift Operator

In this chapter you will learn:

  1. How to shift bits in a value to the right
  2. Syntax for Right Shift Operator
  3. Example - Right Shift Operator

Description

The right shift operator, >>, shifts all of the bits in a value to the right a specified number of times.

Syntax

Its general form is shown here:


value >> num

num specifies the number of positions to right-shift.

Example

The following code fragment shifts the value 32 to the right by two positions:

 
public class Main {
  public static void main(String[] argv) {
/* w  w  w. j a  v  a  2s  .  c om*/
    int a = 32;
    a = a >> 2;
    System.out.println("a is " + a);

  }
}

The output:

Next chapter...

What you will learn in the next chapter:

  1. What is Unsigned Right Shift
  2. Syntax for Unsigned Right Shift
  3. Example - Unsigned Right Shift
Home »
  Java Tutorial »
    Java Langauge »
      Java Operator
Java Arithmetic Operators
Java Compound Assignment Operators
Java Increment and Decrement Operator
Java Logical Operators
Java Logical Operators Shortcut
Java Relational Operators
Java Bitwise Operators
Java Left Shift Operator
Java Right Shift Operator
Java Unsigned Right Shift
Java ternary operator