Last active
November 26, 2021 07:30
-
-
Save celsojr/b4716e90a0449c1c8919d525596e5d40 to your computer and use it in GitHub Desktop.
A program that lists all available storage plans
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
long n = 8; | |
for (int i = 0; i < 60; i++) | |
{ | |
var storage = n << i; | |
Console.WriteLine($"{storage.Amount()} {storage.Unit()}"); | |
} | |
static class Extensions | |
{ | |
private static ReadOnlySpan<char[]> sizes => new[] | |
{ | |
new[] { 'M', 'B' }, | |
new[] { 'G', 'B' }, | |
new[] { 'T', 'B' }, | |
new[] { 'P', 'B' }, | |
new[] { 'E', 'B' }, | |
new[] { 'Z', 'B' }, | |
new[] { 'Y', 'B' } | |
}; | |
private static int Mag(this long value) | |
=> (int)Math.Log(value, 1024); | |
public static string Unit(this long value) => | |
String.Create(sizes[value.Mag()].Length, sizes[value.Mag()], (chars, buf) => | |
{ | |
for (int i = 0; i < chars.Length; i++) | |
{ | |
chars[i] = buf[i]; | |
} | |
}); | |
public static long Amount(this long value) => | |
value.Mag() switch | |
{ | |
1 => value / 1024, | |
var x when x > 1 => value / (1024L << ((value.Mag() - 1) * 10)), | |
_ => value | |
}; | |
} | |
/* Output | |
8 MB | |
16 MB | |
32 MB | |
64 MB | |
128 MB | |
256 MB | |
512 MB | |
1 GB | |
2 GB | |
4 GB | |
8 GB | |
... | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment