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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:51:50+00:00 2026-06-14T23:51:50+00:00

I’m developing a VM in JavaScript and need to multiply two signed 32-bit numbers

  • 0

I’m developing a VM in JavaScript and need to multiply two signed 32-bit numbers with a 64-bit signed result stored as two 32-bit signed numbers (upper 32-bit and lower 32-bit).

I managed to do the same for unsigned numbers by splitting both numbers to 16-bit pairs and multiplying those: a*b = (ah * 2^16 + al) * (bh * 2^16 + bl):

function mul_32_unsigned( a, b )
{
  var ah = a >>> 16;
  var bh = b >>> 16;
  var al = a & 0xFFFF;
  var bl = b & 0xFFFF;

  var mid = ah * bl + al * bh;
  var albl = al * bl;

  var imm = mid + ( albl >>> 16 );

  var carry = ( imm > 0xffffffff ) ? 0x10000 : 0;

  var lo = ( ( mid << 16 ) + albl ) >>> 0;
  var hi = ( ah * bh + ( imm >>> 16 ) + carry ) >>> 0;

  return [ lo, hi ];
}

However, I don’t really understand how to do the same thing for signed numbers. The only thing I can think of is negating any negative a or b to make both positive, perform the unsigned multiplication, then negate the result if needed, but this feels like a clueless suboptimal solution. Any ideas on how to do it better? Splitting a and b into two signed 16-bit numbers each would seem logical, but then I feel lost on how to perform the rest without any mistakes.

p.s. If you think my unsigned implementation is suboptimal as well, please feel free to point that out, too.

  • 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-14T23:51:52+00:00Added an answer on June 14, 2026 at 11:51 pm

    The correct way to split a signed 32-bit integer into two 16-bit integers is as a signed 16-bit upper half, and an unsigned 16-bit lower half — and you need to make an adjustment for negative numbers, by subtracting one from the upper half, and adding 2^16 to the lower half (so as to make it positive).

    For example, the number -100000 should become an upper half of -2 and a lower half of 31072. You can see by reconstructing that -2 * 2^16 + 31072 == -131072 + 31072 == -100000.

    After this, you can do your cross-multiply algorithm as normal; the upper half of the result will be a signed 32-bit integer (because it’s the sum of products some of which are signed), and the lower half will be an unsigned 32-bit integer. Interpreting it involves doing the same “trick” in reverse.

    This corresponds, by the way, to a fairly natural interpretation of what you would see if you looked at the individual words of the native integers on a machine doing the multiplication natively.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am reading a book about Javascript and jQuery and using one of the
I used javascript for loading a picture on my website depending on which small
I have a small JavaScript validation script that validates inputs based on Regex. I
In my XML file chapters tag has more chapter tag.i need to display chapters
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an array which has BIG numbers and small numbers in it. I
I've tracked down a weird MySQL problem to the two different ways I was

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.