← Go Back

Awesomeness of % (Modulo Operator)

Broken Post?Let me know

One day, I was solving a coding question, for which, I had to shift each element in an array to the right. It is an easy task to do but I wanted to know if there was more "elegant way". After doing some Googling, I found one entry on StackOverflow, which blew my mind and I'd like to share it.

I've never much utilized an operator for modulo except when I had to check for "even" or "odd" numbers.

// right shift with modulus for (int i = 0; i < arr.length; i++) { demo[(i+1) % demo.length] = arr[i]; }

The code snippet above is an answer by Artemkller545 to this question, shifting array elements to right? on StackOverflow. An example of shifting each element array to right would be

you have an array, 1 2 3 4 5 6

Shifting it to right by one: 6 1 2 3 4 5

When I looked at the code, it broadened my view of how I can use modulo and thought that I can use other operators in more clever way like that. The code isn't clever enough so that it'd need more than a minute to understand but it makes the shifting look dead simple.

I've learned a new trick today and just knowing one such application of an operator broadened my horizon.