An odd number always has the LSB set to 1. If we AND the given number with 1, the even number will always turn out to be zero. We can using this to check if the number is odd or even.
Examples:
1) n = 4
100 (4)
001 (1)
———– AND
000
2) n = 7
101 (7)
001 (1)
———— AND
001
C Program:
#include int main() { int n = 11; int result = n & 1; if(result) printf("Odd number\n"); else printf("Even number\n"); return 0; }
Reblogged this on PH Bytes.
LikeLike