Int64 Struct

Definition

Represents a 64-bit signed integer.

public value class long : IComparable, IComparable<long>, IConvertible, IEquatable<long>, IFormattable
public value class long : IComparable, IComparable<long>, IConvertible, IEquatable<long>, ISpanFormattable
public value class long : IComparable<long>, IConvertible, IEquatable<long>, IParsable<long>, ISpanParsable<long>, System::Numerics::IAdditionOperators<long, long, long>, System::Numerics::IAdditiveIdentity<long, long>, System::Numerics::IBinaryInteger<long>, System::Numerics::IBinaryNumber<long>, System::Numerics::IBitwiseOperators<long, long, long>, System::Numerics::IComparisonOperators<long, long, bool>, System::Numerics::IDecrementOperators<long>, System::Numerics::IDivisionOperators<long, long, long>, System::Numerics::IEqualityOperators<long, long, bool>, System::Numerics::IIncrementOperators<long>, System::Numerics::IMinMaxValue<long>, System::Numerics::IModulusOperators<long, long, long>, System::Numerics::IMultiplicativeIdentity<long, long>, System::Numerics::IMultiplyOperators<long, long, long>, System::Numerics::INumber<long>, System::Numerics::INumberBase<long>, System::Numerics::IShiftOperators<long, int, long>, System::Numerics::ISignedNumber<long>, System::Numerics::ISubtractionOperators<long, long, long>, System::Numerics::IUnaryNegationOperators<long, long>, System::Numerics::IUnaryPlusOperators<long, long>
public value class long : IComparable<long>, IConvertible, IEquatable<long>, IParsable<long>, ISpanParsable<long>, IUtf8SpanFormattable, System::Numerics::IAdditionOperators<long, long, long>, System::Numerics::IAdditiveIdentity<long, long>, System::Numerics::IBinaryInteger<long>, System::Numerics::IBinaryNumber<long>, System::Numerics::IBitwiseOperators<long, long, long>, System::Numerics::IComparisonOperators<long, long, bool>, System::Numerics::IDecrementOperators<long>, System::Numerics::IDivisionOperators<long, long, long>, System::Numerics::IEqualityOperators<long, long, bool>, System::Numerics::IIncrementOperators<long>, System::Numerics::IMinMaxValue<long>, System::Numerics::IModulusOperators<long, long, long>, System::Numerics::IMultiplicativeIdentity<long, long>, System::Numerics::IMultiplyOperators<long, long, long>, System::Numerics::INumber<long>, System::Numerics::INumberBase<long>, System::Numerics::IShiftOperators<long, int, long>, System::Numerics::ISignedNumber<long>, System::Numerics::ISubtractionOperators<long, long, long>, System::Numerics::IUnaryNegationOperators<long, long>, System::Numerics::IUnaryPlusOperators<long, long>
public value class long : IComparable, IConvertible, IFormattable
public value class long : IComparable, IComparable<long>, IEquatable<long>, IFormattable
public struct Int64 : IComparable, IComparable<long>, IConvertible, IEquatable<long>, IFormattable
public readonly struct Int64 : IComparable, IComparable<long>, IConvertible, IEquatable<long>, IFormattable
public readonly struct Int64 : IComparable, IComparable<long>, IConvertible, IEquatable<long>, ISpanFormattable
public readonly struct Int64 : IComparable<long>, IConvertible, IEquatable<long>, IParsable<long>, ISpanParsable<long>, System.Numerics.IAdditionOperators<long,long,long>, System.Numerics.IAdditiveIdentity<long,long>, System.Numerics.IBinaryInteger<long>, System.Numerics.IBinaryNumber<long>, System.Numerics.IBitwiseOperators<long,long,long>, System.Numerics.IComparisonOperators<long,long,bool>, System.Numerics.IDecrementOperators<long>, System.Numerics.IDivisionOperators<long,long,long>, System.Numerics.IEqualityOperators<long,long,bool>, System.Numerics.IIncrementOperators<long>, System.Numerics.IMinMaxValue<long>, System.Numerics.IModulusOperators<long,long,long>, System.Numerics.IMultiplicativeIdentity<long,long>, System.Numerics.IMultiplyOperators<long,long,long>, System.Numerics.INumber<long>, System.Numerics.INumberBase<long>, System.Numerics.IShiftOperators<long,int,long>, System.Numerics.ISignedNumber<long>, System.Numerics.ISubtractionOperators<long,long,long>, System.Numerics.IUnaryNegationOperators<long,long>, System.Numerics.IUnaryPlusOperators<long,long>
public readonly struct Int64 : IComparable<long>, IConvertible, IEquatable<long>, IParsable<long>, ISpanParsable<long>, IUtf8SpanFormattable, System.Numerics.IAdditionOperators<long,long,long>, System.Numerics.IAdditiveIdentity<long,long>, System.Numerics.IBinaryInteger<long>, System.Numerics.IBinaryNumber<long>, System.Numerics.IBitwiseOperators<long,long,long>, System.Numerics.IComparisonOperators<long,long,bool>, System.Numerics.IDecrementOperators<long>, System.Numerics.IDivisionOperators<long,long,long>, System.Numerics.IEqualityOperators<long,long,bool>, System.Numerics.IIncrementOperators<long>, System.Numerics.IMinMaxValue<long>, System.Numerics.IModulusOperators<long,long,long>, System.Numerics.IMultiplicativeIdentity<long,long>, System.Numerics.IMultiplyOperators<long,long,long>, System.Numerics.INumber<long>, System.Numerics.INumberBase<long>, System.Numerics.IShiftOperators<long,int,long>, System.Numerics.ISignedNumber<long>, System.Numerics.ISubtractionOperators<long,long,long>, System.Numerics.IUnaryNegationOperators<long,long>, System.Numerics.IUnaryPlusOperators<long,long>
[System.Serializable]
public struct Int64 : IComparable, IConvertible, IFormattable
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public struct Int64 : IComparable, IComparable<long>, IConvertible, IEquatable<long>, IFormattable
public struct Int64 : IComparable, IComparable<long>, IEquatable<long>, IFormattable
type int64 = struct
    interface IConvertible
    interface IFormattable
