The "module" is one of the aritmentiche available on almost all programming languages (I used it the first time with the Basic Commodore 64) . May appear as a simple operator, as in the case of Actionscript or Javascript, which 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, not talk of codes or ciphers, but useful things much simpler.
Divisible by alternating ...
The first use 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 here the notation to indicate the operation of the module) is zero, a is divisible by b. I remember when I had to do with a problem like I did not know 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!
Basically exactly what I was doing that by its nature makes the operation of module: 2 verified that the division had no rest, in this case, check 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 tested, the same thing can be resolved by:
1 2 3
| a % 2 ) == 0 ) { if ((a% 2) == 0) { / / Even } |
Or, for 3, by:
1 2 3
| a % 3 ) == 0 ) { if ((a% 3) == 0) { / / Divisible by 3 } |
Small note: thanks to the track if you have to do with the powers of 2 annche there a faster way to check whether a number is odd or even. In binary notation, in fact, the numbers are all equal to zero the first bit! Thus, using the logical operators, you can know whether a number is odd or even simply "Protestant" the first bit:
1 2 3
| ! ( a & 1 ) ) { if ((a & 1)) { / / Even } |
In the Assembly, for example, this is bread and butter! 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 known to be faster in terms of run, and then "time machine"! (a & 1) performs the logical operation of 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 check the numbers, that is, with 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 set to zero the number is divisible by 4! If the last 3 bits (mask 00,000,111) are to zero the number is divisible by 8! And so on 
Gskinner shows the use of the module in cases of alternate generation of values. A classic example is to change the background of a list of items: a white and a gray. Gskinner offers:
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; |
Moreover, in this specific case, I would avoid actually perform a "heavy" form of operation. 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 will be set from true to false. verrà impostato da false a true! The second time, however, the condition (fooIndex == false) is true and then fooIndex is 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, it is often referred to as clock arithmetic. A classic timepiece has a dial with hands 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 knowing it, perhaps, perform operations conitnuamente form whenever we are dealing with times.
In Italy we are used to the system 24 hours (or mixed). When we point to 16:00 intend that hour than in other countries is indicated as 04:00 pm. Who uses the postfix am / pm to distinguish from those ofa night hours, even more often performs the operation of the module, module 12 in this case! 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 run the module 12 on the result: 5 + 11 = 16 mod 12 = 4. It is not necessary that you take a calculator to verify 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, such as if by magic you'll actually about 4! That's why they are called the four in the afternoon 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 28 modulo 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. Imagine, for example, they should have an object, a MovieClip, similar 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 four columns of our MovieClip and will "head" with the operation of 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 you can see from the code, the variable i cycle for growing indefinitely. But the coordinate x of our MovieClip never exceeds a certain value, and repeats indefinitely! Here is a concrete example:
Through the two knobs you can vary the number of columns (the form) and the total number of elements, using the algorithm above.
Continued ...
I opened a project on Google Code : undolibrary. For now, this library contains only one component (Knob - knob) to be used with Actionscript 3.0. I mention this because this component is a prerequisite to a series of tutorials / sources in Flash that I wanted to make available.
Google Code
First of all I introduce the concept of Google Code for those who were foreign. Google Code is a free service provided by Google in order to share code, but, above all, to share and collaboratively with a versioning system (subversion) and wikis useful to development teams (primarily open source) or to individual programmers. On the Google Code site include:
You can search for other projects, with a purpose-built search engine. Many development groups, made up of teams that are located in distant geographical areas, taking advantage of this sharing to work together, backed by a well established versioning system (checkout / checkin not to overlap or supprot to create branch - spinoff projects) . Libraries and tools such as Papervision3D , Tweener or AS3corelib , for example, are located right on Google Code enjoying one side of a host of other development and public access to download the source and documentation.
Important point, then, is the possibility - open a project - to set access and viewing. If you want to protect your work or make it completely private rather than public.
Continued ...
Most likely, when it comes out the new version of Adobe Flash, 3D libraries to "low level" as FIVe3D will end up badly! However, before the "Trash", here is an example of what can be achieved (with the latest release 2.1):
The code of the demo above is quite articulate, so I do not propose entirely online. Insertion, however, some interesting pieces of code.
Update: for the source click here
Continued ...
Latest Comments
Mark : @ Mark: Thanks for the answer. For example, if you have a code to save / use the folder ...
Mark : Thanks for the answer. For example, if you have a code to save / use the folder ...
Giovambattista Fazioli : @ Mark: in principle, absolutely, a lot depends on how and what to download. La. ..
Mark : Hello, good guide! I wanted to ask, you can use the same code to run a sql database? Instead of ...
Joseph : Hi I wanted to ask you how you can do in the post I uploaded a gallery of 50 images, for example, ...