Shift Operations - Left, Right or Unsigned Right

This section describes shift operations: 'Left Shift', 'Right Shift', and 'Unsigned Right Shift'. A tutorial example is provided to show you how shift operations work on 'int' data type values.

What is a shift operation? A shift operation is an operation that requires the operand to be represented in a binary format, viewed as a bit string, then shift all bit values to the left or right.

Again, Java defines shift operations on "int" data type values, not on "byte" data type values. So a shift operation requires that the involved "int" value to be represented into a bit string of 32 bits. And the operation will be applied to shift bit values to the left or right on the bit string.

Java defines 3 shift operations called: "Left Shift", "Right Shift", and "Unsigned Right Shift":

1. Shift operation "Left Shift" - "<<": The "Left Shift" operation on an "int" value can be described as:

The following diagram gives an example of a "<<" operation in 2 formats:

  Decimal             Binary
 -858993460  << 6     11001100110011001100110011001100 << 6
-----------------   ---------------------------------------
= 858993408         = 00110011001100110011001100000000

2. Shift operation "Right Shift" - ">>": The "Right Shift" operation on an "int" value can be described as:

The following diagram gives an example of a ">>" operation in 2 formats:

  Decimal             Binary
 -858993460  >> 6     11001100110011001100110011001100 >> 6
-----------------   ---------------------------------------
=4281545523         = 11111111001100110011001100110011

3. Shift operation "Unsigned Right Shift" - ">>>": The "Unsigned Right Shift" operation on an "int" value can be described as:

The following diagram gives an example of a ">>>" operation in 2 formats:

  Decimal             Binary
 -858993460  >>> 6     11001100110011001100110011001100 >>> 6
-----------------   ---------------------------------------
=  53687091         = 00000011001100110011001100110011

To verify the examples given above, I wrote the following test program, ShiftOperations.java:

/* ShiftOperations.java
 * Copyright (c) HerongYang.com. All Rights Reserved.
 */
public class ShiftOperations {
   public static void main(String[] arg) {
      int a = -858993460; // binary: 11001100110011001100110011001100

      int o1 = a << 6;  // left shift
      int o2 = a >> 6;  // right shift
      int o3 = a >>> 6; // unsigned right shift

      System.out.println(" a: "+getBitString(a) +" = "+a);
      System.out.println("o1: "+getBitString(o1) +" = "+o1);
      System.out.println("o2: "+getBitString(o2) +" = "+o2);
      System.out.println("o3: "+getBitString(o3) +" = "+o3);
   }
   private static String getBitString(int x) {
      StringBuffer buf = new StringBuffer();
      for (int i=1; i<=32; i++) buf.append(x>>>(32-i) & 0x00000001);
      return buf.toString();
   }
}

The output of the test program matches my earlier examples:

 a: 11001100110011001100110011001100 = -858993460
o1: 00110011001100110011001100000000 = 858993408
o2: 11111111001100110011001100110011 = -13421773
o3: 00000011001100110011001100110011 = 53687091

Table of Contents

 About This Book

 JDK - Java Development Kit

 Execution Process, Entry Point, Input and Output

 Primitive Data Types and Literals

 Control Flow Statements

Bits, Bytes, Bitwise and Shift Operations

 What Are Bits and Bytes

 "byte" Data Type and Implicit Casting

 Operations on "byte" Data Type Values

 Bitwise Operations on "byte" Values

 Bitwise Operations on "byte" Values - Example Program

Shift Operations - Left, Right or Unsigned Right

 Managing Bit Strings in Byte Arrays

 Reference Data Types and Variables

 Enum Types and Enum Constants

 StringBuffer - The String Buffer Class

 System Properties and Runtime Object Methods

 Generic Classes and Parameterized Types

 Generic Methods and Type Inference

 Lambda Expressions and Method References

 Java Modules - Java Package Aggregation

 Execution Threads and Multi-Threading Java Programs

 ThreadGroup Class and "system" ThreadGroup Tree

 Synchronization Technique and Synchronized Code Blocks

 Deadlock Condition Example Programs

 Garbage Collection and the gc() Method

 Assert Statements and -ea" Option

 Annotation Statements and Declarations

 Java Related Terminologies

 Archived Tutorials

 References

 Full Version in PDF/EPUB