type int64 = struct
    interface IConvertible
    interface ISpanFormattable
    interface IFormattable
type int64 = struct
    interface IConvertible
    interface IFormattable
    interface IParsable<int64>
    interface ISpanFormattable
    interface ISpanParsable<int64>
    interface IAdditionOperators<int64, int64, int64>
    interface IAdditiveIdentity<int64, int64>
    interface IBinaryInteger<int64>
    interface IBinaryNumber<int64>
    interface IBitwiseOperators<int64, int64, int64>
    interface IComparisonOperators<int64, int64, bool>
    interface IEqualityOperators<int64, int64, bool>
    interface IDecrementOperators<int64>
    interface IDivisionOperators<int64, int64, int64>
    interface IIncrementOperators<int64>
    interface IModulusOperators<int64, int64, int64>
    interface IMultiplicativeIdentity<int64, int64>
    interface IMultiplyOperators<int64, int64, int64>
    interface INumber<int64>
    interface INumberBase<int64>
    interface ISubtractionOperators<int64, int64, int64>
    interface IUnaryNegationOperators<int64, int64>
    interface IUnaryPlusOperators<int64, int64>
    interface IShiftOperators<int64, int, int64>
    interface IMinMaxValue<int64>
    interface ISignedNumber<int64>
type int64 = struct
    interface IConvertible
    interface IFormattable
    interface IParsable<int64>
    interface ISpanFormattable
    interface ISpanParsable<int64>
    interface IAdditionOperators<int64, int64, int64>
    interface IAdditiveIdentity<int64, int64>
    interface IBinaryInteger<int64>
    interface IBinaryNumber<int64>
    interface IBitwiseOperators<int64, int64, int64>
    interface IComparisonOperators<int64, int64, bool>
    interface IEqualityOperators<int64, int64, bool>
    interface IDecrementOperators<int64>
    interface IDivisionOperators<int64, int64, int64>
    interface IIncrementOperators<int64>
    interface IModulusOperators<int64, int64, int64>
    interface IMultiplicativeIdentity<int64, int64>
    interface IMultiplyOperators<int64, int64, int64>
    interface INumber<int64>
    interface INumberBase<int64>
    interface ISubtractionOperators<int64, int64, int64>
    interface IUnaryNegationOperators<int64, int64>
    interface IUnaryPlusOperators<int64, int64>
    interface IShiftOperators<int64, int, int64>
    interface IMinMaxValue<int64>
    interface ISignedNumber<int64>
    interface IUtf8SpanFormattable
[<System.Serializable>]
type int64 = struct
    interface IFormattable
    interface IConvertible
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type int64 = struct
    interface IFormattable
    interface IConvertible
type int64 = struct
    interface IFormattable
