Difference between revisions of "Bitwise Operators"

From GiderosMobile
Line 3: Line 3:
 
'''Available since:''' Gideros 2017.10<br/>
 
'''Available since:''' Gideros 2017.10<br/>
 
=== Description ===
 
=== Description ===
<br />
+
<translate><br />
 
Bitwise operators to manipulate numbers.<br /><br />
 
Bitwise operators to manipulate numbers.<br /><br />
  
Line 11: Line 11:
 
a<b> &lt;&lt; </b>b &emsp; Bitwise shift 'a' by 'b' left.<br />
 
a<b> &lt;&lt; </b>b &emsp; Bitwise shift 'a' by 'b' left.<br />
 
a<b> &gt;&gt; </b>b &emsp; Bitwise shift 'a' by 'b' right.<br />
 
a<b> &gt;&gt; </b>b &emsp; Bitwise shift 'a' by 'b' right.<br />
<b> ~ </b>a &emsp; Bitwise 'NOT a'.<br />
+
<b> ~ </b>a &emsp; Bitwise 'NOT a'.<br /></translate>
 
=== Examples ===
 
=== Examples ===
 
'''Set joypad switches'''<br/>
 
'''Set joypad switches'''<br/>
<source lang="lua"><br />  
+
<source lang="lua">  
 
if k==KeyCode.UP then joypad=joypad|0b001000
 
if k==KeyCode.UP then joypad=joypad|0b001000
 
elseif k==KeyCode.DOWN then joypad=joypad|0b000100
 
elseif k==KeyCode.DOWN then joypad=joypad|0b000100
Line 21: Line 21:
 
end</source>
 
end</source>
 
'''Clear joypad switches'''<br/>
 
'''Clear joypad switches'''<br/>
<source lang="lua"><br />  
+
<source lang="lua">  
 
if k==KeyCode.UP then joypad=joypad&0b0111
 
if k==KeyCode.UP then joypad=joypad&0b0111
 
elseif k==KeyCode.DOWN then joypad=joypadRGUDLR&0b1011
 
elseif k==KeyCode.DOWN then joypad=joypadRGUDLR&0b1011
Line 28: Line 28:
 
end</source>
 
end</source>
 
'''Move in a direction based on joystick switches (using above examples)'''<br/>
 
'''Move in a direction based on joystick switches (using above examples)'''<br/>
<source lang="lua"><br />  
+
<source lang="lua">  
 
local deltaX={0,-1,1,0,0,-1,1,0,0,-1,1,0,0,-1,1,0}
 
local deltaX={0,-1,1,0,0,-1,1,0,0,-1,1,0,0,-1,1,0}
 
local deltaY={0,0,0,0,1,1,1,1,-1,-1,-1,-1,0,0,0,0}
 
local deltaY={0,0,0,0,1,1,1,1,-1,-1,-1,-1,0,0,0,0}
Line 34: Line 34:
 
y=y+deltaY[joypad+1]</source>
 
y=y+deltaY[joypad+1]</source>
 
'''Debounce bits, eg joypad switches'''<br/>
 
'''Debounce bits, eg joypad switches'''<br/>
<source lang="lua"><br />  
+
<source lang="lua">  
 
joypad=(joypad~joypadOld)&joypad
 
joypad=(joypad~joypadOld)&joypad
 
joypadOld=joypad</source>
 
joypadOld=joypad</source>

Revision as of 13:34, 23 August 2018

Supported platforms: android, ios, mac, pc
Available since: Gideros 2017.10

Description


Bitwise operators to manipulate numbers.

a | b   Bitwise 'a OR b'.
a & b   Bitwise 'a AND b'.
a ~ b   Bitwise 'a XOR b'.
a << b   Bitwise shift 'a' by 'b' left.
a >> b   Bitwise shift 'a' by 'b' right.
~ a   Bitwise 'NOT a'.

Examples

Set joypad switches

 
if k==KeyCode.UP then joypad=joypad|0b001000
elseif k==KeyCode.DOWN then joypad=joypad|0b000100
elseif k==KeyCode.LEFT then joypad=joypad|0b000010
elseif k==KeyCode.RIGHT then joypad=joypad|0b000001
end

Clear joypad switches

 
if k==KeyCode.UP then joypad=joypad&0b0111
elseif k==KeyCode.DOWN then joypad=joypadRGUDLR&0b1011
elseif k==KeyCode.LEFT then joypad=joypadRGUDLR&0b1101
elseif k==KeyCode.RIGHT then joypad=joypadRGUDLR&0b1110
end

Move in a direction based on joystick switches (using above examples)

 
local deltaX={0,-1,1,0,0,-1,1,0,0,-1,1,0,0,-1,1,0}
local deltaY={0,0,0,0,1,1,1,1,-1,-1,-1,-1,0,0,0,0}
x=x+deltaX[joypad+1]
y=y+deltaY[joypad+1]

Debounce bits, eg joypad switches

 
joypad=(joypad~joypadOld)&joypad
joypadOld=joypad

Methods

Events

Constants