Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8932595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:20:57+00:00 2026-06-15T09:20:57+00:00

According to Wikipedia ( http://en.wikipedia.org/wiki/Binary_GCD_algorithm ) I was trying to write binary GCD for

  • 0

According to Wikipedia (http://en.wikipedia.org/wiki/Binary_GCD_algorithm) I was trying to write binary GCD for bignums (up to 5000 digits).

My GCD itself looks like this:

bitset<N> gcd(bitset<N> u, bitset<N> v) {
    bitset<N> one (string("1"));
    bitset<N> zero (string("0"));

    int shift;

    if (u == 0) return v;
    if (v == 0) return u;

    for (shift = 0; ((u | v) & one) == zero; ++shift) {
        u >>= 1;
        v >>= 1;
    }

    while ((u & one) == zero) u >>= 1;

    do {
        while ((v & one) == zero) v >>= 1;

        if (u.to_string() > v.to_string()) {
            bitset<N> t = v;
            v = u;
            u = t;
        }

        bitsetSubtract(v,u);
    } while (v != 0);

    return u << shift;
}

I’m also using own bitset subtraction function:

void bitsetSubtract(bitset<N> &x, const bitset<N> &y) {
    bool borrow = false;

    for (int i = 0; i < N; i++) {
        if (borrow) {
            if (x[i]) {
                x[i] = y[i];
                borrow = y[i];
            } else {
                x[i] = !y[i];
                borrow = true;
            }
        } else {
            if (x[i]) {
                x[i] = !y[i];
                borrow = false;
            } else {
                x[i] = y[i];
                borrow = y[i];
            }
        }
    }
}

I don’t see any place to improve speed of this algorithm (binary GCD is fast itself), but I’m getting feedback that my program is too slow.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T09:20:59+00:00Added an answer on June 15, 2026 at 9:20 am

    You’ve represented a bignum as an array of base-2 (binary) digits.

    Real bignum libraries don’t use a base of 2. They use a much larger base, because CPUs have instructions that operate on more than one bit at a time. Typically you would use a base of 256 (28), 65536 (216), 4294967296 (232), or 18446744073709551616 (264) if your goal is maximum speed and minimum size, or a base of 100 (with one byte per digit), 10000 (with two bytes per digit), 1000000000 (with four bytes per digit), or 10000000000000000000 (with eight bytes per digit) if you must store precise decimal fractions.

    You need to use something like vector<uint32_t> or vector<uint64_t> as your bignum, and operate on 32 or 64 bits at a time instead of just 1 bit at a time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to use opengl to make isometric drawings. According to: http://en.wikipedia.org/wiki/Isometric_projection#Mathematics this
According to http://en.wikipedia.org/wiki/Producer-consumer_problem I want to simulate P/C problem using semaphore. I am getting
According to wikipedia! http://en.wikipedia.org/wiki/ORDBMS IBM's DB2, Oracle database, and Microsoft SQL Server, make claims
I'm working on coloring a map according to the four-color theorem ( http://en.wikipedia.org/wiki/Four_color_theorem )
I am new to python and graphics but have programmed before. According to http://en.wikipedia.org/wiki/Transformation_matrix#Rotation
According to http://en.wikipedia.org/wiki/Secure_Shell#Key_management , ssh is vulnerable to man-in-the-middle attack when establishing the first
According to the Wikipedia site: http://en.wikipedia.org/wiki/Volatile_variable I copied its sample code for testing in
According to http://en.wikipedia.org/wiki/Comparison_of_Subversion_clients the web interface iF.SVNAdmin can be installed on a linux OS.
According to wiki http://en.wikipedia.org/wiki/Extensible_Messaging_and_Presence_Protocol , xmpp is using http binding (rather an http pooling)
According to http://en.wikipedia.org/wiki/Heap_%28data_structure%29#Comparison_of_theoretic_bounds_for_variants , it takes Θ(logn) (which translates to O(logn)) to perform the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.