[英]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 可行的解决方案(以防万一,有人在这样做或类似的事情:P)这不是最优化的解决方案,但它确实适用于32位,我将发布一个更优化的解决方案,该解决方案针对每个值使用bigNum代替未签名int,因为谁知道您的值可能是(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. 我试图创建一个类,该类将处理C ++中任意长度的无符号整数值,在有人说之前,我充分意识到已经存在处理此功能的库。 This is a purely learning experience for me.
对我来说,这是一次纯粹的学习经历。
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 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: 当我尝试打印数字时,就会出现问题,我已经实现了加减法等方法。当决定为数字的每个数字打印什么时,我正在这样做:
Print value and divide the remaining topValue by 10 to get at the next value 打印值并将剩余的topValue除以10得到下一个值
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. 在此无符号值上执行
topCopy % 10
它给出的答案就像是一个有符号值,并给我一个不正确的否定答案,我需要的是可以提取无符号值的最后一位的东西。
2,820,130,816 % 10 should be (for my usage) 6 but output is 0 . 2,820,130,816 % 10应该是(就我而言) 6但输出是0 。
I need a operation that will give me 6 from 2,820,130,816 % 10 instead of 0. 我需要一个操作,该操作将使我获得2,820,130,816 % 10中的 6而不是0。
Thanks! 谢谢!
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. 由于您的第一个操作数是一个
int
,并且由于2,820,130,816
大于可以存储在32位int
的最大值,因此您将得到错误的结果。
Changing the type of topCopy
to unsigned
will fix this problem, and give you back a 6. 将
topCopy
的类型更改为unsigned
将解决此问题,并给您6。
Since you are working with some large numbers you might want to use a larger integer type, such as unsigned long long. 由于您正在使用一些大数,因此您可能需要使用更大的整数类型,例如unsigned long long。 You are overflowing the range of a 32-bit int.
您溢出了32位int的范围。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.