Difference between revisions of "Bitwise Operators"
From GiderosMobile
(Created page with "__NOTOC__ '''Supported platforms:''' <br/> '''Available since:''' Gideros 2017.10<br/> === Description === <br /> Bitwise operators to manipulate numbers.<br /><br /> a<b> |...") |
m (Text replacement - "</source>" to "</syntaxhighlight>") |
||
(17 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
__NOTOC__ | __NOTOC__ | ||
− | '''Supported platforms:''' <br/> | + | '''Supported platforms:''' [[File:Platform android.png]][[File:Platform ios.png]][[File:Platform mac.png]][[File:Platform pc.png]]<br/> |
'''Available since:''' Gideros 2017.10<br/> | '''Available since:''' Gideros 2017.10<br/> | ||
+ | |||
=== Description === | === Description === | ||
− | <br /> | + | Bitwise operators to manipulate numbers. |
− | Bitwise | + | |
+ | a''' | '''b     Bitwise 'a OR b'.<br/> | ||
+ | a''' & '''b     Bitwise 'a AND b'.<br/> | ||
+ | a''' ~ '''b     Bitwise 'a XOR b'.<br/> | ||
+ | a''' << '''b   Bitwise shift 'a' by 'b' left.<br/> | ||
+ | a''' >> '''b   Bitwise shift 'a' by 'b' right.<br/> | ||
+ | ''' ~ '''a     Bitwise 'NOT a'.<br/> | ||
+ | |||
+ | === Examples === | ||
+ | '''Set joypad switches''' | ||
+ | <syntaxhighlight lang="lua"> | ||
+ | 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 | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | '''Clear joypad switches''' | ||
+ | <syntaxhighlight lang="lua"> | ||
+ | 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 | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | '''Move in a direction based on joystick switches (using above examples)''' | ||
+ | <syntaxhighlight lang="lua"> | ||
+ | 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] | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | '''Debounce bits, eg joypad switches''' | ||
+ | <syntaxhighlight lang="lua"> | ||
+ | debouncedJoypad=(joypad~joypadOld)&joypad | ||
+ | joypadOld=joypad | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | === See also === | ||
+ | '''https://www.lua.org/pil/3.3.html''' | ||
− | + | {{GIDEROS IMPORTANT LINKS}} | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | { | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− |
Latest revision as of 14:27, 13 July 2023
Supported platforms:
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
debouncedJoypad=(joypad~joypadOld)&joypad
joypadOld=joypad
See also
https://www.lua.org/pil/3.3.html