|
76 | 76 | local UINT32_MAX = 0xFFFFFFFF;-- 32 bit word |
77 | 77 | local WORD_MASK = UINT32_MAX; |
78 | 78 | local function bwAnd(int1, int2) --TODO: enforce uint32 params! |
79 | | - if lua_version_int < 53 then |
80 | | - return bit32.band(int1, int2); |
81 | | - else |
82 | | - return int1.__band(int2); |
83 | | - end |
| 79 | + assert(int1 and type(int1) == "number" and math.floor(int1) == int1, "Expecting integer"); |
| 80 | + assert(int2 and type(int2) == "number" and math.floor(int2) == int2, "Expecting integer"); |
| 81 | + return bit32.band(int1, int2); |
84 | 82 | end |
85 | 83 |
|
86 | 84 | local function bwLshift(int1, int2) |
87 | | - if lua_version_int < 53 then |
88 | | - return int1 * math.pow(2,int2); |
89 | | - --return bit32.lshift(int1, int2); |
90 | | - else |
91 | | - return int1:__shl(int2); |
92 | | - end |
| 85 | + assert(int1 and type(int1) == "number" and math.floor(int1) == int1, "Expecting integer"); |
| 86 | + assert(int2 and type(int2) == "number" and math.floor(int2) == int2, "Expecting integer"); |
| 87 | + return int1 * math.pow(2,int2); |
93 | 88 | end |
94 | 89 |
|
95 | 90 | local function bwRshift(int1, int2) |
96 | | - if lua_version_int < 53 then |
97 | | - return bit32.rshift(int1, int2); |
98 | | - else |
99 | | - return int1.__shr(int2); |
100 | | - end |
| 91 | + assert(int1 and type(int1) == "number" and math.floor(int1) == int1, "Expecting integer"); |
| 92 | + assert(int2 and type(int2) == "number" and math.floor(int2) == int2, "Expecting integer"); |
| 93 | + return bit32.rshift(int1, int2); |
101 | 94 | end |
102 | 95 |
|
103 | 96 | local function bwOr(int1, int2) |
104 | | - if lua_version_int < 53 then |
105 | | - return bit32.bor(int1, int2); |
106 | | - else |
107 | | - return int1.__bor(int2); |
108 | | - end |
| 97 | + assert(int1 and type(int1) == "number" and math.floor(int1) == int1, "Expecting integer"); |
| 98 | + assert(int2 and type(int2) == "number" and math.floor(int2) == int2, "Expecting integer"); |
| 99 | + return bit32.bor(int1, int2); |
109 | 100 | end |
110 | 101 |
|
111 | 102 | local function bwXor(int1, int2) |
112 | | - if lua_version_int < 53 then |
113 | | - return bit32.bxor(int1, int2); |
114 | | - else |
115 | | - return int1.__bxor(int2); |
116 | | - end |
| 103 | + assert(int1 and type(int1) == "number" and math.floor(int1) == int1, "Expecting integer"); |
| 104 | + assert(int2 and type(int2) == "number" and math.floor(int2) == int2, "Expecting integer"); |
| 105 | + return bit32.bxor(int1, int2); |
117 | 106 | end |
118 | 107 |
|
119 | | -local function bwNot(int1, int2) |
120 | | - if lua_version_int < 53 then |
121 | | - return bit32.bnot(int1, int2); |
122 | | - else |
123 | | - return int1.__bnot(int2); |
124 | | - end |
| 108 | +local function bwNot(int1) |
| 109 | + assert(int1 and type(int1) == "number" and math.floor(int1) == int1, "Expecting unsigned"); |
| 110 | + return bit32.bnot(int1); |
125 | 111 | end |
126 | 112 |
|
127 | 113 |
|
|
0 commit comments