3

I've found one line in Java like this :

result |= (b & 0x1f) << shift;

I've searched about what operators do but I'm still not able to understand what it is supposed to do assuming result, b and shift are integer values.
Could anyone tell me what does this line is supposed to do ?

Update - Here is the sample part of the code found here

int b, shift = 0, result = 0;
do {
    b = encoded.charAt(index++) - 63;
    result |= (b & 0x1f) << shift;
    shift += 5;
} while (b >= 0x20);
Community
  • 1
  • 1
Sw4Tish
  • 202
  • 4
  • 12
  • You need to provide more context to learn what this is doing. Is the code from here: http://jeffreysambells.com/2010/05/27/decoding-polylines-from-google-maps-direction-api-with-java – Adam Burry Aug 26 '13 at 15:14
  • possible duplicate of [What does "|=" mean in Java?](http://stackoverflow.com/questions/3420925/what-does-mean-in-java) – Zavior Aug 26 '13 at 15:15
  • If you know what `&`, `<<` and `|=` does, you should be able to understand what that line does. – Bernhard Barker Aug 26 '13 at 15:22
  • @Adam Burry updated. @Dukeling Yes, of course, I can't get that part in particular : `(b & 0x1f) << shift` even if I have basic knowledge in hexadecimal values and shift operator. – Sw4Tish Aug 26 '13 at 15:23

2 Answers2

5

Maybe this explanation might help you:

A. (b & 0x1f): performs a logical AND operation between b and 0xf1. This means: return the last 5 bits of b

B. A << shift: shifts to the left an amount of shift bits the result of A operation. This means: shift the last 5 bits of b an amount of shift bits to the left.

C. result |= B: assigns to result variable the result of perform a logical OR operation between result itself and the result of B operation. This means: perform a logical OR between result and the last 5 bits of b shifted to the left an amount of shift bits, and then assign the result to result variable.

Hope it be understandable.

dic19
  • 17,821
  • 6
  • 40
  • 69
2

It preserves the last 5 bits of b, left shifts them some amount and ors them into result.

In this case it is reversing the process described here: https://developers.google.com/maps/documentation/utilities/polylinealgorithm

andriy
  • 4,074
  • 9
  • 44
  • 71
Adam Burry
  • 1,904
  • 13
  • 20