The Euclid Algorithm for Finding the maximum common divisor requires a large number of Modulo operations, which is a complex task on most computers, in contrast, the subtraction operation, the parity of the test number, and the execution speed of the half operation are faster.
The binary maximum common divisor algorithm avoids the remainder process of the Euclid algorithm.
The maximum divisor is based on the following facts:
- If both A and B are even numbers, gcd (a, B) = 2 * gcd (A/2, B/2)
- If a is an odd number and B is an even number, gcd (a, B) = gcd (A/2, B/2)
- If both A and B are odd, gcd (a, B) = gcd (a-B)/2, B)
Therefore, the algorithm for writing the maximum divisor of binary data is as follows (in C ):
[CPP]View plaincopy
- Int gcd (int A, int B ){
- Int c = 1;
- While (a-B ){
- If (A & 1 ){
- If (B & 1 ){
- If (A> B) A = (a-B)> 1; else B = (B-a)> 1;
- }
- Else B> = 1;
- }
- Else {
- If (B & 1) A >>= 1; else C <<= 1, A >>= 1, B >>= 1;
- }
- }
- Return C *;
- }
Or
[CPP]View plaincopy
- Int gcd (int u, int v ){
- Int K = 1, t;
- While (~ U & 1 &&~ V & 1) k <= 1, u> = 1, V> = 1;
- T = (U & 1 )? -V: U> 1;
- Do {
- While (~ T & 1) T> = 1;
- If (T> 0) u = T; else v =-T;
- } While (t = u-V );
- Return u * K;
- }
Binary maximum common divisor Algorithm