public static class NumberExtensions
public static bool IsPowerOf2(this long input)
public static bool IsPowerOf2Alt(this long input)
long count = input - ((input >> 1) & 033333333333) - ((input >> 2) & 011111111111);
return ((count + (count >> 3)) & 030707070707) % 63 == 1;
private static readonly (long Input, bool Expected)[] Cases =
RunTests(NumberExtensions.IsPowerOf2);
static void RunTests(Func<long, bool> method)
foreach (var test in Cases)
var actual = method(test.Input);
var result = actual.Equals(test.Expected);
Console.WriteLine($"\"{test.Input}\": OK");
Console.WriteLine($"\"{test.Input}\": Fail (expected: \"{test.Expected}\", actual: \"{actual}\")");
Console.WriteLine($"\"{test.Input}\": Fail with exception:\r\n{ex}");