Public Structure Int64
Implements IComparable, IComparable(Of Long), IConvertible, IEquatable(Of Long), IFormattable
Public Structure Int64
Implements IComparable, IComparable(Of Long), IConvertible, IEquatable(Of Long), ISpanFormattable
Public Structure Int64
Implements IAdditionOperators(Of Long, Long, Long), IAdditiveIdentity(Of Long, Long), IBinaryInteger(Of Long), IBinaryNumber(Of Long), IBitwiseOperators(Of Long, Long, Long), IComparable(Of Long), IComparisonOperators(Of Long, Long, Boolean), IConvertible, IDecrementOperators(Of Long), IDivisionOperators(Of Long, Long, Long), IEqualityOperators(Of Long, Long, Boolean), IEquatable(Of Long), IIncrementOperators(Of Long), IMinMaxValue(Of Long), IModulusOperators(Of Long, Long, Long), IMultiplicativeIdentity(Of Long, Long), IMultiplyOperators(Of Long, Long, Long), INumber(Of Long), INumberBase(Of Long), IParsable(Of Long), IShiftOperators(Of Long, Integer, Long), ISignedNumber(Of Long), ISpanParsable(Of Long), ISubtractionOperators(Of Long, Long, Long), IUnaryNegationOperators(Of Long, Long), IUnaryPlusOperators(Of Long, Long)
Public Structure Int64
Implements IAdditionOperators(Of Long, Long, Long), IAdditiveIdentity(Of Long, Long), IBinaryInteger(Of Long), IBinaryNumber(Of Long), IBitwiseOperators(Of Long, Long, Long), IComparable(Of Long), IComparisonOperators(Of Long, Long, Boolean), IConvertible, IDecrementOperators(Of Long), IDivisionOperators(Of Long, Long, Long), IEqualityOperators(Of Long, Long, Boolean), IEquatable(Of Long), IIncrementOperators(Of Long), IMinMaxValue(Of Long), IModulusOperators(Of Long, Long, Long), IMultiplicativeIdentity(Of Long, Long), IMultiplyOperators(Of Long, Long, Long), INumber(Of Long), INumberBase(Of Long), IParsable(Of Long), IShiftOperators(Of Long, Integer, Long), ISignedNumber(Of Long), ISpanParsable(Of Long), ISubtractionOperators(Of Long, Long, Long), IUnaryNegationOperators(Of Long, Long), IUnaryPlusOperators(Of Long, Long), IUtf8SpanFormattable
Public Structure Int64
Implements IComparable, IConvertible, IFormattable
Public Structure Int64
Implements IComparable, IComparable(Of Long), IEquatable(Of Long), IFormattable
Inheritance
Attributes
Implements
IComparable IComparable<Int64> IConvertible IEquatable<Int64> IFormattable ISpanFormattable IComparable<TSelf> IEquatable<TSelf> IParsable<Int64> IParsable<TSelf> ISpanParsable<Int64> ISpanParsable<TSelf> IAdditionOperators<Int64,Int64,Int64> IAdditionOperators<TSelf,TSelf,TSelf> IAdditiveIdentity<Int64,Int64> IAdditiveIdentity<TSelf,TSelf> IBinaryInteger<Int64> IBinaryNumber<Int64> IBinaryNumber<TSelf> IBitwiseOperators<Int64,Int64,Int64> IBitwiseOperators<TSelf,TSelf,TSelf> IComparisonOperators<Int64,Int64,Boolean> IComparisonOperators<TSelf,TSelf,Boolean> IDecrementOperators<Int64> IDecrementOperators<TSelf> IDivisionOperators<Int64,Int64,Int64> IDivisionOperators<TSelf,TSelf,TSelf> IEqualityOperators<Int64,Int64,Boolean> IEqualityOperators<TSelf,TOther,TResult> IEqualityOperators<TSelf,TSelf,Boolean> IIncrementOperators<Int64> IIncrementOperators<TSelf> IMinMaxValue<Int64> IModulusOperators<Int64,Int64,Int64> IModulusOperators<TSelf,TSelf,TSelf> IMultiplicativeIdentity<Int64,Int64> IMultiplicativeIdentity<TSelf,TSelf> IMultiplyOperators<Int64,Int64,Int64> IMultiplyOperators<TSelf,TSelf,TSelf> INumber<Int64> INumber<TSelf> INumberBase<Int64> INumberBase<TSelf> IShiftOperators<Int64,Int32,Int64> IShiftOperators<TSelf,Int32,TSelf> ISignedNumber<Int64> ISubtractionOperators<Int64,Int64,Int64> ISubtractionOperators<TSelf,TSelf,TSelf> IUnaryNegationOperators<Int64,Int64> IUnaryNegationOperators<TSelf,TSelf> IUnaryPlusOperators<Int64,Int64> IUnaryPlusOperators<TSelf,TSelf> IUtf8SpanFormattable

Remarks

