Skip to main content

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


 

Comments

Popular posts from this blog

WAP to calculate the monthly telephone bills as per the following rule: Minimum Rs. 200 for upto 100 calls. Plus Rs. 0.60 per call for next 50 calls. Plus Rs. 0.50 per call for next 50 calls. Plus Rs. 0.40 per call for any call beyond 200 calls.

  #include<iostream.h> #include<conio.h> void main() { int calls; float bill; cout<<" Enter number of calls : "; cin>>calls; if (calls<=100) bill=200; else if (calls>100 && calls<=150) { calls=calls-100; bill=200+(0.60*calls); } else if (calls>150 && calls<=200) { calls=calls-150; bill=200+(0.60*50)+(0.50*calls); } else { calls=calls-200; bill=200+(0.60*50)+(0.50*50)+(0.40*calls); } cout<<" Your bill is Rs. "<<bill; getch(); }   Output: Enter number of calls : 190 Your bill is Rs.250

Write a program to calculate the total expenses. Quantity and price per item are input by the user and discount of 10% is offered if the expense is more than 7000.

  #include<iostream.h> #include<conio.h> void main() { int totalexp, qty, price, discount; cout<<" Enter quantity: "; cin>>qty; cout<<" Enter price: "; cin>>price; totalexp=qty*price; if (totalexp>7000) { discount=(totalexp*0.1); totalexp=totalexp-discount; } cout<<" Total Expense is Rs. "<<totalexp; getch(); }