Difference between revisions of "Bitwise Operators"

From GiderosMobile
(removed language stuff)
Line 1: Line 1:
 
__NOTOC__
 
__NOTOC__
<languages />
 
 
<!-- GIDEROSOBJ:Bitwise Operators -->
 
<!-- GIDEROSOBJ:Bitwise Operators -->
'''<translate>Supported platforms</translate>:''' [[File:Platform android.png]][[File:Platform ios.png]][[File:Platform mac.png]][[File:Platform pc.png]]<br/>
+
'''Supported platforms:''' [[File:Platform android.png]][[File:Platform ios.png]][[File:Platform mac.png]][[File:Platform pc.png]]<br/>
'''<translate>Available since</translate>:''' Gideros 2017.10<br/>
+
'''Available since:''' Gideros 2017.10<br/>
=== <translate>Description</translate> ===
 
<translate><br />
 
Bitwise operators to manipulate numbers.<br /><br />
 
  
a<b> | </b>b &emsp; Bitwise 'a OR b'.<br />
+
=== Description ===
a<b> & </b>b &emsp; Bitwise 'a AND b'.<br />
+
Bitwise operators to manipulate numbers.
a<b> ~ </b>b &emsp; Bitwise 'a XOR b'.<br />
+
 
a<b> &lt;&lt; </b>b &emsp; Bitwise shift 'a' by 'b' left.<br />
+
a''' | '''b Bitwise 'a OR b'.<br/>
a<b> &gt;&gt; </b>b &emsp; Bitwise shift 'a' by 'b' right.<br />
+
a''' & '''b Bitwise 'a AND b'.<br/>
<b> ~ </b>a &emsp; Bitwise 'NOT a'.<br /></translate>
+
a''' ~ '''b Bitwise 'a XOR b'.<br/>
=== <translate>Examples</translate> ===
+
a''' &lt;&lt; '''b Bitwise shift 'a' by 'b' left.<br/>
'''Set joypad switches'''<br/>
+
a''' &gt;&gt; '''b Bitwise shift 'a' by 'b' right.<br/>
<source lang="lua">  
+
''' ~ '''a Bitwise 'NOT a'.<br/>
 +
 
 +
=== Examples ===
 +
'''Set joypad switches'''
 +
<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
 
elseif k==KeyCode.LEFT then joypad=joypad|0b000010
 
elseif k==KeyCode.LEFT then joypad=joypad|0b000010
 
elseif k==KeyCode.RIGHT then joypad=joypad|0b000001
 
elseif k==KeyCode.RIGHT then joypad=joypad|0b000001
end</source>
+
end
'''Clear joypad switches'''<br/>
+
</source>
<source lang="lua">  
+
 
 +
'''Clear joypad switches'''
 +
<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
 
elseif k==KeyCode.LEFT then joypad=joypadRGUDLR&0b1101
 
elseif k==KeyCode.LEFT then joypad=joypadRGUDLR&0b1101
 
elseif k==KeyCode.RIGHT then joypad=joypadRGUDLR&0b1110
 
elseif k==KeyCode.RIGHT then joypad=joypadRGUDLR&0b1110
end</source>
+
end
'''Move in a direction based on joystick switches (using above examples)'''<br/>
+
</source>
<source lang="lua">  
+
 
 +
'''Move in a direction based on joystick switches (using above examples)'''
 +
<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}
 
x=x+deltaX[joypad+1]
 
x=x+deltaX[joypad+1]
y=y+deltaY[joypad+1]</source>
+
y=y+deltaY[joypad+1]
'''Debounce bits, eg joypad switches'''<br/>
+
</source>
<source lang="lua">  
+
 
 +
'''Debounce bits, eg joypad switches'''
 +
<source lang="lua">
 
joypad=(joypad~joypadOld)&joypad
 
joypad=(joypad~joypadOld)&joypad
joypadOld=joypad</source>
+
joypadOld=joypad
 +
</source>
 +
 
 +
 
 +
'''See also: https://www.lua.org/pil/3.3.html'''
 +
 
 
{|-
 
{|-
 
| style="width: 50%; vertical-align:top;"|
 
| style="width: 50%; vertical-align:top;"|
=== <translate>Methods</translate> ===
+
=== Methods ===
 
| style="width: 50%; vertical-align:top;"|
 
| style="width: 50%; vertical-align:top;"|
=== <translate>Events</translate> ===
+
=== Events ===
=== <translate>Constants</translate> ===
+
=== Constants ===
 
|}
 
|}
  
 
{{GIDEROS IMPORTANT LINKS}}
 
{{GIDEROS IMPORTANT LINKS}}

Revision as of 14:34, 31 May 2021

Supported platforms: Platform android.pngPlatform ios.pngPlatform mac.pngPlatform pc.png
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


See also: https://www.lua.org/pil/3.3.html

Methods

Events

Constants