Proof process of the Lowbit (x) Function

· · 算法·理论

The expression of the lowbit(x) function is given by lowbit (x)=x \& -x

It is known that for any integer x be expressed in the form of binary expansion:

and $N$ is the total number of the elements in array $k$).For this form,the identity $lowbit (x)=2^{k_1}$ always holds. Why? This is the proof process below. Let the number $M$ be the **number of digits** of the number $x$ and the array $m$ be $\complement_{[1,M]\cap\Z}{k}$, then let the array $y$ be $\sum_{i=1}^{M}2^{m_i}$. Because the intersection of $x$ and $y$ is empty, y is one's complement of x,and then $y$ is equal to $\sim{x}$. According to properties of **bitwise AND operation**,$x\&\sim x = 0$.But if update y to y+1, the result of $x\&y$ will be different, and this is the key to $lowbit(x)$. **On the one hand, if $x$ is an odd number and update $y$ to $y+1$**,the first digit of $y$ is one, so the result is one, which is equal to $2^0$. **On the other hand,if $x$ is a even number**,the first digit of $y$ is one.Then update $y$ to $y+1$,**there is a continuous carry occurring on $y$ until the next one**(donate it as $k_0$),whose value is one in $x$, of present digit position is zero.After that,this digit position will be one whose value is the same as the number $x$'s.**So it is easy to know that the $x\&y=2^{k_0}$** Based on above context, we can conclude the function $ lowbit(x) = \begin{cases} 1 & x\&1 = 1 \\ 2^{k_0} & else \end{cases} $ , which equal to $lowbit(x) = x \& -x=2^{k_i}$ Thus,the proof is completed.