The Lowbit () function of the tree array is the position of the lowest 1, so it can be calculated by bitwise operation.
The tree array can successfully find the cause of Lowbit by x& (x^ (x-1)):
First set x=6, that is 110 (2) so we make x-1=101 can find, when we subtract a binary number, from the lowest bit one (that is, lowbit) The backward part is the opposite, because the minus 1 has an effect on each subsequent bit, and because it is a binary, The effect is to make every one of them take the opposite.
110
101
Starting from the lowest bit one (second bit) backwards all the opposite so we're going to be different from the X or the other, then from Lowbit onwards is all 1.
110^101=011
And then we'll use X and the new number of bits and a bit with the next because X Lowbit the previous part is 1 or 0,lowbit is 1, after the part is 0, the new number Lowbit before the part is 0,lowbit is 1, after the part is 1 so with the end after their intersection is Lowbit
110&011=010
and Lowbit's common calculation method is x&-x, its principle and the above method is different this formula uses the computer's complement calculation principle complement calculation Simple is the original code anti-code anti-add as follows:
0110 (2) =6
Its complement is: 0110
becomes 0001 after the inverse code
Plus one for 0010.
Can be found to become anti-code after x and anti-code digits each bit is different, so when the reverse code plus 1 after the magical thing happened, the anti-code will be 11 straight carry until the encounter 0, and this 0 becomes 1, so this number last surface appeared a 100 ... String. Because it is anti-code, after carry since the role of 1 to make the carry part of all the inverse and the same as the original code, so you can find Lowbit the previous part X and its complement is-X opposite, Lowbit X and-X are 1,lowbit after the X and the-X. are 0 so x&-x after the addition The lowbit bit is 1, and the rest of the bits are 0.
Explanation of the principle of the tree array lowbit () function x& (x^ (x-1)) x&-x