12,945,401 members (81,124 online)

#### Stats

49.4K views
20 bookmarked
Posted 22 Feb 2005

# Scaling 64 bit integers

, 22 Feb 2005
An article on scaling 64 bit integers using extended precision integer arithmetic.
 MulDiv64.lib TestMulDiv64.exe MulDiv64 Release MulDiv64.lib TestMulDiv64 Release TestMulDiv64.exe // // TestMulDiv64.cpp : Defines the entry point for the console application. // // This is a small test program that shows the use of MulDiv64. // You can run it to see the three calculated results. // The first result should be 0x0000000000000001, which is clearly wrong. // The second result is 0x38E38E38E38E38E2, which is the correct result. // The third result is the same as the second showing that for certain // denominators the faster MulShr64 can be used. // // Author: Richard van der Wal // e-Mail: R.vdWal@xs4all.nl // #include "stdafx.h" #include "..\MulDiv64.h" // For printing the 64-bit values to the console union{ __int64 qword; DWORD dword[2]; } rep; // The program starts here int _tmain(int argc, _TCHAR* argv[]) { __int64 r = 0; __int64 a = 0xaaaaaaaaaaaaaaaa; __int64 b = 0x5555555555555555; __int64 c = 0x1000000000000000; // = 1 shl 60 char s = 60; // This will return an incorrect result r = a * b / c; rep.qword = r; printf("Plain result: \t\t%08X %08X\n", rep.dword[1], rep.dword[0]); // This will return the correct result r = MulDiv64(a, b, c); rep.qword = r; printf("MulDiv64 result: \t%08X %08X\n", rep.dword[1], rep.dword[0]); // Because dividing by c can be expressed as a right shift // we can obtain the correct scaling faster with: r = MulShr64(a, b, s); rep.qword = r; printf("MulShr64 result: \t%08X %08X\n", rep.dword[1], rep.dword[0]); return 0; }

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

A list of licenses authors might use can be found here