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

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(); }

C++ Casino Game Project

Description: This C++ program on CASINO GAME is a simple text base game.We have used procedure oriented method to design this game. This program is without grahics to keep program easy for beginners . Player can deposit his money to play. From this amount he can bet on number between 1 to 12. If he win he gets 10 times of money otherwise lost his money. In this project, We have used programming concept like do..while loop, user defined function, library function like randomize(), random() etc. #include<iostream.h> #include<conio.h> #include<stdlib.h> #include<stdio.h> #include<time.h> void draw_line( int n, char ch); void rules(); void main() { int balanceamt,amt,no,dice; char playername[80],ch; clrscr(); draw_line(60,'='); cout<<" \n\n\n\n\t\tCASINO GAME\n\n\n\n "; draw_line(60,'='); cout<<" \n\n\nEnter Your Name : "; gets(playername); cout<<" \n\n\Enter Deposit amount to play game : ...

How to Create Project report(Vehicle Management System)

INTRODUCTION Vehicle Management System is software which is helpful for bus operators, who wants to operate many bus trips in a day. Vehicle Management System is a windows application written for 32-bit Windows operating systems which focused in the area of adding, editing and deleting the passengers, staff and the bus routes. In this software a person can be register as a user and he can manage the bus routes and the staff, passengers’ details.  He can add a bus and its details including bus route details. User can also add the details of the staff and their duty time in the system. There are mainly 5 modules in this software Bus Management Route Management Employee Management Passenger Management             In Bus module a user can add a new bus details to the database. He can set the details of the route and the timing also. Bus details like the type, engine number, seating capacity and ...