# [Lua] Bitwise Functions

Discussion in 'Mods & Lua Functions' started by Forrest, Dec 22, 2016.

1. ### ForrestBanned User

Joined:
Dec 21, 2016
Messages:
57
12
0
Left Shift
Code (Text):
1.
2. function lshift(x, y)
3.   return x * 2 ^ y
4. end
5.
Example:
Code (Text):
1.
2. print( lshift(1, 11) ) -- prints 2048
3.
Right Shift
Code (Text):
1.
2. function rshift(x, y)
3.   return math.floor(x / 2 ^ y)
4. end
5.
Example:
Code (Text):
1.
2. print( rshift(11, 1) ) -- prints 5
3.
Number To Binary
Code (Text):
1.
2. function getBits(num, asString)
3.     local x = {}
4.     while num > 0 do
5.         rest = num % 2
6.         table.insert(x, 1, rest)
7.         num = (num - rest) / 2
8.     end
9.     return asString and table.concat(x) or x
10. end
11.
Example:
Code (Text):
1.
2. print( getBits(67, true) ) -- prints 1000011
3.
Binary to Number
Code (Text):
1.
2. function binToNum(binary)
3.     local bin, sum = tostring(binary):reverse(), 0
4.     for i = 1, #bin do
5.         num = bin:sub(i, i) == "1" and 1 or 0
6.         sum = sum + num * math.pow(2, i - 1)
7.     end
8.     return sum
9. end
10.
Example:
Code (Text):
1.
2. print( binToNum(1000011) ) -- prints 67
3.
Bitwise Operations
Code (Text):
1.
2. function bitwise(bits, c, oper)
3.     local t = {}
4.     for k, v in pairs(bits) do
5.         if oper == "and" then
6.             if (v == 1 and c[k] == 1) then
7.                 table.insert(t, 1)
8.             else
9.                 table.insert(t, 0)
10.             end
11.         elseif oper == "or" then
12.             if (v == 1 or c[k] == 1) then
13.                 table.insert(t, 1)
14.             else
15.                 table.insert(t, 0)
16.             end
17.         elseif oper == "xor" then
18.            if ((v == 1 and c[k] ~= 1) or ( c[k] == 1 and v ~= 1) ) then
19.                 table.insert(t, 1)
20.             else
21.                 table.insert(t, 0)
22.             end
23.         elseif oper == "not" then
24.             table.insert(t, (v == 0 and 1 or 0))
25.         else
26.             -- just a default for testing
27.             table.insert(t, v)
28.         end
29.     end
30.     return table.concat(t)
31. end
32.
Example:
Code (Text):
1.
2. local binary = bitwise(getBits(48), getBits(5), "xor")
3. print(binary, binToNum(binary))
4. -- prints 011000    24
5.

Joe Rod and Tarek1337 like this.
2. ### Static_Support TeamSupport Team

Joined:
Feb 14, 2015
Messages:
3,725
1,648
274
some of these already exist :|
Code (Text):
1.
2.   [rol] => function: builtin#69
3.   [rshift] => function: builtin#67
4.   [ror] => function: builtin#70
5.   [bswap] => function: builtin#65
6.   [bxor] => function: builtin#73
7.   [bor] => function: builtin#72
8.   [arshift] => function: builtin#68
9.   [bnot] => function: builtin#64
10.   [tobit] => function: builtin#63
11.   [lshift] => function: builtin#66
12.   [tohex] => function: builtin#74
13.   [band] => function: builtin#71

Joe Rod, Tarek1337 and cbrm like this.
3. ### ForrestBanned User

Joined:
Dec 21, 2016
Messages:
57
12
0
Thanks for the reply but I tried using the bitwise functions in 1.2 and they weren't working for me.
The functions in this thread are not strictly written for TFS they should work with any lua script.

Edit: @Xeraphus can you explain how the built in functions you listed work? It really isn't helpful at all to just say well some of these functions exist already and not give any information beyond that.

That is like me going into threads you either created or are helping in and say oh that code you wrote it has been done before but not giving any more information.

Last edited: Dec 22, 2016
Tarek1337 likes this.
4. ### tokenzz:thinking:

Joined:
Feb 2, 2013
Messages:
773
340
2
who needs a converter from binary to num when you have tonumber?
e.g
Code (Text):
1.
2. tonumber("1001100", 2)
would print 76 which is right

another example

Code (Text):
1.
2. tonumber("0x539", 16)
3.
would print 1337

Joe Rod and Tarek1337 like this.
5. ### ForrestBanned User

Joined:
Dec 21, 2016
Messages:
57
12
0
I do =)

You would be suprised what functions are missing from selective versions/variations of lua.

Tarek1337 likes this.
6. ### tokenzz:thinking:

Joined:
Feb 2, 2013
Messages:
773
340
2
of course you do codex. I am always surprised of your arrogance

Tarek1337 likes this.
7. ### ForrestBanned User

Joined:
Dec 21, 2016
Messages:
57
12
0
I am not codex but nice try, close but no cigar

Tarek1337 likes this.
8. ### Static_Support TeamSupport Team

Joined:
Feb 14, 2015
Messages:
3,725
1,648
274
never claimed to know how they work i just said they exist already
if you wanted bitwise functions for lua in general just go to lua 5.3
https://www.lua.org/manual/5.3/manual.html#3.4.2

9. ### ForrestBanned User

Joined:
Dec 21, 2016
Messages:
57
12
0
Again I stress, how about your interpretation; not just a diagram or link to a site. Your level of competence on this language is limited and thats ok but please don't act like you are all knowing and rudly come into another persons thread to give your 1 or 2 line fruitless opinion or a condescending remark and this goes for you as well @tokenzz you are not enlightening anyone by acting in a similar manner.

10. ### Static_Support TeamSupport Team

Joined:
Feb 14, 2015
Messages:
3,725
1,648
274
when did i ever act like i was all knowing
me telling you the functions you created already exist isn't an opinion, it's a fact
don't have to snap at me just because you didn't know they existed already ;\

StreamSide likes this.
11. ### ForrestBanned User

Joined:
Dec 21, 2016
Messages:
57
12
0
I do know they already existed and if you can't determine how you behave on an open forum then maybe you shouldn't be on one to begin with.

12. ### Static_Support TeamSupport Team

Joined:
Feb 14, 2015
Messages:
3,725
1,648
274
ill leave
sorry boss

Tarek1337 and tokenzz like this.

Joined:
Feb 2, 2013
Messages:
773