Int64 is an immutable value type that represents signed integers with values that range from negative 9,223,372,036,854,775,808 (which is represented by the Int64.MinValue constant) through positive 9,223,372,036,854,775,807 (which is represented by the Int64.MaxValue constant. The .NET Framework also includes an unsigned 64-bit integer value type, UInt64, which represents values that range from 0 to 18,446,744,073,709,551,615.

Instantiating an Int64 Value

You can instantiate an Int64 value in several ways:

  • You can declare an Int64 variable and assign it a literal integer value that is within the range of the Int64 data type. The following example declares two Int64 variables and assigns them values in this way.

    long number1 = -64301728;
    long number2 = 255486129307;
    
    let number1 = -64301728L
    let number2 = 255486129307L
    
    Dim number1 As Long = -64301728
    Dim number2 As Long = 255486129307
    
  • You can assign the value of an integral type whose range is a subset of the Int64 type. This is a widening conversion that does not require a cast operator in C# or a conversion method in Visual Basic. In F#, only the Int32 type can be widened automatically.

    sbyte value1 = 124;
    short value2 = 1618;
    int value3 = Int32.MaxValue;
    
    long number1 = value1;
    long number2 = value2;
    long number3 = value3;
    
    let value1 = 124y
    let value2 = 1618s
    let value3 = Int32.MaxValue
    
    let number1 = int64 value1
    let number2 = int64 value2
    let number3: int64 = value3
    
    Dim value1 As SByte = 124
    Dim value2 As Int16 = 1618
    Dim value3 As Int32 = Int32.MaxValue
    
    Dim number1 As Long = value1
    Dim number2 As Long = value2
    Dim number3 As Long = value3
    
  • You can assign the value of a numeric type whose range exceeds that of the Int64 type. This is a narrowing conversion, so it requires a cast operator in C# or F# and a conversion method in Visual Basic if Option Strict is on. If the numeric value is a Single, Double, or Decimal value that includes a fractional component, the handling of its fractional part depends on the compiler performing the conversion. The following example performs narrowing conversions to assign several numeric values to Int64 variables.

    ulong ulNumber = 163245617943825;
    try {
       long number1 = (long) ulNumber;
       Console.WriteLine(number1);
    }
    catch (OverflowException) {
       Console.WriteLine("{0} is out of range of an Int64.", ulNumber);
    }
    
    double dbl2 = 35901.997;
    try {
       long number2 = (long) dbl2;
       Console.WriteLine(number2);
    }
    catch (OverflowException) {
       Console.WriteLine("{0} is out of range of an Int64.", dbl2);
    }
    
    BigInteger bigNumber = (BigInteger) 1.63201978555e30;
    try {
       long number3 = (long) bigNumber;
       Console.WriteLine(number3);
    }
    catch (OverflowException) {
       Console.WriteLine("{0} is out of range of an Int64.", bigNumber);
    }
    // The example displays the following output:
    //    163245617943825
    //    35902
    //    1,632,019,785,549,999,969,612,091,883,520 is out of range of an Int64.
    
    let ulNumber = 163245617943825uL
    try
        let number1 = int64 ulNumber
        printfn $"{number1}"
    with :? OverflowException ->
        printfn $"{ulNumber} is out of range of an Int64."
    
    let dbl2 = 35901.997
    try
        let number2 = int64 dbl2
        printfn $"{number2}"
    with :? OverflowException ->
        printfn $"{dbl2} is out of range of an Int64."
    
    let bigNumber = BigInteger 1.63201978555e30
    try
        let number3 = int64 bigNumber
        printfn $"{number3}"
    with :? OverflowException ->
        printfn $"{bigNumber} is out of range of an Int64."
    
    // The example displays the following output:
    //    163245617943825
    //    35902
    //    1,632,019,785,549,999,969,612,091,883,520 is out of range of an Int64.
    
    Dim ulNumber As ULong = 163245617943825
    Try
       Dim number1 As Long = CLng(ulNumber)
       Console.WriteLine(number1)
    Catch e As OverflowException
       Console.WriteLine("{0} is out of range of an Int64.", ulNumber)
    End Try
    
    Dim dbl2 As Double = 35901.997
    Try
       Dim number2 As Long = CLng(dbl2)
       Console.WriteLine(number2)
    Catch e As OverflowException
       Console.WriteLine("{0} is out of range of an Int64.", dbl2)
    End Try
       
    Dim bigNumber As BigInteger = 1.63201978555e30
    Try
       Dim number3 As Long = CLng(bigNumber)
       Console.WriteLine(number3)
    Catch e As OverflowException
       Console.WriteLine("{0:N0} is out of range of an Int64.", bigNumber)
    End Try    
    ' The example displays the following output:
    '    163245617943825
    '    35902
    '    1,632,019,785,549,999,969,612,091,883,520 is out of range of an Int64.
    
  • You can call a method of the Convert class to convert any supported type to an Int64 value. This is possible because Int64 supports the IConvertible interface. The following example illustrates the conversion of an array of Decimal values to Int64 values.

    decimal[] values= { Decimal.MinValue, -1034.23m, -12m, 0m, 147m,
                        199.55m, 9214.16m, Decimal.MaxValue };
    long result;
    
    foreach (decimal value in values)
    {
       try {
          result = Convert.ToInt64(value);
          Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.",
                            value.GetType().Name, value,
                            result.GetType().Name, result);
       }
       catch (OverflowException) {
          Console.WriteLine("{0} is outside the range of the Int64 type.",
                            value);
       }
    }
    // The example displays the following output:
    //    -79228162514264337593543950335 is outside the range of the Int64 type.
    //    Converted the Decimal value '-1034.23' to the Int64 value -1034.
    //    Converted the Decimal value '-12' to the Int64 value -12.
    //    Converted the Decimal value '0' to the Int64 value 0.
    //    Converted the Decimal value '147' to the Int64 value 147.
    //    Converted the Decimal value '199.55' to the Int64 value 200.
    //    Converted the Decimal value '9214.16' to the Int64 value 9214.
    //    79228162514264337593543950335 is outside the range of the Int64 type.
    
    let values= 
        [| Decimal.MinValue; -1034.23M; -12M; 0M; 147M
           199.55M; 9214.16M; Decimal.MaxValue |]
    
    for value in values do
        try
            let result = Convert.ToInt64 value
            printfn $"Converted the {value.GetType().Name} value '{value}' to the {result.GetType().Name} value {result}." 
        with :? OverflowException ->
            printfn $"{value} is outside the range of the Int64 type."
        
    // The example displays the following output:
    //    -79228162514264337593543950335 is outside the range of the Int64 type.
    //    Converted the Decimal value '-1034.23' to the Int64 value -1034.
    //    Converted the Decimal value '-12' to the Int64 value -12.
    //    Converted the Decimal value '0' to the Int64 value 0.
    //    Converted the Decimal value '147' to the Int64 value 147.
    //    Converted the Decimal value '199.55' to the Int64 value 200.
    //    Converted the Decimal value '9214.16' to the Int64 value 9214.
    //    79228162514264337593543950335 is outside the range of the Int64 type.
    
    Dim values() As Decimal = { Decimal.MinValue, -1034.23d, -12d, 0d, 147d, _
                                199.55d, 9214.16d, Decimal.MaxValue }
    Dim result As Long
    
    For Each value As Decimal In values
       Try
          result = Convert.ToInt64(value)
          Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.", _
                            value.GetType().Name, value, _
                            result.GetType().Name, result)
       Catch e As OverflowException
          Console.WriteLine("{0} is outside the range of the Int64 type.", _
                            value)
       End Try
    Next
    ' The example displays the following output:
    '    -79228162514264337593543950335 is outside the range of the Int64 type.
    '    Converted the Decimal value '-1034.23' to the Int64 value -1034.
    '    Converted the Decimal value '-12' to the Int64 value -12.
    '    Converted the Decimal value '0' to the Int64 value 0.
    '    Converted the Decimal value '147' to the Int64 value 147.
    '    Converted the Decimal value '199.55' to the Int64 value 200.
    '    Converted the Decimal value '9214.16' to the Int64 value 9214.
    '    79228162514264337593543950335 is outside the range of the Int64 type.
    
  • You can call the Parse or TryParse method to convert the string representation of an Int64 value to an Int64. The string can contain either decimal or hexadecimal digits. The following example illustrates the parse operation by using both a decimal and a hexadecimal string.

    string string1 = "244681903147";
    try {
       long number1 = Int64.Parse(string1);
       Console.WriteLine(number1);
    }
    catch (OverflowException) {
       Console.WriteLine("'{0}' is out of range of a 64-bit integer.", string1);
    }
    catch (FormatException) {
       Console.WriteLine("The format of '{0}' is invalid.", string1);
    }
    
    string string2 = "F9A3CFF0A";
    try {
       long number2 = Int64.Parse(string2,
                                  System.Globalization.NumberStyles.HexNumber);
       Console.WriteLine(number2);
    }
    catch (OverflowException) {
       Console.WriteLine("'{0}' is out of range of a 64-bit integer.", string2);
    }
    catch (FormatException) {
       Console.WriteLine("The format of '{0}' is invalid.", string2);
    }
    // The example displays the following output:
    //    244681903147
    //    67012198154
    
    let string1 = "244681903147"
    try
        let number1 = Int64.Parse string1
        printfn $"{number1}"
    with
    | :? OverflowException ->
        printfn $"'{string1}' is out of range of a 64-bit integer."
    | :? FormatException ->
        printfn $"The format of '{string1}' is invalid."
    
    let string2 = "F9A3CFF0A"
    try
        let number2 = Int64.Parse(string2, NumberStyles.HexNumber)
        printfn $"{number2}"
    
    with
    | :? OverflowException ->
        printfn $"'{string2}' is out of range of a 64-bit integer."
    | :? FormatException ->
        printfn $"The format of '{string2}' is invalid."
    
    // The example displays the following output:
    //    244681903147
    //    67012198154
    
    Dim string1 As String = "244681903147"
    Try
       Dim number1 As Long = Int64.Parse(string1)
       Console.WriteLine(number1)
    Catch e As OverflowException
       Console.WriteLine("'{0}' is out of range of a 64-bit integer.", string1)
    Catch e As FormatException
       Console.WriteLine("The format of '{0}' is invalid.", string1)
    End Try
    
    Dim string2 As String = "F9A3CFF0A"
    Try
       Dim number2 As Long = Int64.Parse(string2,
                                System.Globalization.NumberStyles.HexNumber)
       Console.WriteLine(number2)
    Catch e As OverflowException
       Console.WriteLine("'{0}' is out of range of a 64-bit integer.", string2)
    Catch e As FormatException
       Console.WriteLine("The format of '{0}' is invalid.", string2)
    End Try
    ' The example displays the following output:
    '    244681903147
    '    67012198154
    

