The Right Shift
By: aathishankaran
The right shift
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;
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;
Looking at the same operation in binary shows more clearly how this happens:
0010011 35
>> 2
00001000
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, 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 hexadecimal characters.
//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’, ‘c’
};
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
Archived Comments
1. In the above, Masking sign extension program, Please correct the hex[15] value to 'f' instead of &a
View Tutorial By: Praful at 2012-09-06 06:30:16
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Program using concept of byte long short and int in java
Update contents of a file within a jar file
Tomcat and httpd configured in port 8080 and 80
Count number of vowels, consonants and digits in a String in Java
Student marks calculation program 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