Consider the binary GCD algorithm. If both operands are of the form 2i-1, it can be substantially simplified.
To start with, there are obviously no zeroes at the end in the first step, so you go straight to the loop.
In the loop, in the subtraction, you have two numbers that are of the form 2i-1, and the left hand side is bigger than the right hand side, so the subtraction just resets as many low bits in y
as there are bits set in x
, that is, the subtraction is equivalent to y &= ~x
. The subtraction is immediately followed by shifting y
right by the number of trailing zeroes in it, so you have a number of the form 2i-1 again, but popcnt(x)
shorter.
From this it should be obvious that only the lengths (ie exponents) ever matter, and the identity
gcd(2a-1, 2b-1) = 2gcd(a, b)-1 follows from it.