Click here to Skip to main content
15,884,629 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am curious how that was done before we had int64 data types available, using only int32 data type seems a challenge.

using int64 data type i would take the left most 32 bits, convert to int32, multiply that by 2^32, and lastly add the right most 32 bits to get the final result.

What I have tried:

it seems like a catch-22 ! I need the int64 data structure to implement the int64 data structure!

i am hoping there is another method besides using biginteger to do this?
Posted
Updated 26-Apr-22 5:42am
Comments
Richard Deeming 26-Apr-22 11:30am    
Eric Lippert wrote a series of blog posts back in 2013 implementing basic numeric types from scratch:
Math from scratch, part one | Fabulous adventures in coding[^]

That series might give you some idea of how you could build up an 64-bit integer from smaller components.

 
Share this answer
 
One way would be to read the 64-bit value into two uint32_t variables, and then use multiple-precision division to extract the decimal digits (in reverse order). For example, given hi, lo containing the 64-bit value, you can extract the lowest significant decimal digit by:

C++
hi_quot = hi / 10;                  /* divide high part by 10 */
    hi_rem = hi % 10;
    hi_rem = (hi_rem << 16) % 10;   /* use modulus arithmetic to calculate remainder of high part */
    hi_rem = (hi_rem << 16) % 10;
    lo_quot = lo / 10;              /* divide low part by 10 */
    lo_rem = lo % 10;
    
    rem = (hi_rem + lo_rem) % 10;   /* use modulus arithmetic to calculate the digit */
    
    hi = hi_quot;                   /* use "magic numbers" to combine hi_rem, lo_quot */
    lo = hi_rem * 0x19999999 + (6 * hi_rem) / 10 + lo_quot;


Repeat until all digits are extracted (from least significant to most significant).

Note that I have not tested this code!
 
Share this answer
 
Comments
vbnet-rich 28-Apr-22 10:23am    
Daniel - i am not sure what the magic numbers purpose is here, but i tried the code above (using an int32) and got an ovflw on the magic number calc... i can see in int64 variables the result and method i need; i've started with n1_long=1234567890123456, which won't fit in an int32, so i split the long into 2 int32 chunks, using n1_long >> 32 giving the left chunk value= 287445; n1_long Mod 2^32 giving the right chunk value= 1015724736; (I did those ops using bit shifts on the original int64 binary n1_long, and all future ops can be done using bit add's, bit-multiplies etc... My question is when i'm done and ready to present the result (a 64-bit long integer), how do i display a 2^63 or uint64 value, using just int32 data types. The DECIMAL data type (16bytes long vs 8bytes fo int64) would also work as Richard M noted above, but i'd like to see if this can be done using int32 data types alone. Ultimately, i'd like to extend this method to provide an int128 structure of 2-int64 chunks; but the catch-22 delimna i see is: i need the int64 data type to support this method, and looking ahead, i will need the int128 data type (or biginteger) to support the proposed extension to my_int128.
Daniel Pfeffer 29-Apr-22 5:25am    
all variables, the "magical constant" 0x19999999, and the result must all be unsigned 32-bit values. If the results are signed, then they may overflow the signed 32-bit representation.
If an unsigned 32-bit value is unavailable, you could handle this (with extra complexity) by splitting the 64-bit value into four 16-bit values, each stored in a signed 32-bit variable, and using multiple-precision division to extract the digits in a similar manner.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900