In the process of programming, we often encounter places where even numbers are judged, such as a list to determine the even number position plus a background. In the past, I always thought that there were two ways to judge even numbers, until I looked at the big guys on the Internet, I found that judging even numbers could still be played like this, which simply made me "surprised". The first two of them are also the easiest to think of and the most used ways.
1. Binary method
We all know that the numbers in the computer are represented by binary, and the numbers on you are either 0 or 1, such as the binary of 5 is 101, 6 is 110, 7 is 111, 8 is 1000, and so on. We found that the enumerated 4 numbers have a feature, the last digit of the odd number is 1, the last digit of the even number is 0, using this feature is easy to judge, the code implementation is as follows
public boolean isEven(int number) {
return (number & 1) == 0;
}
2. Residual method
When we were in school, we learned that the numbers that can be divisible by 2 are even numbers, and those that cannot be divisible are odd numbers. So we can judge a number divided by 2 to take the remainder, see whether the remainder is 1 or 0, so that we can also judge, the code implementation is as follows
public boolean isEven(int number) {
return number % 2 == 0;
}
These two ways are the most used in our daily development, but also the most easy to understand, the following several ways the average person really may not be able to think, here let everyone open their eyes, see if there is any way to make you feel "surprised".
3. Traversal method
It is difficult to think of judging an even number and still use the traversal method, who is it traversing, and how to traverse it? Speaking of the traversal method, I think it is better to call it a numeral method, what does it mean? Remember the experience of just learning to count, such as 1, 2, 3..., judging odd and even numbers can also be counted like this, such as starting from 0 is even, odd, even, odd ..., the code is implemented as follows
public boolean isEven(int number) {
boolean result = true;
for (int i = 0; i < number; i++) result = !result;
return result;
}
Seeing this traversal method, it reminds me of a "classic" sorting algorithm, sleep sorting method, which is not explained here, understand that people should understand, these two put together, comparable to Crouching Dragon Phoenix, sleep sorting method examples are as follows

4. Interception method
We all know that the number of bits, if it is 0, 2, 4, 6, 8, then this is an even number, otherwise it is an odd number. If you want to say so, you can take the rest of the judgment by dividing 10, but what does this have to do with interception, why is it called interception? I imagine the inner monologue of the big guy who invented this method: You want to divide? Do you have to take the rest in addition to the whole? How do I take the remainder? What to do with such a complex, string interception will not, direct interception of the last bit on the line, which is so complicated. An example is shown below
5. Exhaustive law
Yes, judging odd and even numbers can also be exhausted, and I can exhaust as many scenes as you want. forehead...... I really can't make it up, why should I use poor moves, can you understand why he uses poor moves? Looking at this screenshot, is 0 odd or even?
Surprise, not surprise, simple single odd and even number of judgments, can play out flowers, sometimes think about it, writing code is not so boring, when you want to find some fun, the big guys will always bring a little surprise to make you happy.