Difference between revisions of "Operator Reference"

1,805 bytes added ,  16:37, 24 June 2018
m
→‎Note: Made the first sentence easier to read by swapping the order of the hexadecimal and decimal example values.
imported>Homecom
m (→‎Math Operators: corrected either a use of the term "byte" where "nibble" was intended, or use of a nibble count (6) where a byte count (3) was required.)
imported>Homecom
m (→‎Note: Made the first sentence easier to read by swapping the order of the hexadecimal and decimal example values.)
 
(2 intermediate revisions by the same user not shown)
Line 67: Line 67:
</source>
</source>


Note:
=== Note ===
* Modulous unreliable for values greater than 0x80000000
 
* Code that uses modulous to manually isolate the low-order 3 bytes (6 nibbles) of a form ID will fail if the user has more than 127 mods installed.
In the past, there was concern about the correctness of the [https://en.wikipedia.org/wiki/Modulo_operation modulo] (<code>%</code>) operator when applied to negative integers such as -2,147,483,647 (0x80000001). Those concerns were unfounded.
 
''The modulo operator produces correct results in all cases'', but it must be understood that the result will have the sign of the dividend in Papyrus (as in Java and many other languages), therefore <code>3 % 2 == 1</code> and <code>-3 % 2 == -1</code>.
 
A specific case of concern was the calculation necessary to isolate the low-order 3 bytes (6 nibbles, or 24 bits) of an integer, which requires a modulo operation with a divisor of 2^24 (AKA 0x01000000 or 16,777,216). When that modulo operation is applied to dividends in the range -2,147,483,647 (0x80000001) to -1 (0xFFFFFFFF), inclusive, the results correctly range from -16,777,215 (0xFF000001) to -1 (0xFFFFFFFF). However, the goal of isolating the low-order 3 bytes of the integer (performing an operation equivalent to a bit-wise AND with 0x00FFFFFF) is ''not'' met in those cases. In order to meet that goal, negative values must be handled specially. For example:
 
<source lang="papyrus">
int Function getLow3Bytes(int i)
{Set the high 8 bits of i to zero, and return the low 24 bits intact.
Equivalent to "i & 0x00FFFFFF" in C, Java, etc.}
    if i < 0
        ; Force i to be a positive number, by changing its high bit (bit 32) from 1 to 0.
        i += 0x80000000
    endIf
    return i % 0x01000000
EndFunction
</source>
 
Such emulation of bit-wise operations using mathematical operations is a little more complicated when the high bit must be preserved:


<source lang="papyrus">
<source lang="papyrus">
int ex1 = (0x7fffffff % 0x01000000) ; =  0x00ffffff as expected
int Function getHighByteAsLowByte(int i)
int ex2 = (0x80000000 % 0x01000000) ; = 0x00000000 as expected
{Return the high byte of i as a value between 0 and 255 (0x00-0xFF), inclusive.
int ex3 = (0x80000001 % 0x01000000) ; = -0x00ffffff, not expected
  Equivalent to "i >> 24 & 0xFF" in C, Java, etc.}
int ex4 = (0x80000002 % 0x01000000) ; = -0x00fffffe, not expected
    if i < 0
        ; Force i to be a positive number, by changing its high bit (bit 32) from 1 to 0.
        ; Then divide by 0x01000000 (16,777,216), which is now equivalent to a right shift of 24 bits.
        ; Finally, restore the value of the high bit we cleared, now that it is bit 8 and cannot affect the sign.
        return (i + 0x80000000) / 0x01000000 + 0x80
    endIf
    return i / 0x01000000
EndFunction
</source>
</source>


Anonymous user