Performing Operations on Int64 Values

The Int64 type supports standard mathematical operations such as addition, subtraction, division, multiplication, negation, and unary negation. Like the other integral types, the Int64 type also supports the bitwise AND, OR, XOR, left shift, and right shift operators.

You can use the standard numeric operators to compare two Int64 values, or you can call the CompareTo or Equals method.

You can also call the members of the Math class to perform a wide range of numeric operations, including getting the absolute value of a number, calculating the quotient and remainder from integral division, determining the maximum or minimum value of two long integers, getting the sign of a number, and rounding a number.

Representing an Int64 as a String

The Int64 type provides full support for standard and custom numeric format strings. (For more information, see Formatting Types, Standard Numeric Format Strings, and Custom Numeric Format Strings.)

To format an Int64 value as an integral string with no leading zeros, you can call the parameterless ToString() method. By using the "D" format specifier, you can also include a specified number of leading zeros in the string representation. By using the "N" format specifier, you can include group separators and specify the number of decimal digits to appear in the string representation of the number. By using the "X" format specifier, you can represent an Int64 value as a hexadecimal string. The following example formats the elements in an array of Int64 values in these four ways.

long[] numbers = { -1403, 0, 169, 1483104 };
foreach (var number in numbers) {
   // Display value using default formatting.
   Console.Write("{0,-8}  -->   ", number.ToString());
   // Display value with 3 digits and leading zeros.
   Console.Write("{0,8:D3}", number);
   // Display value with 1 decimal digit.
   Console.Write("{0,13:N1}", number);
   // Display value as hexadecimal.
   Console.Write("{0,18:X2}", number);
   // Display value with eight hexadecimal digits.
   Console.WriteLine("{0,18:X8}", number);
}
// The example displays the following output:
//    -1403     -->      -1403     -1,403.0  FFFFFFFFFFFFFA85  FFFFFFFFFFFFFA85
//    0         -->        000          0.0                00          00000000
//    169       -->        169        169.0                A9          000000A9
//    1483104   -->    1483104  1,483,104.0            16A160          0016A160
let numbers = [| -1403L; 0L; 169L; 1483104L |]
for number in numbers do
    // Display value using default formatting.
    printf $"{number.ToString(),-8}  -->   "
    // Display value with 3 digits and leading zeros.
    printf $"{number,8:D3}"
    // Display value with 1 decimal digit.
    printf $"{number,13:N1}"
    // Display value as hexadecimal.
    printf $"{number,18:X2}"
    // Display value with eight hexadecimal digits.
    printfn $"{number,18:X8}"

