有不少數論 (Number Theory) 的計算中都會用到找兩個正整數的最大公因數 (greatest common divisor, GCD),而最基本常用找最大公因數的方法中就是利用輾轉相除法 (Euclidean algorithm),如下
$gcd(x, y) = gcd(y, x - q \times y)$
其中 q 為任意整數,下面的這段程式實作出輾轉相除法的概念:int gcd(int x, int y)
{
while ((x %= y) && (y %= x)) ;
return (x + y);
}
然而,在現在處理器中求餘數的運算時間相較於基本的加減法以及位元運算 (bitwise operation) 是慢很多的,因此 Knuth 的 TAOCP (The Art of Computer Programming) 中的第 4.5.2 節就有提到其實曾經有人提出了另一種方法,僅僅使用位元運算以及加減法來找最大公因數。