You are not logged in.

Applications: [GameMaster: OPEN] | [Volunteer Testers: OPEN]


This forum will be permanently shut down on Friday 13.07.2018
Please copy or save all important information from old forum before they will be deactivated
We have moved to new board. https://forum.runesofmagic.gameforge.com/Come join us.

Peryl

Intermediate

  • "Peryl" started this thread

Posts: 313

Location: Elsewhere

  • Send private message

1

Sunday, February 5th 2012, 4:41am

Bitwise Operations in Lua

Here are a few functions to do some quick and dirty bitwise operations in Lua. This is not the fastest code around, nor is it even all that complete. But it should fulfill most needs.

I did not write these functions directly, found the code on the Lua-users wiki and updated it for Lua 5.1.

If you really need a much more complete set of bitwise operations in Lua, I suggest looking at lua-bit-numberlua which is part of the ModuleCompressDeflateLua compression module here (look under lmod/bit/numberlua.lua).




Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
--[[
    Function for bitwise operations in Lua
    
    This code is not the fastest at doing bitwise operations, but it gets the
    job done in pure Lua.
    
    The code assumes that all number variables are 32 bit integers.
    
    Not, and, or, xor code by Roberto Ierusalimschy (http://lua-users.org/lists/lua-l/2002-09/msg00134.html)
    Updated to Lua 5.1 by Peryl
    Logical shift code added by Peryl (can't remember where I got the original code from though)
--]]




--[[ Tables and constants for bitwise operator functions ]]--


local ff = 2^32 - 1
local maxsize = 2^32
local floor = math.floor
local powtbl = { 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536,
                 131072, 262144, 524288, 1048576, 2097152, 4194304, 8388608, 16777216,
                 33554432, 67108864, 134217728, 268435456, 536870912, 1073741824, 2147483648 }
local xortbl = {
        { 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, },
        { 1,  0,  3,  2,  5,  4,  7,  6,  9,  8, 11, 10, 13, 12, 15, 14, },
        { 2,  3,  0,  1,  6,  7,  4,  5, 10, 11,  8,  9, 14, 15, 12, 13, },
        { 3,  2,  1,  0,  7,  6,  5,  4, 11, 10,  9,  8, 15, 14, 13, 12, },
        { 4,  5,  6,  7,  0,  1,  2,  3, 12, 13, 14, 15,  8,  9, 10, 11, },
        { 5,  4,  7,  6,  1,  0,  3,  2, 13, 12, 15, 14,  9,  8, 11, 10, },
        { 6,  7,  4,  5,  2,  3,  0,  1, 14, 15, 12, 13, 10, 11,  8,  9, },
        { 7,  6,  5,  4,  3,  2,  1,  0, 15, 14, 13, 12, 11, 10,  9,  8, },
        { 8,  9, 10, 11, 12, 13, 14, 15,  0,  1,  2,  3,  4,  5,  6,  7, },
        { 9,  8, 11, 10, 13, 12, 15, 14,  1,  0,  3,  2,  5,  4,  7,  6, },
        {10, 11,  8,  9, 14, 15, 12, 13,  2,  3,  0,  1,  6,  7,  4,  5, },
        {11, 10,  9,  8, 15, 14, 13, 12,  3,  2,  1,  0,  7,  6,  5,  4, },
        {12, 13, 14, 15,  8,  9, 10, 11,  4,  5,  6,  7,  0,  1,  2,  3, },
        {13, 12, 15, 14,  9,  8, 11, 10,  5,  4,  7,  6,  1,  0,  3,  2, },
        {14, 15, 12, 13, 10, 11,  8,  9,  6,  7,  4,  5,  2,  3,  0,  1, },
        {15, 14, 13, 12, 11, 10,  9,  8,  7,  6,  5,  4,  3,  2,  1,  0, },
    }




--[[ Perform a bitwise Exclusive OR (XOR) ]]--


function bxor(a,b)
    local res, mult = 0, 1
    while a > 0 and b > 0 do
        local a2, b2 = a % 16, b % 16
        res = res + xortbl[a2+1][b2+1] * mult
        a = (a-a2) / 16
        b = (b-b2) / 16
        mult = mult * 16
    end
    res = res + (a+b) * mult
    return res
end




--[[ Perform a bitwise negation (NOT) ]]--


function bnot(a) 
  return ff - a 
end




--[[ Perform a bitwise AND ]]--


function band(a,b)
    return ((a+b) - bxor(a,b))/2
end




--[[ Perform a bitwise OR ]]--


function bor(a,b)
    return ff - band(ff - a, ff - b)
end




--[[ Perform a logical shift left ]]--


function lsl(a,n)
    if n < 1 then
        return a
    else
        return (n > 31) and 0 or (a * powtbl[n]) % maxsize
    end
end




--[[ Perform a logical shift right ]]--


function lsr(a,n)
    if n < 1 then
        return a    -- 0 shifting does nothing, negative shifts are ignored
    else
        return (n > 31) and 0 or (floor(a / powtbl[n]) % maxsize)
    end
end
2013... The year from hell....