// The example displays the following output:
//    -1403     -->      -1403     -1,403.0  FFFFFFFFFFFFFA85  FFFFFFFFFFFFFA85
//    0         -->        000          0.0                00          00000000
//    169       -->        169        169.0                A9          000000A9
//    1483104   -->    1483104  1,483,104.0            16A160          0016A160
Dim numbers() As Long = { -1403, 0, 169, 1483104 }
For Each number In numbers
   ' Display value using default formatting.
   Console.Write("{0,-8}  -->   ", number.ToString())
   ' Display value with 3 digits and leading zeros.
   Console.Write("{0,8:D3}", number)
   ' Display value with 1 decimal digit.
   Console.Write("{0,13:N1}", number) 
   ' Display value as hexadecimal.
   Console.Write("{0,18:X2}", number)
   ' Display value with eight hexadecimal digits.
   Console.WriteLine("{0,18:X8}", number)
Next   
' The example displays the following output:
'    -1403     -->      -1403     -1,403.0  FFFFFFFFFFFFFA85  FFFFFFFFFFFFFA85
'    0         -->        000          0.0                00          00000000
'    169       -->        169        169.0                A9          000000A9
'    1483104   -->    1483104  1,483,104.0            16A160          0016A160

You can also format an Int64 value as a binary, octal, decimal, or hexadecimal string by calling the ToString(Int64, Int32) method and supplying the base as the method's second parameter. The following example calls this method to display the binary, octal, and hexadecimal representations of an array of integer values.

long[] numbers = { -146, 11043, 2781913 };
foreach (var number in numbers) {
   Console.WriteLine("{0} (Base 10):", number);
   Console.WriteLine("   Binary:  {0}", Convert.ToString(number, 2));
   Console.WriteLine("   Octal:   {0}", Convert.ToString(number, 8));
   Console.WriteLine("   Hex:     {0}\n", Convert.ToString(number, 16));
}
// The example displays the following output:
//    -146 (Base 10):
//       Binary:  1111111111111111111111111111111111111111111111111111111101101110
//       Octal:   1777777777777777777556
//       Hex:     ffffffffffffff6e
//
//    11043 (Base 10):
//       Binary:  10101100100011
//       Octal:   25443
//       Hex:     2b23
//
//    2781913 (Base 10):
//       Binary:  1010100111001011011001
//       Octal:   12471331
//       Hex:     2a72d9
let numbers = [| -146L; 11043L; 2781913L |]
for number in numbers do
    printfn $"{number} (Base 10):"
    printfn $"   Binary:  {Convert.ToString(number, 2)}"
    printfn $"   Octal:   {Convert.ToString(number, 8)}"
    printfn $"   Hex:     {Convert.ToString(number, 16)}\n"

// The example displays the following output:
//    -146 (Base 10):
//       Binary:  1111111111111111111111111111111111111111111111111111111101101110
//       Octal:   1777777777777777777556
//       Hex:     ffffffffffffff6e
//
//    11043 (Base 10):
//       Binary:  10101100100011
//       Octal:   25443
//       Hex:     2b23
//
//    2781913 (Base 10):
//       Binary:  1010100111001011011001
//       Octal:   12471331
//       Hex:     2a72d9
Dim numbers() As Long = { -146, 11043, 2781913 }
For Each number In numbers
   Console.WriteLine("{0} (Base 10):", number)
   Console.WriteLine("   Binary:  {0}", Convert.ToString(number, 2))
   Console.WriteLine("   Octal:   {0}", Convert.ToString(number, 8))
   Console.WriteLine("   Hex:     {0}", Convert.ToString(number, 16))
   Console.WriteLine()
Next      
' The example displays the following output:
'    -146 (Base 10):
'       Binary:  1111111111111111111111111111111111111111111111111111111101101110
'       Octal:   1777777777777777777556
'       Hex:     ffffffffffffff6e
'
'    11043 (Base 10):
'       Binary:  10101100100011
'       Octal:   25443
'       Hex:     2b23
'
'    2781913 (Base 10):
'       Binary:  1010100111001011011001
'       Octal:   12471331
'       Hex:     2a72d9

Working with Non-Decimal 32-Bit Integer Values

In addition to working with individual long integers as decimal values, you may want to perform bitwise operations with long integer values, or work with the binary or hexadecimal representations of long integer values. Int64 values are represented in 63 bits, with the sixty-fourth bit used as a sign bit. Positive values are represented by using sign-and-magnitude representation. Negative values are in two's complement representation. This is important to keep in mind when you perform bitwise operations on Int64 values or when you work with individual bits. In order to perform a numeric, Boolean, or comparison operation on any two non-decimal values, both values must use the same representation.

Fields

MaxValue

Represents the largest possible value of an Int64. This field is constant.

MinValue

Represents the smallest possible value of an Int64. This field is constant.

Methods

Abs(Int64)

Computes the absolute of a value.

Clamp(Int64, Int64, Int64)

Clamps a value to an inclusive minimum and maximum value.

CompareTo(Int64)

Compares this instance to a specified 64-bit signed integer and returns an indication of their relative values.

CompareTo(Object)

Compares this instance to a specified object and returns an indication of their relative values.

CopySign(Int64, Int64)

Copies the sign of a value to the sign of another value.

CreateChecked<TOther>(TOther)

Creates an instance of the current type from a value, throwing an overflow exception for any values that fall outside the representable range of the current type.

CreateSaturating<TOther>(TOther)

Creates an instance of the current type from a value, saturating any values that fall outside the representable range of the current type.

CreateTruncating<TOther>(TOther)

Creates an instance of the current type from a value, truncating any values that fall outside the representable range of the current type.

DivRem(Int64, Int64)

Computes the quotient and remainder of two values.

Equals(Int64)

Returns a value indicating whether this instance is equal to a specified Int64 value.

Equals(Object)

Returns a value indicating whether this instance is equal to a specified object.

GetHashCode()

