The arithmetic module

The "module" is one of the aritmentiche available on almost all programming languages (I used it the first time with the Basic Commodore 64) . It may appear as a simple operator, as in the case of Actionscript or Javascript that both use the character "percent" (% b), or as a method or instruction. For developers may be useful in contexts apparently different. Gskinner spoke recently, showing some classic but interesting examples.

Put simply, the operation returns two numbers form the rest of their division. For completeness, we say that the operation of the module is a very broad topic, which I discussed in the RSA encryption ! This time, however, will not talk of codes or ciphers, but much more simple and useful things.

Divisible by toggle ...

The first use that we can make the operation of the module is to determine whether a number is divisible by a number b! If the risultatto of a% b (I will use the notation here% to indicate the operation of the module) is zero, a is divisible by b. I remember when I dealt with a similar problem did not know about the operation of the module. On the Commodore 64 I needed to know if a number was divisible by 2. At the time I used this syntax (I found the code origionale - year 1983):

1
2
3
10 INPUT A
12 B = A / 2: IF B = INT (B) THEN PRINT "OK THE NUMBER" A "AND 'EQUAL": GOTO 10
14 PRINT "THE NUMBER" A "AND 'ODD": GOTO 10

The BASIC of the Commodore 64, however, did not have the operation module - at least that I remember!

In practice doing exactly that by its nature, makes the operation of module: verificavo that the division by 2 did not rest, in this case verificavo that the result did not contain decimal ( IF B = INT(B) )! If the division operation is equal to its full, then the number is equal!

If a have our value to be checked, the same thing can be resolved by:

1
2
3
a % 2 ) == 0 ) { if ((a% 2) == 0) {
/ / Even
}

Or, for 3, from:

1
2
3
a % 3 ) == 0 ) { if ((a% 3) == 0) {
/ / Divisible by 3
}

Small note: thanks to the track if it has to do with him even powers of 2 there is a quicker way to check if a number is even or odd. In binary notation, in fact, the numbers all have the first bit equal to zero! Thus, using the logical operators, you can know if a number, odd or even simply "testanto" the first bit:

1
2
3
! ( a & 1 ) ) { if ((a & 1)) {
/ / Even
}

In the Assembly, for example, this is their daily bread! It is even more immediate and simple (the Motorola 68020, for example, had an instruction BTST that "tested" just a single bit ;) ). – moltiplicazione bit a bit) tra il nostro valore a e 1 (chiamato maschera ). The logical operations (AND, OR, NOT, XOR, etc ...), in fact, are notoriously quick to level and then eseguzione "time machine"! (a & 1) performs the logical AND ( & - multiplication bitwise) between our value a , and 1 (called mask). If a = 7 (00000111), for example:

1
2
3
00000111 AND
00000001 =
00000001; odd

If a = 8:

1
2
3
00001000 AND
00000001 =
00000000; equal

Not only can we see the even numbers, ie the first bit to 0, and therefore divisible by 2, but can also occur if a number is divisible by 4, 8, 16, 32, 64, etc ... using the appropriate forms:

1
2
3
! ( a & 3 ) ) { if ((a & 3)) {
/ / Divisible by 4 (mask = 00000011 = 3), since 4 is 00000100)
}

If the last two bits are to zero the number is divisible by 4! If the last 3 bits (mask 00000111) are to zero the number is divisible by 8! And so on :)

Gskinner shows the use of the module in case of generation of alternating values. A classic example is to change the background of a list of items: a white and gray. Gskinner proposes:

1
2
3
4
5
rowIndex % 2 == 0 ) { if (rowIndex% 2 == 0) {
rowColor = 0xFFFFFF;
{ Else {}
rowColor = 0xCCCCCC;
}

First of all the code above can be solved with a single line of code:

1
2 == 0 ) ? 0xFFFFFF : 0xCCCCCC ; rowColor = (rowIndex% 2 == 0)? 0xFFFFFF: 0xCCCCCC;

Furthermore, in this specific case, would not actually perform a "heavy" operation of the module. This particular situation is often engaged in a loop, so it would be "good" not to waste clock cycles the machine. In these cases I like the cleanest (and logical) solution:

1
fooIndex == false ) ) ? 0xFFFFFF : 0xCCCCCC ; rowColor = (fooIndex = (fooIndex == false))? 0xFFFFFF: 0xCCCCCC;

