Gamevar operators: Difference between revisions

From EDukeWiki
Jump to navigation Jump to search
No edit summary
m Formatting and make it more apparent that the 'var' affix is no longer needed
 
(13 intermediate revisions by 4 users not shown)
Line 1: Line 1:
<'''operator'''>'''var''' <gamevar> <value><br>
<'''operator'''>'''varvar''' <gamevar> <value>


Gamevar operators are commands that perform various mathematical operations on [[gamevar]]s.
<span {{code}}> <'''operator'''> <gamevar> (<constant>|<gamevar>)</span>


For example:
<span {{code}}> <'''operator'''>'''var''' <gamevar> <constant> </span> ''(Deprecated)''


setvar temp 1 // assigns a value of 1 to the gamevar ''temp''.
<span {{code}}> <'''operator'''>'''varvar''' <gamevar> <gamevar></span> ''(Deprecated)''
 
 
eduke32 CON supports a range of binary operators that perform various arithmetic and bitwise logical operations on variables, here denoted as [[gamevar]]s.
 
The syntax is similar to Intel x86 assembly, where the first argument is the destination as well as the first operand (2-address code).
 
In older versions of eduke32 (and in vanilla Duke Nukem 3D), it was necessary to distinguish between '''var''', and '''varvar''' binary operations. The difference between them is that the singular '''var''' operations expect a gamevar for the first, and a constant value (or label) for the second argument, while the '''varvar''' operations expect gamevars for both arguments.
 
This distinction is no longer required, and one can now write the operator without any '''var''' appendix, and use either constant or variable as the second argument.  
 
==== Example ====
 
var temp 0 0  // initialize a global variable with initial value 0
   
   
  addvar temp 1 // adds 1 to ''temp'', causing its value to become 2.
  set temp 1   // assigns a value of 1 to the gamevar ''temp''.
   
   
  mulvar temp 2 // multiplies ''temp'' by 2, causing its value to become 4.
  add temp 1    // adds 1 to ''temp'', causing its value to become 2.
mul temp 2   // multiplies ''temp'' by 2, causing its value to become 4.
 
== Operators ==


{| {{prettytable}}
{| {{prettytable}}
Line 16: Line 31:
|-
|-
|set
|set
|=
|<code>=</code>
|=
|=
|assignment
|assignment
|-
|-
|add
|add
|<nowiki>+</nowiki>
|<code><nowiki>+</nowiki></code>
|<nowiki>+</nowiki>
|<nowiki>+</nowiki>
|addition
|addition
Line 27: Line 42:
|-
|-
|sub
|sub
|<nowiki>-</nowiki>
|<code><nowiki>-</nowiki></code>
|<nowiki>&ndash;</nowiki>
|<nowiki>&ndash;</nowiki>
|subtraction
|subtraction
Line 33: Line 48:
|-
|-
|mul
|mul
|*
|<code>*</code>
|&times;
|&times;
|multiplication
|multiplication
Line 39: Line 54:
|-
|-
|div
|div
|/
|<code>/</code>
|&#247;
|&#247;
|division
|division with rounding toward 0 for nonnegative dividends
|Ensure that <value> does not equal 0 at any time.
|Ensure that <value> does not equal 0 at any time. '''The rounding behavior is undefined for negative dividends.'''
|-
|-
|mod
|mod
|%
|<code>%</code>
|
|
|[[Wikipedia:Modulo operation|modulo]], remainder
|[[Wikipedia:Modulo operation|modulo]], remainder
|Ensure that <value> does not equal 0 at any time.
|Ensure that <value> does not equal 0 at any time; sign of the result is that of left hand side operand (dividend).
|-
|-
|and
|and
|&
|<code>&</code>
|  
|  
|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.
|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]]): 01001010 AND 11110100 is 01000000
|-
|-
|or
|or
|<nowiki>|</nowiki>
|<code><nowiki>|</nowiki></code>
|  
|  
|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.
|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
|xor
|^
|<code>^</code>
|  
|  
|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.
|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
|}
|}
{{varsuffix}}


[[Category:Gamevar manipulation]]
[[Category:Gamevar manipulation]]

Latest revision as of 01:59, 10 January 2021

<operator> <gamevar> (<constant>|<gamevar>)

<operator>var <gamevar> <constant> (Deprecated)

<operator>varvar <gamevar> <gamevar> (Deprecated)


eduke32 CON supports a range of binary operators that perform various arithmetic and bitwise logical operations on variables, here denoted as gamevars.

The syntax is similar to Intel x86 assembly, where the first argument is the destination as well as the first operand (2-address code).

In older versions of eduke32 (and in vanilla Duke Nukem 3D), it was necessary to distinguish between var, and varvar binary operations. The difference between them is that the singular var operations expect a gamevar for the first, and a constant value (or label) for the second argument, while the varvar operations expect gamevars for both arguments.

This distinction is no longer required, and one can now write the operator without any var appendix, and use either constant or variable as the second argument.

Example

var temp 0 0  // initialize a global variable with initial value 0

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

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

mul temp 2    // multiplies temp by 2, causing its value to become 4.

Operators

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