Sw4Tish
Sw4Tish

Reputation: 202

Syntax/operators Java - What does this line mean?

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

Answers (2)

Adam Burry
Adam Burry

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

dic19
dic19

Reputation: 17971

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.

Upvotes: 5

Related Questions