Returns the hash code for this instance.

GetTypeCode()

Returns the TypeCode for value type Int64.

IsEvenInteger(Int64)

Determines if a value represents an even integral number.

IsNegative(Int64)

Determines if a value is negative.

IsOddInteger(Int64)

Determines if a value represents an odd integral number.

IsPositive(Int64)

Determines if a value is positive.

IsPow2(Int64)

Determines if a value is a power of two.

LeadingZeroCount(Int64)

Computes the number of leading zeros in a value.

Log2(Int64)

Computes the log2 of a value.

Max(Int64, Int64)

Compares two values to compute which is greater.

MaxMagnitude(Int64, Int64)

Compares two values to compute which is greater.

Min(Int64, Int64)

Compares two values to compute which is lesser.

MinMagnitude(Int64, Int64)

Compares two values to compute which is lesser.

Parse(ReadOnlySpan<Char>, IFormatProvider)

Parses a span of characters into a value.

Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)

Converts the span representation of a number in a specified style and culture-specific format to its 64-bit signed integer equivalent.

Parse(String)

Converts the string representation of a number to its 64-bit signed integer equivalent.

Parse(String, IFormatProvider)

Converts the string representation of a number in a specified culture-specific format to its 64-bit signed integer equivalent.

Parse(String, NumberStyles)

Converts the string representation of a number in a specified style to its 64-bit signed integer equivalent.

Parse(String, NumberStyles, IFormatProvider)

Converts the string representation of a number in a specified style and culture-specific format to its 64-bit signed integer equivalent.

PopCount(Int64)

Computes the number of bits that are set in a value.

RotateLeft(Int64, Int32)

Rotates a value left by a given amount.

RotateRight(Int64, Int32)

Rotates a value right by a given amount.

Sign(Int64)

Computes the sign of a value.

ToString()

Converts the numeric value of this instance to its equivalent string representation.

ToString(IFormatProvider)

Converts the numeric value of this instance to its equivalent string representation using the specified culture-specific format information.

ToString(String)

Converts the numeric value of this instance to its equivalent string representation, using the specified format.

ToString(String, IFormatProvider)

Converts the numeric value of this instance to its equivalent string representation using the specified format and culture-specific format information.

TrailingZeroCount(Int64)

Computes the number of trailing zeros in a value.

TryFormat(Span<Byte>, Int32, ReadOnlySpan<Char>, IFormatProvider)
TryFormat(Span<Char>, Int32, ReadOnlySpan<Char>, IFormatProvider)

Tries to format the value of the current long number instance into the provided span of characters.

TryParse(ReadOnlySpan<Char>, IFormatProvider, Int64)

Tries to parse a span of characters into a value.

TryParse(ReadOnlySpan<Char>, Int64)

Converts the span representation of a number to its 64-bit signed integer equivalent. A return value indicates whether the conversion succeeded or failed.

TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, Int64)

Converts the span representation of a number in a specified style and culture-specific format to its 64-bit signed integer equivalent. A return value indicates whether the conversion succeeded or failed.

TryParse(String, IFormatProvider, Int64)

Tries to parse a string into a value.

TryParse(String, Int64)

Converts the string representation of a number to its 64-bit signed integer equivalent. A return value indicates whether the conversion succeeded or failed.

TryParse(String, NumberStyles, IFormatProvider, Int64)

Converts the string representation of a number in a specified style and culture-specific format to its 64-bit signed integer equivalent. A return value indicates whether the conversion succeeded or failed.

Explicit Interface Implementations

IAdditionOperators<Int64,Int64,Int64>.Addition(Int64, Int64)
IAdditionOperators<Int64,Int64,Int64>.CheckedAddition(Int64, Int64)
IAdditiveIdentity<Int64,Int64>.AdditiveIdentity
IBinaryInteger<Int64>.GetByteCount()

Gets the number of bytes that will be written as part of TryWriteLittleEndian(Span<Byte>, Int32).

IBinaryInteger<Int64>.GetShortestBitLength()

Gets the length, in bits, of the shortest two's complement representation of the current value.

IBinaryInteger<Int64>.TryReadBigEndian(ReadOnlySpan<Byte>, Boolean, Int64)
IBinaryInteger<Int64>.TryReadLittleEndian(ReadOnlySpan<Byte>, Boolean, Int64)
IBinaryInteger<Int64>.TryWriteBigEndian(Span<Byte>, Int32)

Tries to write the current value, in big-endian format, to a given span.

IBinaryInteger<Int64>.TryWriteLittleEndian(Span<Byte>, Int32)

Tries to write the current value, in little-endian format, to a given span.

IBinaryNumber<Int64>.AllBitsSet
IBitwiseOperators<Int64,Int64,Int64>.BitwiseAnd(Int64, Int64)
IBitwiseOperators<Int64,Int64,Int64>.BitwiseOr(Int64, Int64)
IBitwiseOperators<Int64,Int64,Int64>.ExclusiveOr(Int64, Int64)
IBitwiseOperators<Int64,Int64,Int64>.OnesComplement(Int64)
IComparable.CompareTo(Object)

Compares this instance to a specified object and returns an indication of their relative values.

IComparisonOperators<Int64,Int64,Boolean>.GreaterThan(Int64, Int64)
IComparisonOperators<Int64,Int64,Boolean>.GreaterThanOrEqual(Int64, Int64)
IComparisonOperators<Int64,Int64,Boolean>.LessThan(Int64, Int64)
IComparisonOperators<Int64,Int64,Boolean>.LessThanOrEqual(Int64, Int64)
IConvertible.GetTypeCode()

