A look at the difference between the modulo and the remainder operators
Photo by Caspar Camille Rubin on Unsplash
This is different from languages like Java, where % is the modulo operator.
In this piece, we?ll look at the difference between the modulo and the remainder operator.
The modulo operator works like the mod operator in math. It?s a basic part of modular arithmetic, which works like the clock. The number wraps around to something smaller than the given value, when it?s bigger than it.
For example, a clock has 12 hours. We represent that in math with by writing x mod 12 where x is an integer. For example if x is 20 then 20 mod 12 is 8 since we subtract 12 until it?s between 0 and 11.
Another example would be a negative number for x. If x is -1, then -1 mod 12 is 11 since we add 12 to it to make it within between 0 and 11.
12 mod 12 is 0 since we subtract 12 from it until it?s within the same range.
The operand after the mod can be positive or negative.
If the right-hand operand is negative, then the range of it must be from the negative number plus 1 to 0.
For example, if we have 1 mod -3 . Then we subtract 3 from it to get -2 .
To see more properties of modular arithmetic, check out this article for modular arithmetic and this article for the modulo operator from Wikipedia.
10 % 2
we get 0 since 10 is evenly divisible by 2.
If the first operand isn?t even divisible by the second operand, then we get a non-zero remainder. For example, if we have:
10 % 3
Then we get 1 since 10 divided by 3 has a remainder of 1.
Since the percent sign is a remainder operator, it also works if either number is negative. For example, if we have:
10 % -3
Then we get 1 because the quotient is -3 and the remainder is 1.
On the other hand, if we write:
-10 % 3
Then we get -1 because the quotient is -3 and the remainder is -1.
Bitwise Operator for Doing Modular Arithmetic
We can use the >>> operator, which is the zero left shift operator, to compute a number modulo 2 to the 32nd power.
The zero left shift operator shifts right by pushing zero in from the left and the rightmost one falls off the shift.
For example, if we write:
2**32 >>> 32
Then we get 0 since we pushed 32 zeroes in from the left, which pushed all the ones out.
Writing 2**32 >>> 0 is the same as 2**32 >>> 32.
If we write 2**32 + 1 >>> 32 then we get 1 since we added the 33rd bit on the left with the value 1, then we pushed in 32 zeroes from the left, leaving only 1 bit left.
Photo by Artem Riasnianskyi on Unsplash
Using Typed Array for Modulo Operation
We can also use typed arrays like the Uint8Array, Uint16Array, and Uint32Array for modulo operations since each entry can only be 0 to 2**8?1, 0 to 2**16?1, or 0 to 2**32?1respectively. The U in the first character of the name means unsigned.
In each example below, we create a typed array with one entry, then we assign various values to it to compute x mod 2**8 , x mod 2**16 and x mod 2**32 respectively.
For example, if we write:
const arr1 = new Uint8Array(1);arr1 = 2**8;console.log(arr1);arr1 = 2**8 + 1;console.log(arr1);
Then we get that the first console.log gives us 0 and the second console.log gives us 1 since the entries are wrapped to be between 0 and 2**8 – 1.
Likewise, we can do the same thing with the other kinds of typed arrays as follows:
const arr1 = new Uint16Array(1);arr1 = 2**16;console.log(arr1);arr1 = 2**16 + 1;console.log(arr1);
const arr1 = new Uint32Array(1);arr1 = 2**32;console.log(arr1);arr1 = 2**32 + 1;console.log(arr1);
Then we get the same results as the first example.
One example would be this:
const mod = (a, b) => ((a % b) + b) % b
It wraps the results of a % b to be within 0 and b ? 1 or b+1 and 0 if b is negative by adding a % b to b. a % b is always less than a since it?s the remainder, but it might not be within the range of 0 and b ? 1 or b+1 and 0and 0 if b is negative so we add b to it.
If we write:
console.log(mod(1, 12));console.log(mod(13, 12));console.log(mod(13, -12));
Then we should get:
This is what we expect.