Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is 64-bit math accomplished on a 32-bit machine?

If a 32-bit processor is, indeed, really only 32 bits in length, then how can math operations work on 64-bit numbers? For example:

long lngTemp1 = 123456789123;
long lngTemp2 = lngTemp1 * 123;

According to MSDN, a long in C# is a signed 64-bit number: http://msdn.microsoft.com/en-us/library/ctetwysk(VS.71).aspx

How is it that a 32-bit Intel Microprocessor can execute code, like the above without getting an overflow?

like image 648
Icemanind Avatar asked Jul 06 '10 20:07

Icemanind


People also ask

Can a 64-bit program run on a 32-bit computer?

Summary. The 64-bit versions of Windows use the Microsoft Windows-32-on-Windows-64 (WOW64) subsystem to run 32-bit programs without modifications. The 64-bit versions of Windows don't provide support for 16-bit binaries or 32-bit drivers.

Is it better to run 32-bit on 64-bit?

Simply put, a 64-bit processor is more capable than a 32-bit processor because it can handle more data at once. A 64-bit processor can store more computational values, including memory addresses, which means it can access over 4 billion times the physical memory of a 32-bit processor.

Is Windows 10 32 or 64-bit?

Windows 10 comes in both 32-bit and 64-bit varieties. While they look and feel nearly identical, the latter takes advantage of faster and better hardware specs. With the era of 32-bit processors winding down, Microsoft is putting the lesser version of its operating system on the back burner.


1 Answers

They use the carry bit for add and subtract. The assembler ops for "add with carry" and "subtract with carry" (or "borrow") can be used for arbitrary bit length extended precision addition and subtraction.

For multiply, if you only have a 32-bit result from multiply, you can break it into 16-bit value pairs and multiply and then shift and add (with carry) to get a full 64-bit result from 32-bit multiply. Basically, doing the long-hand version (any two 16-bit multiplies fit in a 32-bit result) can be used to generate arbitrary bit-length multiplies using a more limited precision.

FWIW, the Intel 32-bit asm "mul" instruction can put a 64-bit result in EDX:EAX so you can actually do multiplies in 32-bit chunks (with 64-bit values to add) rather than 16-bit chunks (with 32-bit values to shift and add).

like image 137
Adisak Avatar answered Oct 20 '22 00:10

Adisak