It is not uncommon to want to determine whether a number is even or odd. For integers backed by a binary representation (
BinaryInteger), this can be done very quickly by checking the trailing bit.
I think the standard library should use this trick when checking for multiples of 2, rather than doing a remainder operation and comparing that to 0.
I’ve had some trouble building the Swift standard library on my machine and profiling this change, so I’m reluctant to submit a pull request directly. It seems considerably faster to use
trailingZeroBitCount != 0 than
isMultiple(of: 2) when measured using
XCTest, for what that’s worth.
It also seems to compile much more compactly, though I doubt that benefit could be borne out in the same method.