Last active
April 12, 2016 19:20
-
-
Save zs40x/0997d27757aac8f541106ef38147e9fc to your computer and use it in GitHub Desktop.
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
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