I have seen in many c++ codes (like the fenwick tree) that x & -x
is used. For instance:
int sum(int idx)
{
int ret = 0;
for (idx++; idx > 0; idx -= idx & -idx) // here!
ret += bit[idx];
return ret;
}
So:
- What is the meaning of this and what will it cause to happen? (And what are the situations I can use this)
- Why this happens and how?
- In which programming languages is this implemented?
- Is there any situations that this will cause a problem or don't work properly?