How can I check if a given number is even or odd in C?
相关问题
- Multiple sockets for clients to connect to
- Do the Java Integer and Double objects have unnece
- What is the best way to do a search in a large fil
- glDrawElements only draws half a quad
- Index of single bit in long integer (in C) [duplic
IsOdd(int x) { return true; }
Proof of correctness - consider the set of all positive integers and suppose there is a non-empty set of integers that are not odd. Because positive integers are well-ordered, there will be a smallest not odd number, which in itself is pretty odd, so clearly that number can't be in the set. Therefore this set cannot be non-empty. Repeat for negative integers except look for the greatest not odd number.
+66% faster >
!(i%2) / i%2 == 0
The code checks the last bit of the integer if it's 1 in Binary
Explanation
the & bitwise AND operator checks the rightmost bit in our return line if it's 1
Think of it as true & false
When we compare n with 1 which means
0001
in binary (number of zeros doesn't matter).then let's just Imagine that we have the integer n with a size of 1 byte.
It'd be represented by 8-bit / 8-binary digits.
If the int n was 7 and we compare it with 1, It's like
Which F stands for false and T for true.
What if I want to check the bit before the rightmost?
Simply change
n & 1
ton & 2
which 2 represents0010
in Binary and so on.I suggest using hexadecimal notation if you're a beginner to bitwise operations
return n & 1;
>>return n & 0x01;
.For the sake of discussion...
You only need to look at the last digit in any given number to see if it is even or odd. Signed, unsigned, positive, negative - they are all the same with regards to this. So this should work all round: -
The key here is in the third line of code, the division operator performs an integer division, so that result are missing the fraction part of the result. So for example 222 / 10 will give 22 as a result. Then multiply it again with 10 and you have 220. Subtract that from the original 222 and you end up with 2, which by magic is the same number as the last digit in the original number. ;-) The parenthesis are there to remind us of the order the calculation is done in. First do the division and the multiplication, then subtract the result from the original number. We could leave them out, since the priority is higher for division and multiplication than of subtraction, but this gives us "more readable" code.
We could make it all completely unreadable if we wanted to. It would make no difference whatsoever for a modern compiler: -
But it would make the code way harder to maintain in the future. Just imagine that you would like to change the text for odd numbers to "is not even". Then someone else later on want to find out what changes you made and perform a svn diff or similar...
If you are not worried about portability but more about speed, you could have a look at the least significant bit. If that bit is set to 1 it is an odd number, if it is 0 it's an even number. On a little endian system, like Intel's x86 architecture it would be something like this: -
[Joke mode="on"]
[Joke mode="off"]
EDIT: Added confusing values to the enum.
Portable:
Unportable: