diff options
| author | jussi | 2024-05-01 15:21:28 +0300 |
|---|---|---|
| committer | jussi | 2024-05-01 15:21:28 +0300 |
| commit | 4452bccfa63cf86c134aa616ee0bebcc66beca03 (patch) | |
| tree | a40befeb0393fd5599240ccb85a4500781d78c17 /src/bitwiseOp.c | |
| parent | bdd660be01f3742befe15dff26929a77eeefe61d (diff) | |
| download | reilua-enhanced-4452bccfa63cf86c134aa616ee0bebcc66beca03.tar.gz reilua-enhanced-4452bccfa63cf86c134aa616ee0bebcc66beca03.tar.bz2 reilua-enhanced-4452bccfa63cf86c134aa616ee0bebcc66beca03.zip | |
Bitwise operations for cross Lua compatibility.
Diffstat (limited to 'src/bitwiseOp.c')
| -rw-r--r-- | src/bitwiseOp.c | 153 |
1 files changed, 153 insertions, 0 deletions
diff --git a/src/bitwiseOp.c b/src/bitwiseOp.c new file mode 100644 index 0000000..1e1aeb1 --- /dev/null +++ b/src/bitwiseOp.c @@ -0,0 +1,153 @@ +#include "main.h" +#include "state.h" +#include "lua_core.h" +#include "core.h" +#include "bitwiseOp.h" + +/* +## Bitwise Operations - Arithmetic +*/ + +/* +> result = RL.BitAnd( int a, int b ) + +Equivalent to a & b in C + +- Success return int +*/ +int lbitAnd( lua_State* L ) { + int a = luaL_checkinteger( L, 1 ); + int b = luaL_checkinteger( L, 2 ); + + lua_pushinteger( L, a & b ); + + return 1; +} + +/* +> result = RL.BitOr( int a, int b ) + +Equivalent to a | b in C + +- Success return int +*/ +int lbitOr( lua_State* L ) { + int a = luaL_checkinteger( L, 1 ); + int b = luaL_checkinteger( L, 2 ); + + lua_pushinteger( L, a | b ); + + return 1; +} + +/* +> result = RL.BitXor( int a, int b ) + +Equivalent to a ^ b in C + +- Success return int +*/ +int lbitXor( lua_State* L ) { + int a = luaL_checkinteger( L, 1 ); + int b = luaL_checkinteger( L, 2 ); + + lua_pushinteger( L, a ^ b ); + + return 1; +} + +/* +> result = RL.BitNot( int v ) + +Equivalent to ~v in C + +- Success return int +*/ +int lbitNot( lua_State* L ) { + int v = luaL_checkinteger( L, 1 ); + + lua_pushinteger( L, ~v ); + + return 1; +} + +/* +> result = RL.BitShiftLeft( int v, int n ) + +Equivalent to v << n in C + +- Success return int +*/ +int lbitShiftLeft( lua_State* L ) { + int v = luaL_checkinteger( L, 1 ); + int n = luaL_checkinteger( L, 2 ); + + lua_pushinteger( L, v << n ); + + return 1; +} + +/* +> result = RL.BitShiftRight( int v, int n ) + +Equivalent to v >> n in C + +- Success return int +*/ +int lbitShiftRight( lua_State* L ) { + int v = luaL_checkinteger( L, 1 ); + int n = luaL_checkinteger( L, 2 ); + + lua_pushinteger( L, v >> n ); + + return 1; +} + +/* +> result = RL.BitSet( int v, int i, bool b ) + +Set bit in index i to state b in value v + +- Success return int +*/ +int lbitSet( lua_State* L ) { + int v = luaL_checkinteger( L, 1 ); + int i = luaL_checkinteger( L, 2 ); + bool b = uluaGetBoolean( L, 3 ); + + lua_pushinteger( L, b ? v | 1 << i : v & ~( 1 << i ) ); + + return 1; +} + +/* +> bit = RL.BitGet( int v, int i ) + +Get bit in index i from value v + +- Success return bool +*/ +int lbitGet( lua_State* L ) { + int v = luaL_checkinteger( L, 1 ); + int i = luaL_checkinteger( L, 2 ); + + lua_pushboolean( L, 0 < ( v & ( 1 << i ) ) ); + + return 1; +} + +/* +> result = RL.BitToggle( int v, int i ) + +Toggle bit in index i in value v + +- Success return int +*/ +int lbitToggle( lua_State* L ) { + int v = luaL_checkinteger( L, 1 ); + int i = luaL_checkinteger( L, 2 ); + + lua_pushinteger( L, v ^ 1 << i ); + + return 1; +} |
