Reputation: 113
Is there a fast way to find if a 32-bit integer is a multiple of 4 without using the % operator (In C++)?
Upvotes: 1
Views: 439
Reputation: 35643
yes, there is.
((i & 3) == 0)
Note that this may not be any faster. Also a good optimizing compiler will convert your modulus against constant 4 to the fastest operation anyway, so it may well generate this automatically.
Check the generated code if you are interested.
Upvotes: 22