Skip to content

Instantly share code, notes, and snippets.

@zs40x
Last active April 12, 2016 19:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zs40x/0997d27757aac8f541106ef38147e9fc to your computer and use it in GitHub Desktop.
Save zs40x/0997d27757aac8f541106ef38147e9fc to your computer and use it in GitHub Desktop.
public class Program
{
public static void Main(string[] args)
{
PowerOf2Example();
}
private static void PowerOf2Example()
{
var numbers = new List<int>() {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
foreach (var number in GetPowersOf2( numbers ))
{
Console.WriteLine(number);
}
}
private static IEnumerable<int> GetPowersOf2(IEnumerable<int> numbers)
{
foreach (var number in numbers)
{
if((number != 0) && ((number & (number - 1)) == 0))
{
yield return number;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment