简体   繁体   中英

Java - how does reversing byte array with bitwise shift operator work in this example?

I was learning Java from the book - Java 7 for Absolute Beginners by Jay Bryant. There was a particular example on page 164 where he read and then reversed the contents of the text file, using a particular method as follows:

private static void reverseByteArray(byte[] inBytes) {
    int inLength = inBytes.length;
    for (int i = 0; i < (inLength >>1); i++) {
        byte temp = inBytes[i];
        inBytes[i] = inBytes[inLength - i - 1];
        inBytes[inLength - i - 1] = temp;
    }
}

My question is what does the bitwise shift operator do in the following line for (int i = 0; i < (inLength >>1) ; i++) {

-What is its role in the entire operation to reverse the text contents?

I believe I understand the subsequent code under the for loop, which is to swap the first byte's value with the last byte's value.

If there is no bitwise shift operator present, the output will not be reversed.

Initial: To sleep: perchance to dream: ay, there's the rub; For in that sleep of death what dreams may come When we have shuffled off this mortal coil, Must give us pause: there's the respect That makes calamity of so long life

Output: efil gnol os fo ytimalac sekam tahT tcepser eht s'ereht :esuap su evig tsuM ,lioc latrom siht ffo delffuhs evah ew nehW emoc yam smaerd tahw htaed fo peels taht ni roF ;bur eht s'ereht ,ya :maerd ot ecnahcrep :peels oT

Thank you.

what does the bitwise shift operator do in the following line

For reversing the array, you just need to iterate till it's half the length. That is what the bit shift operator is doing. Bit shift by 1 truncates the last bit and is equivalent to divide by 2.

length >> 1  == length / 2

If there is no bitwise shift operator present, the output will not be reversed

If you iterate till length , then till length / 2 , each element will be swapped with corresponding element from the end. But, proceeding further from that will start swapping the element, with the element towards the left. So, the final array will be same as original.

Consider this simple example: arr = [1, 2, 3, 4, 5]

  • You start at index 0 , and swap arr[0] with arr[4] , array becomes:

     [5, 2, 3, 4, 1] 
  • index = 1 , swap arr[1] with arr[3] :

     [5, 4, 3, 2, 1] 

Till here all is fine, and you just iterate before half the length. Now if you proceed further:

  • index = 2 , swap arr[2] and arr[2] , no change

  • index = 3 , swap arr[3] and arr[1] :

     [5, 2, 3, 4, 1] 

See, array starts changing to previous state.

Shifting an int right by 1 is equivalent to truncating its binary representation by one digit, from the right-hand side. This is obviously equivalent to performing integer division by two. (you can test this with a short program, or by observing that, for example, binary 101 >> 1 == binary 10)

The test step of this for loop, then, compares i to inLength/2 As usual, inlength is not changed by the application of this operator (the operator returns a result without altering its arguments, just as 5+4 does not change the value of 5 or 4)

The role of this test is simply to bound the loop: we iterate through half of the string.

(inLength >> 1) == (int) inLength / 2

>> is a signed right shift operator which effectively halves the values for every move.
<< is left shift operator which effectively doubles the value.
>>> is an unsigned right shift operator which fills MSB always with 0.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM