Tom Tetlaw
Tom Tetlaw

Reputation: 113

Fast find multiple of 4

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

Answers (1)

Ben
Ben

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

Related Questions