1

How to calculate the largest $k$ that satisfies

$$x\equiv0\quad(mod \ 2^k)$$


Let $k_*$ be the largest $k$ that satisfies $x\equiv0\ (mod \ 2^k)$, I'm trying to find the explicit expression of $k_*$ with respect to $x$.

  • Actually, if we enter $x$ in binary form, it is obvious that $k_*$ is the number of $0$s to the right of the number. For example, if $x=100101110000$ in binary, then $k_*=4$.

  • I already know that Euclid's algorithm can find the $k_*$, but what I want to know is, does an explicit expression of $k_*$ exist?

Appreciate your help!

Jerry L
  • 81

2 Answers2

0

There is no explicit expression, but the book "Hacker's Delight (2nd edition)" by Henry Warren devotes section 5-4 to a number of algorithms for computing this.

This is called "the trailing zeros" problem.

Some of the algorithms are quite surprising.

The whole book is highly recommended.

marty cohen
  • 110,450
0

If $\oplus$ represents the binary XOR operation, then $x \oplus (x-1) = 2^{k_* + 1} - 1$, from which $k_*$ can be found with no trouble; we can either be sloppy and say that $$ k_* = \lfloor \log_2(x \oplus (x-1))\rfloor $$ or be more careful and say that $$ k_* = \log_2 \frac{(x \oplus (x-1)) + 1}{2} $$ where in the second expression, we don't have to round down the logarithm.

Misha Lavrov
  • 159,700