# Programming Tutorials

## right shift operator, >>, in Java

By: Emiley J Printer Friendly Format

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

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

 Most Viewed Articles (in Java ) Latest Articles (in Java)

Comment on this tutorial
{"ft_lang_label":"__label__en","ft_lang_prob":0.79037267,"math_prob":0.9691388,"size":3426,"snap":"2021-04-2021-17","text_gpt3_token_len":962,"char_repetition_ratio":0.10929281,"word_repetition_ratio":0.015576324,"special_character_ratio":0.29684764,"punctuation_ratio":0.115497075,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9564728,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-01-18T17:00:00Z\",\"WARC-Record-ID\":\"<urn:uuid:ad1a64f5-f259-4fbd-bddc-b3440379c43b>\",\"Content-Length\":\"34614\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:fb2fa471-a2f3-4392-aaaf-cdc0ba58642e>\",\"WARC-Concurrent-To\":\"<urn:uuid:03e72def-2d68-4179-b8b3-05c71c80e5c2>\",\"WARC-IP-Address\":\"172.67.211.62\",\"WARC-Target-URI\":\"https://java-samples.com/showtutorial.php?tutorialid=269\",\"WARC-Payload-Digest\":\"sha1:R65RNUOA436Z7BHWBNPUJVYGG4DJQ77E\",\"WARC-Block-Digest\":\"sha1:35JFW4YZ66Q6IQXRXEPDGKKOOJHPNSBS\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-04/CC-MAIN-2021-04_segments_1610703515075.32_warc_CC-MAIN-20210118154332-20210118184332-00349.warc.gz\"}"}