o false , a seconda di come vogliamo impostare inizialmente il nostro colore sfondo. Where fooIndex precedentemete is a value set to true or false , depending on how we initially set our background color. This approach is more subtle, less immediate, at first, but faster because it performs logical true / false. la condizione (fooIndex == false) sarà falsa e di conseguenza fooIndex verrà impostato da true a false. If fooIndex is true, when the statement is executed (fooIndex = (fooIndex == false) ) the condition (fooIndex == false) is false and consequently fooIndex is set from true to false. verrà impostato da false a true! The second time, however, the condition (fooIndex == false) will be true, and then fooIndex will be set from false to true! And so on ...

Repetitions: to watch the behavior

The finite arithmetic, which is the basis of the functioning of the operation of the module, is often referred to as clock arithmetic. A classic watch with hands has a dial divided into 12 hours (12 plus 12 day night!), Sufficient to indicate any one of 24 hours a day. At 08:00 AM If I asked you what time your watch will score between 18 hours, your answer would be 8 +18 = 26! But the 02:00 at night! Involuntarily and without perhaps knowing it, we perform operations conitnuamente form whenever we have to do with the times.

In Italy we are used to the system 24 hours (or mixed). When we point to 16:00 quell'orario mean that in other countries is indicated as 04:00 pm. Who uses the postfix am / pm to distinguish the hours diune from those night, even more often performs the operation of the module, in this case module 12! We Italians, and others, however, being accustomed to consider the 24 hours, perform operations with Form 24!

When I'm 11 in the morning (11:00 for all) and think "in 5 hours", the Italians respond 5 +11 = 16 (four in the afternoon), a stranger could also answer 5 +11 = 4 pm. Since 5 + 11 classical arithmetic, is obviously 16, as did the stranger to meet 4 pm? He simply ran the module 12 on the result: 5 + 11 = 16 mod 12 = 4. It is not necessary that you take a calculator to check the accuracy of the calculation, but these steps are a bit 'all, displayed a face of a clock and place the hands on the 11 mentally, then add - always visually - 5 hours and, as by magic you'll actually about 4! That's why four in the afternoon are called 16 :)

In Italy, instead, we use a Form 24, in fact: 11 +5 = 16 mod 24 = 16. When I am to 23:00 pm, 5 hours after the 28:00 will not be, because form 28 12 = 4! 122514884225 Form 24 = 17! What happens is that the key result of the Form 24 will never exceed 24. But the most interesting thing is that the numbers are repeated indefinitely:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
1% 24 = 1
2% 24 = 2
3% 24 = 3
...
22% 24 = 22
23% 24 = 23
24% 24 = 0
25% 24 = 1
26% 24 = 2
27% 24 = 3
...
48% 24 = 0
49% 24 = 1
50% 24 = 2
51% 24 = 3
...

This repetition is a disarming ease. Suppose, for example, of having to have an object, a MovieClip, similarly to a chessboard, in four columns. Create a MovieClip quandrato, 55 × 55 pixels and the properties panel esportiamolo as Simbolo . The following code will have our MovieClip on four columns, and will "wrap" with the operation of the module:

1
2
3
4
5
6
7
Number = 4 ; var col: Number = 4;
var i= 0 ; i < 12 ; i ++ ) { for (var i = 0; i <12; i + +) {
Sprite = new Simbolo ( ) ; var s: Sprite = new Symbol ();
s ) ; addChild (s);
= ( i % col ) * 60 ; s. = x (with i%) * 60;
= Math . floor ( i / col ) * 60 ; s. y = Math . floor (i / col) * 60;
}

cresce in modo indefinito. As shown by the code, the variable i cycle for growing indefinitely. But the coordinate x of our MovieClip never exceeds a certain value, and is repeated indefinitely! Here is a concrete example:

Flash Player Loading ...

Using the two knobs can vary the number of columns (the module) and the total number of elements, using the algorithm above.

Comment on: "The arithmetic form"

Leave a comment

TAG XHTML PERMITS: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> INSERTION CODE:
 <pre></pre> // blocco generico <code></code> // blocco generico [cc_actionscript][/cc_actionscript] // Actionscript [cc_actionscript3][/cc_actionscript3] // Actionscript 3 [cc_css][/cc_css] // CSS Style Sheet [cc_html][/cc_html] // HTML [cc_js][/cc_js] // Javascript [cc_objc][/cc_objc] // Objective-C [cc_php][/cc_objc] // PHP [cc_sql][/cc_sql] // SQL 


Stop SOPA