Powers of two and binary representations

70 Views Asked by At

Let $\langle n \rangle$ denote the 2's complement binary representation of an integer. Let $n>1$, show that $\langle n\rangle \ \&\ \langle n-1\rangle = 0 $ iff $n$ is a power of $2$. In the above $\&$ denotes the bitwise $\mathbf{And}$ between two binary integers.

I succeeded to show that if $n$ is a power of two then the condition satisfies. However, I'm straggling to show that the converse is true.

My attempt: Let $\langle n \rangle = a_{m-1},...,a_0$, then \begin{align*} n &= \sum_{i=0}^{m-2} a_i\cdot 2^{i}\\ n-1&= n +(-1) \\ &= \sum_{i=0}^{m-2}a_i\cdot 2^i-2^{m-1} + \sum_{i=0}^{m-2}2^i\\ &=-2^{m-1}+\sum_{i=0}^{m-2}\left(a_i+1\right)\cdot 2^i. \end{align*}

Taking modoulo $2^k$, for $k=1,...,2^{m}$ yields \begin{align*} (a_{k-1}+1)\cdot a_{k-1} \equiv 0\mod {2^k}\implies 2^k|a_{k-1}(a_{k-1}+1)\implies a_{k-1}(a_{k-1}+1)=0\ \forall{1\leq k\leq m}. \end{align*} But we already known that $a_{k-1}\in\{0,1\}$.

1

There are 1 best solutions below

0
On BEST ANSWER

For the converse direction, maybe it's easier to prove the inverse.

If $n$ is not a power of $2$, then let $n = 2^{m-1}+r$, with the remainder $1 \le r < 2^{m-1}$. Then

$$\begin{align*} n &= 2^{m-1} + r\\ n-1 &= 2^{m-1} + (r-1)\\ n \& (n-1) &= 2^{m-1} + [r\&(r-1)]\\ &\ge 2^{m-1}\\ &\ne 0 \end{align*}$$