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 4616002
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:55:13+00:00 2026-05-22T01:55:13+00:00

As homework, I’m implementing Karatsuba’s algorithm and benchmarking it against a primary-school-style O(n^2) multiplication

  • 0

As homework, I’m implementing Karatsuba’s algorithm and benchmarking it against a primary-school-style O(n^2) multiplication algorithm on large integers.

I guessed my only choice here was to bring the numbers to their byte array representations and then work them from there.

Well, I’m stuck here… when using the * operator, I don’t know how would I detect/correct if the number overflows a byte multiplication or adds a carry. Any ideas?

public static BigInteger simpleMultiply(BigInteger x, BigInteger y){

        //BigInteger result = x.multiply(y);

        byte [] xByteArray = x.toByteArray();
        byte [] yByteArray = y.toByteArray();

        int resultSize = xByteArray.length*yByteArray.length;

        byte [][] rowsAndColumns = new byte[resultSize][resultSize];

        for (int i =0; i<xByteArray.length;i++)
           for (int j=0; j<yByteArray.length;j++){


               rowsAndColumns[i][j] = (byte )(xByteArray[i] * yByteArray[j]); 
               // how would I detect/handle carry or overflow here?               
           }

        return null;
    }
  • 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-22T01:55:14+00:00Added an answer on May 22, 2026 at 1:55 am

    The result of a byte multiplication is 2 bytes. You have to use the low order byte as the result and the high order byte as the carry (overflow).

    I would also advise you to be careful of the sign of your bytes. Since bytes in Java are signed, you’ll have to either use only the low 7 bits of them or convert them to ints and correct the sign before multiplying them.

    You’ll want a loop like:

            for (int i =0; i<xByteArray.length;i++)
               for (int j=0; j<yByteArray.length;j++){
                   // convert bytes to ints
                   int xDigit = xByteArray[i], yDigit = yByteArray[j];
                   // convert signed to unsigned
                   if (xDigit < 0)
                       xDigit += 256;
                   if (yDigit < 0)
                       yDigit += 256;
                   // compute result of multiplication
                   int result = xDigit * yDigit;
                   // capture low order byte
                   rowsAndColumns[i][j] = (byte)(result & 0xFF);
                   // get overflow (high order byte)
                   int overflow = result >> 8;
                   // handle overflow here
                   // ...
               }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For my homework assignment, I need to implement Horners Algorithm for converting between bases.
This is a homework question, exactly as follows: The heuristic path algorithm (Pohl, 1977)
As homework, I should implement integer multiplication on numbers of a 1000 digits using
For homework, I was given the following 8 code fragments to analyze and give
For homework, I need to build a small java application.The main goal is to
(This is no homework and no work issue. It's just my personal interest/occupation and
For a homework assignment I was given a Card class that has enumerated types
My kiddo had a homework assignment to write Blackjack in Java. I helped him
I'm doing some homework and while I have some experience with SML, Haskell has
This is homework, and this questions Extends this one So there is a button

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.