Chaining int64

I was looking at a question recently (Currency system similar to "Clicker Heroes" in C# - Questions & Answers - Unity Discussions) and I noticed one suggestion by @BoredMormon was to chain int64’s. I looked around a bit with no luck, so I decided to ask a question. So, how would I go about chaining int64’s?


P.S. I am relatively new to c# coding, so if you could keep your answers simple, that would be great!

Chaining, though an odd name is really using multiple variables to make up a very large concatenated variable.


int64 = 9,223,372,036,854,775,807 = 9.2 Quintillion

If you had two values of type int64/long you would “chain” them together, each able to hold 9.2 Quintillion each, but you would be able to extend the number by including them as one number output

9,223,372,036,854,775,807| 9,223,372,036,854,775,807
^Var 2                     ^ Var 1

When Var 1 hits 9,223,372,036,854,775,807, then Var 2 begins at 1 and Var 1 starts at 0 and continues to increase. soit would look like:

1                                           | 0,000,000,000,000,000,000
^Var 2                                        ^ Var 1

Concatenated together and with formatting applied to the numbered variable(to pad zeros) you would get a total of: 10,000,000,000,000,000,000 which is outside of a int64/long signed var.

The point being you’re tracking these large numbers through a number of variables and combining them to reach values outside of there defined behavior.


Here is an example, i worked it up in a ConsoleApplication as proof of concept, obviously some refactoring would be necessary for Unity(storing variable values(serialization)) and all that jazz.

Edit #2:(Comments): please remember this is just a console(dos/terminal/command line) application to test going past a signed long/int64. In the case below the code should be extracted as the logic(which isn’t super awesome or anything) for increasing past 9,223,372,036,854,775,807 as defined as a long/int64 signed(note, signed basically gives you +/- the max value, unsigned(ulong type) will allow for double the positive capacity of long. Comments in line below:

using System;

namespace ConsoleApplication1
	class Program
		static void Main(string[] args)
			bool imToBig = false; // since we are only using two long's to describe a huge chained number, a simple boolean variable to indicate we have gone past long.MaxValue is needed.
			long num1 = 9223372036854700000; // I initialized num1 at almost long.MaxValue, since the goal was to test going past the positive max of a long, i want to get there faster then starting at 0
			long num2 = 0; // The second long that will be prepended to num1 var once num1 reaches long.MaxValue

			do // Do, a loop, in this case i want to increase num1 in a loop displaying the output to the console window, unless a escape is hit of course which will exit the loop and the small program.
				while (!Console.KeyAvailable) // while no key has been hit continue doing the inner loop
					if (num1 < 0) // when a signed integer value goes past it's MaxValue it goes negative, so i'm asserting here that once num1 goes to -1 or beyond that we have flipped the max value and we need to start prepending num2, but we must reset the values first... along with the boolean that dictates we have gone into a wormhole.
						imToBig = true;
						num1 = 0;
						num2 += 1;
// the value string is assigned using a ternary operator, it's like an quick if/else(without nesting them), in the case below value is assigned by evaluating imToBig boolean.  If it is true then the first operation to the right of the question mark is invoked, in this case it will concat(combine) the num2 value, then the num1 value, but for any missing values i'm padding (inserting) zero's, there is a small expression to take into consideration the length(number places) num2 has.  If imToBig is false then just num1 is outputted. 
					string value = imToBig ? string.Concat(num2.ToString(), num1.ToString().PadLeft(18 + (imToBig ? num2.ToString().Length : 0), '0')) : num1.ToString();
					Console.WriteLine(value); // output the value of value( :) ) to the console to view.
					num1++; // in crease num1 by 1, this would be any type of incease.  In the case of an idle game each category of WHATEVER would be added up for a total to add to the money or based off refresh timing per category/business/etc increase, this is the simplest case.
			} while (Console.ReadKey(true).Key != ConsoleKey.Escape);

I can’t say this is very optimal either with all the string love going on. @mj321 and the big int class may be a better option due to how the values are stored.