Difference between revisions of "Gamevar operators"

From EDukeWiki
Jump to navigation Jump to search
(specify mod behavior with negative arguments, see further discussion on the discussion page.)
Line 55: Line 55:
 
|[[Wikipedia:Bitwise operation|bitwise]] AND
 
|[[Wikipedia:Bitwise operation|bitwise]] AND
 
|The bits in the result are set only if they were set in both of the values being operated on. Ex. (all numbers are [[Wikipedia:Binary numeral system|base-2]]):<br/>
 
|The bits in the result are set only if they were set in both of the values being operated on. Ex. (all numbers are [[Wikipedia:Binary numeral system|base-2]]):<br/>
01001010 AND<br/>
+
01001010 AND 11110100 is 01000000
11110100 is<br/>
 
01000000
 
 
|-
 
|-
 
|or
 
|or
Line 64: Line 62:
 
|[[Wikipedia:Bitwise operation|bitwise]] OR
 
|[[Wikipedia:Bitwise operation|bitwise]] OR
 
|The bits in the result are set only if they were set in either of the values being operated on. Ex.:<br/>
 
|The bits in the result are set only if they were set in either of the values being operated on. Ex.:<br/>
01001010 OR<br/>
+
01001010 OR 11110100 is 11111110
11110100 is<br/>
 
11111110
 
 
|-
 
|-
 
|xor
 
|xor
Line 73: Line 69:
 
|[[Wikipedia:Bitwise operation|bitwise]] XOR
 
|[[Wikipedia:Bitwise operation|bitwise]] XOR
 
|The bits in the result are set if only one of the values being operated on has those bits set. Ex.:<br/>
 
|The bits in the result are set if only one of the values being operated on has those bits set. Ex.:<br/>
01001010 XOR<br/>
+
01001010 XOR 11110100 is 10111110
11110100 is<br/>
 
10111110
 
 
|}
 
|}
  

Revision as of 13:20, 8 March 2015

<operator>var <gamevar> <value>
<operator>varvar <gamevar> <value>

Gamevar operators are commands that perform various mathematical and bitwise logical operations on gamevars. The first argument must be a writable gamevar and is taken as the destination as well and the first operand (2-address code).

For example:

setvar temp 1 // assigns a value of 1 to the gamevar temp.

addvar temp 1 // adds 1 to temp, causing its value to become 2.

mulvar temp 2 // multiplies temp by 2, causing its value to become 4.
Operator C/C++ Math Description Notes
set = = assignment
add + + addition
sub - subtraction
mul * × multiplication
div / ÷ division with rounding toward 0 for nonnegative dividends Ensure that <value> does not equal 0 at any time. The rounding behavior is undefined for negative dividends.
mod % modulo, remainder Ensure that <value> does not equal 0 at any time; sign of the result is that of left hand side operand (dividend).
and & bitwise AND The bits in the result are set only if they were set in both of the values being operated on. Ex. (all numbers are base-2):

01001010 AND 11110100 is 01000000

or | bitwise OR The bits in the result are set only if they were set in either of the values being operated on. Ex.:

01001010 OR 11110100 is 11111110

xor ^ bitwise XOR The bits in the result are set if only one of the values being operated on has those bits set. Ex.:

01001010 XOR 11110100 is 10111110

Commands with an additional "var" suffix take gamevars rather than constants or defined labels for their inputs. As an alternate short form, "varvar" can be dropped from these commands; for example ife serves an an alias for ifvarvare.