Returns the TypeCode for value type Int64.

IConvertible.ToBoolean(IFormatProvider)

For a description of this member, see ToBoolean(IFormatProvider).

IConvertible.ToByte(IFormatProvider)

For a description of this member, see ToByte(IFormatProvider).

IConvertible.ToChar(IFormatProvider)

For a description of this member, see ToChar(IFormatProvider).

IConvertible.ToDateTime(IFormatProvider)

This conversion is not supported. Attempting to use this method throws an InvalidCastException.

IConvertible.ToDecimal(IFormatProvider)

For a description of this member, see ToDecimal(IFormatProvider).

IConvertible.ToDouble(IFormatProvider)

For a description of this member, see ToDouble(IFormatProvider).

IConvertible.ToInt16(IFormatProvider)

For a description of this member, see ToInt16(IFormatProvider).

IConvertible.ToInt32(IFormatProvider)

For a description of this member, see ToInt32(IFormatProvider).

IConvertible.ToInt64(IFormatProvider)

For a description of this member, see ToInt64(IFormatProvider).

IConvertible.ToSByte(IFormatProvider)

For a description of this member, see ToSByte(IFormatProvider).

IConvertible.ToSingle(IFormatProvider)

For a description of this member, see ToSingle(IFormatProvider).

IConvertible.ToType(Type, IFormatProvider)

For a description of this member, see ToType(Type, IFormatProvider).

IConvertible.ToUInt16(IFormatProvider)

For a description of this member, see ToUInt16(IFormatProvider).

IConvertible.ToUInt32(IFormatProvider)

For a description of this member, see ToUInt32(IFormatProvider).

IConvertible.ToUInt64(IFormatProvider)

For a description of this member, see ToUInt64(IFormatProvider).

IDecrementOperators<Int64>.CheckedDecrement(Int64)
IDecrementOperators<Int64>.Decrement(Int64)
IDivisionOperators<Int64,Int64,Int64>.Division(Int64, Int64)
IEqualityOperators<Int64,Int64,Boolean>.Equality(Int64, Int64)
IEqualityOperators<Int64,Int64,Boolean>.Inequality(Int64, Int64)
IIncrementOperators<Int64>.CheckedIncrement(Int64)
IIncrementOperators<Int64>.Increment(Int64)
IMinMaxValue<Int64>.MaxValue
IMinMaxValue<Int64>.MinValue
IModulusOperators<Int64,Int64,Int64>.Modulus(Int64, Int64)
IMultiplicativeIdentity<Int64,Int64>.MultiplicativeIdentity
IMultiplyOperators<Int64,Int64,Int64>.CheckedMultiply(Int64, Int64)
IMultiplyOperators<Int64,Int64,Int64>.Multiply(Int64, Int64)
INumber<Int64>.MaxNumber(Int64, Int64)
INumber<Int64>.MinNumber(Int64, Int64)
INumberBase<Int64>.IsCanonical(Int64)
INumberBase<Int64>.IsComplexNumber(Int64)
INumberBase<Int64>.IsFinite(Int64)
INumberBase<Int64>.IsImaginaryNumber(Int64)
INumberBase<Int64>.IsInfinity(Int64)
INumberBase<Int64>.IsInteger(Int64)
INumberBase<Int64>.IsNaN(Int64)
INumberBase<Int64>.IsNegativeInfinity(Int64)
INumberBase<Int64>.IsNormal(Int64)
INumberBase<Int64>.IsPositiveInfinity(Int64)
INumberBase<Int64>.IsRealNumber(Int64)
INumberBase<Int64>.IsSubnormal(Int64)
INumberBase<Int64>.IsZero(Int64)
INumberBase<Int64>.MaxMagnitudeNumber(Int64, Int64)
INumberBase<Int64>.MinMagnitudeNumber(Int64, Int64)
INumberBase<Int64>.One
INumberBase<Int64>.Radix
INumberBase<Int64>.TryConvertFromChecked<TOther>(TOther, Int64)
INumberBase<Int64>.TryConvertFromSaturating<TOther>(TOther, Int64)
INumberBase<Int64>.TryConvertFromTruncating<TOther>(TOther, Int64)
INumberBase<Int64>.TryConvertToChecked<TOther>(Int64, TOther)
INumberBase<Int64>.TryConvertToSaturating<TOther>(Int64, TOther)
INumberBase<Int64>.TryConvertToTruncating<TOther>(Int64, TOther)
INumberBase<Int64>.Zero
IShiftOperators<Int64,Int32,Int64>.LeftShift(Int64, Int32)
IShiftOperators<Int64,Int32,Int64>.RightShift(Int64, Int32)
IShiftOperators<Int64,Int32,Int64>.UnsignedRightShift(Int64, Int32)
ISignedNumber<Int64>.NegativeOne
ISubtractionOperators<Int64,Int64,Int64>.CheckedSubtraction(Int64, Int64)
ISubtractionOperators<Int64,Int64,Int64>.Subtraction(Int64, Int64)
IUnaryNegationOperators<Int64,Int64>.CheckedUnaryNegation(Int64)
IUnaryNegationOperators<Int64,Int64>.UnaryNegation(Int64)
IUnaryPlusOperators<Int64,Int64>.UnaryPlus(Int64)

Applies to

Thread Safety

All members of this type are thread safe. Members that appear to modify instance state actually return a new instance initialized with the new value. As with any other type, reading and writing to a shared variable that contains an instance of this type must be protected by a lock to guarantee thread safety.

See also