How to define a petabyte constant in C#

1 Answer

0 votes
using System;

class PetabyteExample
{
    /*
        In C#, the largest unsigned integer type is 'ulong' (64‑bit).
        Its maximum value is about 1.84 × 10^19.

        A petabyte fits comfortably:

            1 PiB = 2^50  = 1,125,899,906,842,624 bytes
            1 PB  = 10^15 = 1,000,000,000,000,000 bytes

        So both can be stored safely in a ulong.
    */

    // Binary petabyte (pebibyte), using a bit shift.
    // 1UL << 50 = 2^50 bytes.
    public const ulong PETABYTE_PIB = 1UL << 50;

    // Decimal petabyte (SI petabyte), using a readable numeric literal.
    // C# allows underscores for digit grouping.
    public const ulong PETABYTE_PB = 1_000_000_000_000_000UL;

    static void Main()
    {
        Console.WriteLine("1 PiB = " + PETABYTE_PIB + " bytes");
        Console.WriteLine("1 PB  = " + PETABYTE_PB + " bytes");
    }
}



/*
run:

1 PiB = 1125899906842624 bytes
1 PB  = 1000000000000000 bytes

*/

 



answered May 2 by avibootz
...