128 bit math in Xojo
This was back in version 2006r4, almost 20 years ago. Until then Xojo only had 32-bit integers.
A couple years ago I saw a feature request to add 128 bit integers to Xojo. One of the things I did then was to add LargeNumberMBS class to our plugin. That is a dynamic integer, which provides large integer numbers from 64 to 4128 bits depending on how many digits the number needs. That is using a library in C++, which has a couple of optimizations, so it works quite well.
This year I learned, that the Int128 support in C++ got better. LLVM for macOS has an __int128 type built-in. That is also in GCC for Linux on 64-bit platforms. And even Microsoft includes such a type in their runtime libraries. Great, we can finally provide two new classes: Int128MBS and UInt128MBS.
Check out the classes. They provide constructors to create instances based on String or Int64 or MemoryBlocks. We havse operators to convert and do the usual math operations. For bitwise math we have AND, OR, XOR as well as shifting left and right.
Since Xojo doesn't do an efficient increment or decrement, we got this here with Inc and Dec methods to add/subtract one. In a tight loop, this is more efficient to do this in place and avoid making new objects each time.
For a bit more advanced math, we got a Pow method to quickly calculate the power of a number. This uses bit shifting to do it more efficient then just multiplying the number a lot of times. Same for the Sqrt function, that finds the rounded down square root.
We have a few properties on the class. We can count the number of bits needed for the number with BitWidth. The PopCount property counts the 1 bits. CountLeftZeros and CountRightZeros count the number of bits from left or right. With doubleValue, you can query the value as a double data type with limited precision.
Please check out the class. Let us know if you have issues or need improvements on the overloaded operators. It may be a bit slower than Int64 math, so do only if the result is expected to be big enough.
See also our BigNumberMBS and BiggerNumberMBS classes for a huge floating point number and see LargeNumberMBS for an up to 4128 bit integer number.