Skip to content

Instantly share code, notes, and snippets.

@NovemberDev
Created August 18, 2020 14:17
Show Gist options
  • Save NovemberDev/14807e6e968f374bb687c9f3787d503d to your computer and use it in GitHub Desktop.
Save NovemberDev/14807e6e968f374bb687c9f3787d503d to your computer and use it in GitHub Desktop.
public static bool IsEven(int i) {
return (i & 1) == 0;
}
/*
Returns if the given integer is even using Bitwise & AND operator:
1010 equals 10 in decimal
0001 equals 1 in decimal
0000 AND returns 0 so it is even (0 of 10, 1 of 1)
0011 equals 3 in decimal
0001 equals 1 in decimal
0001 AND returns 1 so it is uneven (1 of 3, 1 of 1)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment