Finding the leading exponent of a binary number

134 Views Asked by At

Let's say that the binary representation of a number $k$ is $2^{X_n} + 2^{X_{n-1}} + \dots + 2^{X_0}$ with each term in this polynomial having a $1$ or $0$ multiplied to it (I just haven't showed them here).

Now, given only $k$, and no other information, is there a way that I can find the number $X_n$(which is the highest degree of the binary representation of $k$)?

1

There are 1 best solutions below

2
On BEST ANSWER

Suppose the highest power of $2$ that appears is $2^n$. Then the smallest $k$ could be is $2^n$, which would happen if all the lower binary digits of $k$ were $0$. On the other hand, the largest $k$ could be is if all the other digits were $1$, which is the value $2^{n+1}-1$ (that's because if you add $1_2$ to $1111\cdots 1_2$, you get $1000\cdots 0_2$). Thus

$$2^n \leq k <2^{n+1}$$

Taking logs with base $2$, this gives

$$n \leq \log_2 k < n+1$$

Since $n$ is integral, this means

$$\boxed{n=\lfloor\log_2 k\rfloor}$$

This says that $k$ has $1 + \lfloor\log_2 k\rfloor$ necessary digits in its binary expansion (assuming $k\neq 0$ is integral).