Reputation: 202
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);
Upvotes: 3
Views: 2689
Reputation: 1902
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
Upvotes: 2
Reputation: 17971
Maybe this explanation might help you:
A.
(b & 0x1f)
: performs a logicalAND
operation betweenb
and0xf1
. This means: return the last 5 bits ofb
B.
A << shift
: shifts to the left an amount ofshift
bits the result ofA
operation. This means: shift the last 5 bits ofb
an amount ofshift
bits to the left.C.
result |= B
: assigns toresult
variable the result of perform a logicalOR
operation betweenresult
itself and the result ofB
operation. This means: perform a logicalOR
betweenresult
and the last 5 bits ofb
shifted to the left an amount ofshift
bits, and then assign the result toresult
variable.
Hope it be understandable.
Upvotes: 5