The percent symbol (%) has a secret double life. It is also known as the modulus operator. The modulus operator is used for applications where you need to restrict a value to a certain range, continuously loop through a range of values, skipping over values and much more.
This operator is important for implementing our Caesar Cipher. We introduced the Caesar Cipher in the previous post. In this application we needed a way to loop around when we shifted the letters. The modulus operator allows us to do just that.
All the mod operator does is give us the remainder of a value when that value is divided by the number that comes after it. The implication is that the modulo operator will not give a value that is larger than the divisor. We use 26 as the divisor (to represent the 26 letters)
An array of numbers has the index of 0…N-1, N being the array length. To encode the letter ‘A’ with a right shift of 4 we take the index of the letter (0) and add 4 to it to get 4. And then we divide 4 by 26 and take the remainder to get the letter ‘E’
4 mod 26 = 4
That was easy because the value is still less than 25. When we take the letter ‘Y’ with and index of (24) and add 4 to it we get 28. There is no 28th letter of the alphabet, so we use 28 mod 26 to get to the encoded value of 2, which is the index of the letter ‘C’.
28 mod 26 = 2
When we decode a letter we do the same thing but in reverse. We subtract the shift from the index and then add 26 if it is negative. When we take ‘C’ with an index of 2 and subtract 4 we get the value of -2. There is no index of alphabet letters that is negative and so we again use mod 26 which allows us to loop back to the top of the range and we get back 24, the original letter of ‘Y’.%
Thus we describe the encoding and decoding of the shift cipher with 2 equations:
One for encoding:
e(p, s) = (p + s) mod 26
and one for decoding:
d(c, s) = (p - s) mod 26
Next time we will go through how to write a computer program that can use this to encode and decode entire text files.