Java Bitwise Shift Operators

This tutorial will take you step by step through the process of understanding and shift operators that act on individual bits. The best way to learn is to compile and run these programs yourself (copy, paste, compile and run !). Comments such as /* this is a comment */ or // this is another comment are inserted to explain what does the line of code do. The programs are kept simple for the purpose of concentrating on the main idea in question.

The bitwise shift operators are : >> , << , >>>

>> the SHIFT RIGHT operator

<< the SHIFT LEFT operator

>>> the UNSIGNED SHIFT RIGHT operator

Example 1: the >> opearator applied to positive intgers

This example shows the effect of using the >> opearator.

  
class Bits1{ 
   public static void main(String args[]){
      System.out.println(" >> opeartor");


// shift all the bits in 20 (in binary form) to the right by 2 
      System.out.println("20>>2 = "+20>>2);
    }
}

Explanation of 20>>2 = 5

20 in binary is: 00000000000000000000000000010100

shift all bits 2 positions to right 00000000000000000000000000000101

This is 5 (2*2^2+0*2^1+1*2^0) in binary form

Example 2: the >> opearator applied to negative intgers

This example shows the effect of using the >> opearator to negative integers.

  
class Bits2{ 
   public static void main(String args[]){
      System.out.println(" >> opeartor");


// shift all the bits in -1 (in binary form) to the right by 2 
      System.out.println("-1>>2 = "+(-1>>2));
    }
}

Explanations of -1>>2 = -1

-1 in binary is: 11111111111111111111111111111111

shift all bits 2 positions to the right AND insert 1's to left you obtain

11111111111111111111111111111111

This is -1 in binary form

NOTE: the >> operator preserves the leftmost bits. The leftmost bits are filled with the previous content. This is to do with sign extension. In this case there is a 1 at the left and it is preserved. If you do not want to keep the 1 to the left, use the >>> operator which shifts 0's into the leftmost bits

Example 3: the >>> opearator

This example shows the effect of using the >> opearator to negative integers.

  
class Bits3{ 
   public static void main(String args[]){
      System.out.println(" >>> opeartor");


// shift all the bits in -1 (in binary form) to the right by 2 
// without sign extension using >>>

int y = -1>>>2;      
System.out.println("-1>>>2 = "+y);

// -1>>>2 in binary form
System.out.println("-1>>>2 in binary form is "+Integer.toBinaryString(y));

    }
}

when you run the program above you obtain:

-1>>>2 = 1073741823

-1>>>2 in binary form is 111111111111111111111111111111

If you count the number of bits, you find 30 bits ONLY, there should be 32. The two 0's at the left have been ignored. This number is actually 00111111111111111111111111111111 which is a positive large number (1073741823). You have to remember that the >>> fills 0's to the left.

Example 4: the << opearator

This example shows the effect of using the << opearator .

  
class Bits4{ 
   public static void main(String args[]){
      System.out.println(" << opeartor");

// shift all the bits to the left by 2 
int y = 13<<2;      
System.out.println("13<<2 = "+y);
    }
}

13<<2 = 52

13 in binary is : 00000000000000000000000000001101

shift left 2 positions and fill right bits with 0,s

you obtain : 00000000000000000000000000110100

this is 1*2^5+1*2^4+0*2^3+1*2^2+0*2^1+0*2^0 = 52

Do you have a Java Problem?
Ask It in The Java Forum

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Return to : Java Programming Hints and Tips

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site www.erpgreat.com is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.