Binary GCD (Stein): Examples & Code

Algorithm Visualizations November 16, 2026 javascript

Published by Dimitris Kougioumtzis · Updated

Remove common powers of two, then repeatedly subtract odd values.

Remove shared factors of two and reduce odd representatives using shifts and subtraction.

Open the 3D visualization

Binary GCD (Stein) example state preview
Preview of the example's final state. Complete values are available in the worked example and interactive timeline.

When to use Binary GCD (Stein)

Comparing arithmetic models for GCD.

Choosing an alternative

Euclid uses remainders; extended Euclid also finds coefficients.

Worked example: input and state changes

Example input

48,18

These checkpoints follow the actual example timeline. State details show the stored values and decisions; use the visualization to inspect every intervening step.

  1. Step 0: Initial state.

    State at this checkpoint
    {
      "values": [
        48,
        18
      ],
      "active": []
    }
  2. Step 1: Remove common factor 2; accumulated factor 2.

    State at this checkpoint
    {
      "values": [
        24,
        9
      ],
      "active": [
        0,
        1
      ]
    }
  3. Step 2: Subtract odd values after removing factors of two.

    State at this checkpoint
    {
      "values": [
        3,
        6
      ],
      "active": [
        0,
        1
      ]
    }
  4. Step 3: Subtract odd values after removing factors of two.

    State at this checkpoint
    {
      "values": [
        3,
        0
      ],
      "active": [
        0,
        1
      ]
    }
  5. Step 4: Greatest common divisor = 6.

    State at this checkpoint
    {
      "values": [
        6,
        0
      ],
      "active": [
        0
      ]
    }

Expected final state

Greatest common divisor = 6.

Complexity and input bounds

Family: Number theory. Time complexity: O(log max(a,b)) iterations.

Space complexity: O(1) working integers.

Working-space bounds describe algorithm state and any explicitly noted output, excluding the visualization's saved timeline. Recording frames can use more memory than running the function alone. Here n is sequence length, V and E are vertex and edge counts, and other parameters refer to the named input dimensions.

Two nonnegative integers up to 9999, not both zero.

The playground validates these limits before starting. Downloaded functions are callable implementations, with parameter and output-buffer contracts in their comments; they do not all repeat the playground's input validation.

Common mistakes and limitations

Handle zero before removing trailing zero bits; iteration counts are not full bit-complexity bounds.

Further reading

For background on algorithm analysis and the JavaScript function interface:

Editorial and testing approach · Browse all algorithm guides

Complete JavaScript implementation

javascript
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
// Nonnegative integers ≤ 9999, not both zero. Returns greatest common divisor using binary operations. Reference snippets assume validated playground inputs. Fixed scratch buffers have 256 slots. Sorting variants may use a different traversal strategy than the animation.
function algorithm(a, b) {
    if (a == 0) { return b; } if (b == 0) { return a; }
    let factor = 1;
    while ((a & 1) == 0 && (b & 1) == 0) { a >>= 1; b >>= 1; factor <<= 1; }
    while ((a & 1) == 0) { a >>= 1; }
    while (b != 0) {
        while ((b & 1) == 0) { b >>= 1; }
        if (a > b) { let t = a; a = b; b = t; }
        b -= a;
    }
    return a * factor;
}

Source

Primary research source for the algorithm or its underlying method. The playground is a bounded teaching implementation, not the publication's original program.