# right shift operator, >>, in Java

By: Emiley J Emailed: 1768 times Printed: 2515 times

 By: rohit kumar - how this program is work By: Kirti - Hi..thx for the hadoop in By: Spijker - I have altered the code a By: ali mohammed - why we use the java in ne By: ali mohammed - why we use the java in ne By: mizhelle - when I exported the data By: raul - no output as well, i'm ge By: Rajesh - thanx very much... By: Suindu De - Suppose we are executing

The right shift operator, >>, shifts all of the bits in a value to the right a specified number of times. Its general form is shown here:

value >> num

Here, num specifies the number of positions to right-shift the value in value. That is, the >> moves all of the bits in the specified value to the right the number of bit positions specified by num. The following code fragment shifts the value 32 to the right by two positions, resulting in a being set to 8:

int a = 32;
a = a >> 2; // a now contains 8

When a value has bits that are "shifted off," those bits are lost. For example, the next code fragment shifts the value 35 to the right two positions, which causes the two low order bits to be lost, resulting again in a being set to 8.

int a = 35;
a = a >> 2; // a still contains 8

Looking at the same operation in binary shows more clearly how this happens:

00100011 35
>> 2
00001000 8

Each time you shift a value to the right, it divides that value by two-and discards any remainder. You can take advantage of this for high-performance integer division by 2. Of course, you must be sure that you are not shifting any bits off the right end. When you are shifting right, the top (leftmost) bits exposed by the right shift are filled in with the previous contents of the top bit. This is called sign extension and serves to preserve the sign of negative numbers when you shift them right. For example, -8 >> 1 is -4, which, in binary, is

11111000 -8
>>1
11111100 -4

It is interesting to note that if you shift -1 right, the result always remains -1, since sign extension keeps bringing in more ones in the high-order bits. Sometimes it is not desirable to sign-extend values when you are shifting them to the right. For example, the following program converts a byte value to its hexadecimal string representation. Notice that the shifted value is masked by ANDing it with 0x0f to discard any sign-extended bits so that the value can be used as an index into the array of

// Masking sign extension.
class HexByte {
static public void main(String args[]) {
char hex[] = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
};
byte b = (byte) 0xf1;
System.out.println("b = 0x" + hex[(b >> 4) & 0x0f] + hex[b &
0x0f]);
}
}

Here is the output of this program:

b = 0xf1

 Sponsored Links If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.

 1 View Comment`How to use Shift operators with practicals in java codings?????please explain clearly.......` View Tutorial          By: RosyKrish at 2013-01-17 02:42:37 2 View Comment`Each right shift divides number by 2...` View Tutorial          By: kunal at 2013-06-18 03:43:54

Your email(required, will not be shown to the public):

Your sites URL (optional):

More Tutorials by Emiley J
 Password must include both numeric and alphabetic characters - Magento What is Hadoop? Returning multiple values from a web service Tomcat and httpd configured in port 8080 and 80 Java Webservices using Netbeans and Tomcat Java WebService connected to Database How to Deploy a Java Web Service Call a webservice in Java Java WebService - Create your first web service in Java package javax.jws does not exist Getting Started with Android HTML5 Location - getCurrentPosition() in HTML5 HTML5 Canvas - Using Canvas in HTML5 HTML5 - Introduction HTML5 Video - Handling video in HTML5

More Tutorials in Java
 Update contents of a file within a jar file Tomcat and httpd configured in port 8080 and 80 Java File Java String Count number of vowels, consonants and digits in a String in Java Reverse a number in Java Student marks calculation program in Java Handling Fractions in Java Calculate gross salary in Java Calculate average sale of the week in Java Vector in Java - Sample Program MultiLevel Inheritance sample in Java Multiple Inheritance sample in Java Java program using Method Overriding Java program to check if user input is an even number

More Latest News
Most Viewed Articles (in Java )
 InetAddress Example program in Java Read from a COM port using Java program Stack example in Java - push(), pop(), empty(), search() How to use ArrayList in Java The Basic Structure of a Simple Java program Using StringTokenizer in Java FileReader and FileWriter example program in Java Transient vs Volatile modifiers in Java Vector example in Java Method Overriding in Java Method Overloading (function overloading) in Java instanceof sample program in Java append() in Java Student marks calculation program in Java Reading from a file and writing to a file using Java program
Most Emailed Articles (in Java)
 Text to Speech conversion program in Java inheritance in Java How to initialize an Array and how to copy the array Using Charts in JasperReports What is Unicode? Operator Precedence in Java Of Object, equals (), == and hashCode () DateFormat sample program in Java A Simple whois program in Java PushbackReader sample program in Java CharArrayReader example program in Java ByteArrayOutputStream - sample program in Java Vector example in Java Arrays example in Java - asList(), binarySearch(), fill(), sort(), equals() HashSet Sample program in Java