C# Reference ยท Types and Literals
C# Int128 and UInt128 types
Reference for C# Int128 and UInt128: ranges, creating values, parsing, checked arithmetic, BigInteger comparison and why there is no literal syntax.
System.Int128 and System.UInt128 are fixed-size 128-bit integer structs, added in .NET 7. They hold values far beyond long and ulong while keeping value-type speed and no allocation. Unlike the other integer types they have no C# keyword and no literal suffix; you write the type name and build values by conversion, parsing or arithmetic.
Typical uses are hashes and identifiers (a GUID is also 128 bits), large factorials and Fibonacci numbers, high-precision money in smallest units, and intermediate results where a 64-bit multiplication would overflow. When you need unbounded size, use System.Numerics.BigInteger instead.
Int128 big = Int128.MaxValue; UInt128 u = UInt128.Parse("340282366920938463463374607431768211455");
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
public readonly struct Int128 : IBinaryInteger<Int128>, ISignedNumber<Int128> public readonly struct UInt128 : IBinaryInteger<UInt128>, IUnsignedNumber<UInt128>
| Member | Description |
|---|---|
MinValue, MaxValue |
Range limits. Int128 is -2^127 to 2^127 - 1; UInt128 is 0 to 2^128 - 1 (about 3.4 x 10^38). |
Parse(string), TryParse(string, out T) |
Convert text. Parse throws FormatException or OverflowException. |
Abs, Min, Max, Clamp, Log2, PopCount, LeadingZeroCount |
Static helpers from the generic-math interfaces. |
new Int128(ulong upper, ulong lower) |
Builds a value from its two 64-bit halves. |
Conversions. Every other integer type converts to Int128 implicitly, as does UInt128 from the unsigned types. Going down (to long, for instance), to or from double, and between Int128 and UInt128 is explicit.
Errors / undefined behavior. Arithmetic is unchecked by default and wraps around; use checked for OverflowException. Division by zero throws DivideByZeroException.
Usage notes
- No literals.
Int128 x = 170141183460469231731687303715884105727;does not compile because C# has no 128-bit literal. UseInt128.Parse, shifts ((Int128)1 << 100) or arithmetic. - Value types, not hardware types. The CPU has no 128-bit integer instructions; the structs use two 64-bit halves, so they are slower than
long(multiplication and division especially) but much faster thanBigInteger. - Size. Each value takes 16 bytes (
Unsafe.SizeOf<Int128>()prints 16). - Atomicity. Reads and writes are not atomic; protect shared values with a
lockorInterlockedon a wrapper object. - Formatting.
ToString()and format strings such as"N0"or"X"work; the invariant culture is used on our runner. - Choosing. Use
longwhen it fits,Int128for big but bounded results, andBigIntegerwhen the size is not known in advance.