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
This is a follow up to the discussion with @RocketRoy regarding his answer, but it might be useful to anyone who wants to compare these results.
tl;dr From what I've seen, Roy's approach (
(0xFFFFFFFF == (x | 0xFFFFFFFE)
) is not completely optimized tox & 1
as themod
approach, but in practice running times should turn out equal in all cases.So, first I compared the compiled output using Compiler Explorer:
Functions tested:
CLang 3.9.0 with -O3:
GCC 6.2 with -O3:
Hats down to CLang, it realized that all three cases are functionally equal. However, Roy's approach isn't optimized in GCC, so YMMV.
It's similar with Visual Studio; inspecting the disassembly Release x64 (VS2015) for these three functions, I could see that the comparison part is equal for "mod" and "and" cases, and slightly larger for the Roy's "or" case:
However, after running an actual benchmark for comparing these three options (plain mod, bitwise or, bitwise and), results were completely equal (again, Visual Studio 2005 x86/x64, Release build, no debugger attached).
Release assembly uses the
test
instruction forand
andmod
cases, while Roy's case uses thecmp eax,0FFFFFFFFh
approach, but it's heavily unrolled and optimized so there is no difference in practice.My results after 20 runs (i7 3610QM, Windows 10 power plan set to High Performance):
The difference between these options is less than 0.3%, so it's rather obvious the assembly is equal in all cases.
Here is the code if anyone wants to try, with a caveat that I only tested it on Windows (check the
#if LINUX
conditional for theget_time
definition and implement it if needed, taken from this answer).Use the modulo (%) operator to check if there's a remainder when dividing by 2:
A few people have criticized my answer above stating that using x & 1 is "faster" or "more efficient". I do not believe this to be the case.
Out of curiosity, I created two trivial test case programs:
I then compiled these with gcc 4.1.3 on one of my machines 5 different times:
I examined the assembly output of each compile (using gcc -S) and found that in each case, the output for and.c and modulo.c were identical (they both used the andl $1, %eax instruction). I doubt this is a "new" feature, and I suspect it dates back to ancient versions. I also doubt any modern (made in the past 20 years) non-arcane compiler, commercial or open source, lacks such optimization. I would test on other compilers, but I don't have any available at the moment.
If anyone else would care to test other compilers and/or platform targets, and gets a different result, I'd be very interested to know.
Finally, the modulo version is guaranteed by the standard to work whether the integer is positive, negative or zero, regardless of the implementation's representation of signed integers. The bitwise-and version is not. Yes, I realise two's complement is somewhat ubiquitous, so this is not really an issue.
Checking even or odd is a simple task.
We just need to check divisibility of any number and for checking divisibility we use
%
operatorChecking even odd using if else
C program to check even or odd using if else
Using Conditional/Ternary operator
C program to check even or odd using conditional operator.
Using Bitwise operator
A number is even if, when divided by two, the remainder is 0. A number is odd if, when divided by 2, the remainder is 1.
Methods are great!
One more solution to the problem
(children are welcome to vote)