Two methods of odd and even judgment

First of all, let's talk about the simplest way to determine odds and even numbers, that is, if a number is x, if x is divided by 2 and the remainder is 1, it is an odd number; if x is divided by 2 and the remainder is 0, it is an even number. The details are as follows:
Two methods of odd and even judgment
This is relatively simple Next, let’s take a look at another judgment method: use bitwise AND to judge the parity of numbers:
bitwise AND needs to convert two numbers into binary form and then judge them one by one. In other words, "When the two digits of the same digit are both 1, the result is 1; if there is a number with this digit of 0, then the result of the same digit is 0. For
example, 10&13, All converted to binary:
1010
&
1101.
At this time, the result here is 1000. After
understanding the principle of bitwise AND, we can look at the following code:
Two methods of odd and even judgment
The most important thing for this method is that except for the lowest bit, other The number of digits can be divisible by 2, so whether the final number is odd or even depends on the lowest bit of x.
If there is a better method, please feel free to make additional explanations.

Guess you like

Origin blog.51cto.com/14961688/2542480