Posts Tagged mod

Bitwise Alternatives to Multiply, Divide, and Modulus: Faster?

Tags: , , , , , , ,

Many programmers are aware of a special case where you can use a bitwise shift for multiplication or division when you’re multiplying or dividing by a power of two. For example, you can replace i * 2 with i < < 1 and i / 2 with i >> 1. A lesser-known trick works for modulus. But are these alternatives actually any faster? Today’s article puts them to the test!

Read the rest of this article »


Faster isEven

Tags: , , , ,

A common programming task is to determine if an integer is even or odd. Recently, I saw an article showing how to do the task faster than the usual way: (i % 2) == 0. Today’s article shows an even faster way to check for even or odd.

Read the rest of this article »