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

  • SEARCH
  • Home
  • 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 6908581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:34:51+00:00 2026-05-27T08:34:51+00:00

I’m writing a program (in C) in which I try to calculate powers of

  • 0

I’m writing a program (in C) in which I try to calculate powers of big numbers in an as short of a period as possible. The numbers I represent as vectors of digits, so all operations have to be written by hand.

The program would be much faster without all the allocations and deallocations of intermediary results. Is there any algorithm for doing integer multiplication, in-place? For example, the function

void BigInt_Times(BigInt *a, const BigInt *b);

would place the result of the multiplication of a and b inside of a, without using an intermediary value.

  • 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-05-27T08:34:52+00:00Added an answer on May 27, 2026 at 8:34 am

    Here, muln() is 2n (really, n) by n = 2n in-place multiplication for unsigned integers. You can adjust it to operate with 32-bit or 64-bit “digits” instead of 8-bit. The modulo operator is left in for clarity.

    muln2() is n by n = n in-place multiplication (as hinted here), also operating on 8-bit “digits”.

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <limits.h>
    
    typedef unsigned char uint8;
    typedef unsigned short uint16;
    #if UINT_MAX >= 0xFFFFFFFF
    typedef unsigned uint32;
    #else
    typedef unsigned long uint32;
    #endif
    typedef unsigned uint;
    
    void muln(uint8* dst/* n bytes + n extra bytes for product */,
              const uint8* src/* n bytes */,
              uint n)
    {
      uint c1, c2;
    
      memset(dst + n, 0, n);
    
      for (c1 = 0; c1 < n; c1++)
      {
        uint8 carry = 0;
    
        for (c2 = 0; c2 < n; c2++)
        {
          uint16 p = dst[c1] * src[c2] + carry + dst[(c1 + n + c2) % (2 * n)];
          dst[(c1 + n + c2) % (2 * n)] = (uint8)(p & 0xFF);
          carry = (uint8)(p >> 8);
        }
    
        dst[c1] = carry;
      }
    
      for (c1 = 0; c1 < n; c1++)
      {
        uint8 t = dst[c1];
        dst[c1] = dst[n + c1];
        dst[n + c1] = t;
      }
    }
    
    void muln2(uint8* dst/* n bytes */,
               const uint8* src/* n bytes */,
               uint n)
    {
      uint c1, c2;
    
      if (n >= 0xFFFF) abort();
    
      for (c1 = n - 1; c1 != ~0u; c1--)
      {
        uint16 s = 0;
        uint32 p = 0; // p must be able to store ceil(log2(n))+2*8 bits
    
        for (c2 = c1; c2 != ~0u; c2--)
        {
          p += dst[c2] * src[c1 - c2];
        }
    
        dst[c1] = (uint8)(p & 0xFF);
    
        for (c2 = c1 + 1; c2 < n; c2++)
        {
          p >>= 8;
          s += dst[c2] + (uint8)(p & 0xFF);
          dst[c2] = (uint8)(s & 0xFF);
          s >>= 8;
        }
      }
    }
    
    int main(void)
    {
      uint8 a[4] = { 0xFF, 0xFF, 0x00, 0x00 };
      uint8 b[2] = { 0xFF, 0xFF };
    
      printf("0x%02X%02X * 0x%02X%02X = ", a[1], a[0], b[1], b[0]);
      muln(a, b, 2);
      printf("0x%02X%02X%02X%02X\n", a[3], a[2], a[1], a[0]);
    
      a[0] = -2; a[1] = -1;
      b[0] = -3; b[1] = -1;
      printf("0x%02X%02X * 0x%02X%02X = ", a[1], a[0], b[1], b[0]);
      muln2(a, b, 2);
      printf("0x%02X%02X\n", a[1], a[0]);
    
      return 0;
    }
    

    Output:

    0xFFFF * 0xFFFF = 0xFFFE0001
    0xFFFE * 0xFFFD = 0x0006
    

    I think this is the best we can do in-place. One thing I don’t like about muln2() is that it has to accumulate bigger intermediate products and then propagate a bigger carry.

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

Sidebar

Related Questions

I have a text area in my form which accepts all possible characters from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am writing an app with both english and french support. The app requests
I would like to run a str_replace or preg_replace which looks for certain words
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

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.