On lowbit operation

Knowledge about the operation of lowbit

Benpian essay briefly explain an important class of computer operation mode bit computing - \ (lowbit \) operations.

The concept lowbit

We know that any positive integer can be expressed as a binary number. Such as:
\ [(2) _ {10} = (10) 2 \]

\[ (4)_{10}=(100)_2 \]

\[ \cdots \]

Then define a function \ (lowbit = F (X) \) , the value function is \ (X \) is the least significant bit of the binary expression \ (1 \) corresponding to the value.

For example:
\ [(6) _ {10} = (110) 2 \]
so \ (lowbit (6) \) is equal to \ (2 \) , since \ ((110) 2 \) the least significant bit (that is, from left and right second number) is a number corresponding to \ (^ 1 = 2 2 \)

It is assumed that the least significant bit of a binary number \ (1 \) at the right of the left of the number \ (K \) bits, then its \ (\ lowbit) value is
\ [2 ^ {k-1 } \]

Achieve lowbit function

lowbit function implementation in two ways:

One,

x&(x^(x-1))

two,

x&-x

Simple explanation:

We get the value lowbit, just to get the last position 1, and in addition to this position, all positions all set to zero. You can then output.

Then we take a lookx&(x^(x-1))

Take the above example 6:
\ [(110) _2-1 = (101) 2 \]
, we found that the principle of elementary school mathematics subtraction borrow (funny), to be a binary number minus 1, it will emerge from this the last number 1 to the last number are all inverted, i.e., constitute a (01111 \ cdots \) \ string.

We call this number and the number of different or original, will result in: 1 after the first number (including the first one) 1. Take all the other bits are all 0. constitutes a taking a bunch back with a pile 0 1 string.

So then the original style and make a calculation, then in addition to the 1 (the corresponding bits are 1) the original 1, other bits are all 0, to complete the task.

Then we can look atx&-x

Depending on the nature of the computer's complement.

Complement is the original code, anti-code plus one

Such as:
\ [(110) 2 = 6 \]
Anti-Code:
\ [(001) 2 \]
plus one:
\ [(010) 2 \]
can be found becomes reverse code x and the inverted every digit They are different, so when inverted plus 1 magical things happen, every 1 has been inverted will carry until it hits 0, 0, and this becomes 1, so that the final surface structure of a number of 100 ... string. Because it is inverted, then the action of the carry bit of 1 part of all the intake and negated with the same original code, can be found in the previous section x lowbit i.e. its complement opposite -x, -x and x is 1 lowbit, lowbit after both x and -x 0 x & -x so after addition lowbit bit is 1, the remaining bits are 0. Meet the criteria.

1 by the number of statistical calculation lowbit

We can count the number of operations using lowbit form a binary integer 1.

The principle is very simple matter, is this: we will start with lowbit operation to find \ (lowbit (the X-) \) , then subtract this number with the original number, followed by cycle until is reaches zero.

This is also the realization of the principle of Fenwick tree.

Code:

while(x)
{
    x-=x&-x;
    ans++;
}

(Giant very short)

Application lowbit operation

About lowbit operation, the application should be regarded as the most famous tree-like array. But lowbit marvelous array of far more than the tree, and many related topics binary bit computing, there are lowbit shadow operations. Even in the state of compression in DP, lowbit also plays a role can not be ignored.

Guess you like

Origin www.cnblogs.com/fusiwei/p/11752540.html