Search code examples
c++cunsignedmodulobignum

C++ - Modulo of an Unsigned Integer


Working solution (in-case anyone is doing this or something like it :P) This isn't the most optimized solution, but it does work for 32 bits i'll post a more optimized solution that uses bigNum's for each value instead of unsigned int, because who knows you might have a value of (2^128)*(2^32) :P

void print(){
    //Hold temporary backwards string array
    std::string backwards;
    //Copy of data and remainder to hold previous loop's value
    unsigned int topCopy = topValue, remainder = 0;
    //Loop through each digit of highest 32 bit number
    for(int i = 0; topCopy != 0; i++){
        //Find value of all added max values
        unsigned int value = maxValues*maxDigits[i];
        //Find value of topValue's last digit
        value += topCopy % 10;
        //Add in the remainder from the previous loop
        value += remainder;
        //Create remainder so the printing value is correct
        remainder = value / 10;
        //append the value to the string
        backwards += value % 10;
        //setup for the next digit
        topCopy /= 10;
    }
    //append the last digit
    backwards += remainder;
    //print backwards
    for(int i = backwards.length()-1; i >= 0; i--){
        printf("%i", backwards[i]);
    }
    printf("\n");
}

I am attempting to create a class that will handle arbitrary length unsigned integer values in C++, before anyone says it, I am fully aware that there are already pre-existing libraries that handle this functionality. This is a purely learning experience for me.

Implementation Notes:

I am storing the values in my class in this format:

topValue: stores the remainder of the number % 4294967296  
maxValues: stores how many max values are needed to hold the number  

Example:

maxValues = 4  
topValue = 2820130816  
maxValues *= 4294967296
maxValues == 17179869184
maxValues + topValue == 20000000000  

The Issue:

The issue arises when I am attempting to print the number, I've already implemented method's for addition subtraction etc... When deciding what to print for each digit of the number I am doing this:

  1. Take digit of comparison starting with the end (6) 4294967296
  2. Add this to topValue % 10 (topValue's end number)
  3. Print value and divide the remaining topValue by 10 to get at the next value

    const char maxDigits[] = {6, 9, 2, 7, 6, 9, 4, 9, 2, 4};
    void print(){
        int topCopy = topValue;
        for(int i = 0; topCopy != 0; i++){
            int value = maxValues*maxDigits[i];
            value += topCopy % 10; // RIGHT HERE IS THE ISSUE
            value %= 10;
            //print value
            topCopy /= 10;
        }
    
    }
    

When doing the line topCopy % 10 on this unsigned value it gives the answer like it's a signed value and gives me a negative answer that is incorrect, what I need is something that can extract the last digit of the unsigned value.

2,820,130,816 % 10 should be (for my usage) 6 but output is 0.

TL;DR:

I need a operation that will give me 6 from 2,820,130,816 % 10 instead of 0.

Thanks!


Solution

  • The result of the modulo operation depend on the types of the operand. Since your first operand is an int, and because 2,820,130,816 is greater than the max value that can be stored in a 32-bit int, you get a wrong result.

    Changing the type of topCopy to unsigned will fix this problem, and give you back a 6.

    Demo on ideone.