viewing paste Unknown #17016 | Text

Posted on the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
 &  - And.
 |  - Or.
        The bitwise operator AND (&) is used to test two values against each
    other, and results in setting bits which are active in both arguments.
    This can be used for a few things, but in Hercules this operator is
    usually used to create bit-masks in scripts.
       
        The bitwise operator OR (|) sets to 1 a binary position if the binary
    position of one of the numbers is 1. This way a variable can hold
    several values we can check, known as bit-mask. A variable currently
    can hold up to 32 bit-masks (from position 0 to position 1). This is a
    cheap(skate) and easy way to avoid using arrays to store several
    checks that a player can have.
       
        A bit-mask basically is (ab)using the variables bits to set various
    options in one variable. With the current limit in variables it is
    possible to store 32 different options in one variable (by using the
    bits on position 0 to 31).
 
        Example(s):
        - Basic example of the & operator, bit example:
                10 & 2 = 2
        Why? :
                10 = 2^1 + 2^3 (2 + 8), so in bits, it would be 1010
                2 = 2^1 (2), so in bits (same size) it would be 0010
                The & (AND) operator sets bits which are active (1) in both
        arguments, so in the example 1010 & 0010, only the 2^1 bit is
        active (1) in both. Resulting in the bit 0010, which is 2.
        - Basic example of creating and using a bit-mask:
                .@options = 2|4|16; // (note: this is the same as 2+4+16, or 22)
                if (.@options & 1) mes "Option 1 is activated";
                if (.@options & 2) mes "Option 2 is activated";
                if (.@options & 4) mes "Option 3 is activated";
                if (.@options & 8) mes "Option 4 is activated";
                if (.@options & 16) mes "Option 5 is activated";
        This would return the messages about option 2, 3 and 5 being shown
    (since we've set the 2,4 and 16 bit to 1).
 ^  - Xor.
        The bitwise operator XOR (eXclusive OR) sets a binary position to 0 if
    both numbers have the same value in the said position. On the other
    hand, it sets to 1 if they have different values in the said binary
    position. This is another way of setting and unsetting bits in
    bit-masks.
 
        Example:
        - First let's set the quests that are currently in progress:
                inProgress = 1|8|16; // quest 1,8 and 16 are in progress
        - After playing for a bit, the player starts another quest:
                if( inProgress&2 == 0 ){
                        // this will set the bit for quest 2 (inProgress has that bit set to 0)
                        inProgress = inProgress^2;
                        mes "Quest 2: find a newbie and be helpful to him for an hour.";
                        close;
                }
        - After spending some time reading info on Xor's, the player finally
      completes quest 1:
                if( inProgress&1 && isComplete ) {
                        // this will unset the bit for quest 1 (inProgress has that bit set to 1)
                        inProgress = inProgress^1;
                        mes "Quest 1 complete!! You unlocked the secrets of the Xor dynasty, use them wisely.";
                        close;
                }
Viewed 641 times, submitted by Ancyker.