Explanation of the principle of the tree array lowbit () function x& (x^ (x-1)) x&-x

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.