while((number & 1) == 0) {
number >>>= 1;
}
I don't quite understand the condition in while loop, and what does it represent '>>>='.
while((number & 1) == 0) {
number >>>= 1;
}
I don't quite understand the condition in while loop, and what does it represent '>>>='.
The condition in the while
loop tests whether the lowest bit of number
is zero.
The >>>=
operator is a compound assignment operator (see the tutorial here). The statement is the same as:
number = number >>> 1;
The >>>
operator is a bit-wise right-shift with zero fill (see here). It differs from >>
in that >>
will fill with the sign bit, while >>>
always fills with zero.
Note that the code fragment you posted will never terminate if number
starts out as 0.