What is the simplest way to calculate the amount of even numbers in a range of unsigned integers?
An example: if range is [0...4] then the answer is 3 (0,2,4)
I\
The first even number in the range is: (begin + 1) & ~1
(round begin
up to even number).
The last even number in the range is: end & ~1
(round end
down to even number).
The total number of even numbers in the range is therefore: (end & ~1) - ((begin + 1) & ~1) + 1
.
int num_evens = (end & ~1) - ((begin + 1) & ~1) + 1;