What is (x & 1) and (x >>= 1)?
x & 1
is equivalent to x % 2
.
x >> 1
is equivalent to x / 2
So, these things are basically the result and remainder of divide by two.
These are Bitwise Operators (reference).
x & 1
produces a value that is either 1
or 0
, depending on the least significant bit of x
: if the last bit is 1
, the result of x & 1
is 1
; otherwise, it is 0
. This is a bitwise AND operation.
x >>= 1
means "set x
to itself shifted by one bit to the right". The expression evaluates to the new value of x
after the shift.
Note: The value of the most significant bit after the shift is zero for values of unsigned type. For values of signed type the most significant bit is copied from the sign bit of the value prior to shifting as part of sign extension, so the loop will never finish if x
is a signed type, and the initial value is negative.