Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible to do this without a loop?

Tags:

function

c#

loops

I'm having trouble thinking of a solution to this problem that doesn't involve a loop. Basically, if something is greater than some arbitrary number, it loops around. Let's say 64 is the number.

0 => 0
32 => 32
64 => 64
96 => 32
128 => 64
160 => 32
192 => 64

Et cetera.

The way I'm currently doing it involves a while loop that checks to see if the value is over 64 and if it is, subtract 64 from it. Is there another way to do it that doesn't involve loops?

I'm using C# WinForms.

like image 368
John Smith Avatar asked Dec 21 '22 10:12

John Smith


1 Answers

Mod the value by 64, it's an O(1) operation. Like this:

int number;
// number is initialized
number %= 64;
like image 160
djhaskin987 Avatar answered Dec 24 '22 02:12

djhaskin987