0

I wrote simple C++/CLI code to calculate a result of big integer numbers operation, but it failed with the warning warning C4307: '*' : integral constant overflow, here is my code:

int main(array<System::String ^> ^args)
{
    String^ number = gcnew String((wchar_t *)(28433 * (2 ^ 7830457) + 1));

    Console::WriteLine(number);
    return 0;
}
Aan
  • 12,247
  • 36
  • 89
  • 150

2 Answers2

1

2 ^ 7830457 require 7830457 bits to store. Around 1MB just for one number !
You should start by learning variables types and the values they can handle. Do you really need an exact integer value for this number ? Do you really need to display it ? This umber is several millions of char long.

You can't use regular */-+^ operators with such numbers, you need an arbitrary large number math library. And I fear it won't even handle such large number. How to handle arbitrarily large integers

Community
  • 1
  • 1
bokan
  • 3,601
  • 2
  • 23
  • 38
1

You have a number of fundamental misunderstandings in your code:

  1. You're not using big integers anywhere
  2. ^ is xor, and not exponentiation.
  3. You're casting that integer to a pointer, which is a meaningless operation

You should use the BigInteger struct from System.Numerics. Don't forget to add the assembly reference to System.Numerics.dll first.

CodesInChaos
  • 106,488
  • 23
  • 218
  • 262