|
| 1 | +# core.base.bit |
| 2 | + |
| 3 | +This module provides bitwise operations for Lua scripts in Xmake. |
| 4 | + |
| 5 | +::: tip TIP |
| 6 | +To use this module, you need to import it first: `import("core.base.bit")` |
| 7 | +::: |
| 8 | + |
| 9 | +## Overview |
| 10 | + |
| 11 | +Although Lua 5.4+ natively supports bitwise operators, Xmake can be configured to use LuaJIT as the runtime. To ensure compatibility across different Lua versions, Xmake provides the `bit` module for more generic bitwise operations. |
| 12 | + |
| 13 | +This module provides a common set of bitwise operations that work consistently regardless of whether you're using standard Lua or LuaJIT. |
| 14 | + |
| 15 | +## bit.band |
| 16 | + |
| 17 | +- Bitwise AND operation |
| 18 | + |
| 19 | +#### Function Prototype |
| 20 | + |
| 21 | +::: tip API |
| 22 | +```lua |
| 23 | +bit.band(a: <number>, b: <number>) |
| 24 | +``` |
| 25 | +::: |
| 26 | + |
| 27 | +#### Parameter Description |
| 28 | + |
| 29 | +| Parameter | Description | |
| 30 | +|-----------|-------------| |
| 31 | +| a | Required. First number | |
| 32 | +| b | Required. Second number | |
| 33 | + |
| 34 | +#### Return Value |
| 35 | + |
| 36 | +| Type | Description | |
| 37 | +|------|-------------| |
| 38 | +| number | Returns the bitwise AND result of a and b | |
| 39 | + |
| 40 | +#### Usage |
| 41 | + |
| 42 | +```lua |
| 43 | +import("core.base.bit") |
| 44 | + |
| 45 | +local result = bit.band(5, 3) -- Returns 1 |
| 46 | +``` |
| 47 | + |
| 48 | +## bit.bor |
| 49 | + |
| 50 | +- Bitwise OR operation |
| 51 | + |
| 52 | +#### Function Prototype |
| 53 | + |
| 54 | +::: tip API |
| 55 | +```lua |
| 56 | +bit.bor(a: <number>, b: <number>) |
| 57 | +``` |
| 58 | +::: |
| 59 | + |
| 60 | +#### Parameter Description |
| 61 | + |
| 62 | +| Parameter | Description | |
| 63 | +|-----------|-------------| |
| 64 | +| a | Required. First number | |
| 65 | +| b | Required. Second number | |
| 66 | + |
| 67 | +#### Return Value |
| 68 | + |
| 69 | +| Type | Description | |
| 70 | +|------|-------------| |
| 71 | +| number | Returns the bitwise OR result of a and b | |
| 72 | + |
| 73 | +#### Usage |
| 74 | + |
| 75 | +```lua |
| 76 | +import("core.base.bit") |
| 77 | + |
| 78 | +local result = bit.bor(5, 3) -- Returns 7 |
| 79 | +``` |
| 80 | + |
| 81 | +## bit.bxor |
| 82 | + |
| 83 | +- Bitwise XOR operation |
| 84 | + |
| 85 | +#### Function Prototype |
| 86 | + |
| 87 | +::: tip API |
| 88 | +```lua |
| 89 | +bit.bxor(a: <number>, b: <number>) |
| 90 | +``` |
| 91 | +::: |
| 92 | + |
| 93 | +#### Parameter Description |
| 94 | + |
| 95 | +| Parameter | Description | |
| 96 | +|-----------|-------------| |
| 97 | +| a | Required. First number | |
| 98 | +| b | Required. Second number | |
| 99 | + |
| 100 | +#### Return Value |
| 101 | + |
| 102 | +| Type | Description | |
| 103 | +|------|-------------| |
| 104 | +| number | Returns the bitwise XOR result of a and b | |
| 105 | + |
| 106 | +#### Usage |
| 107 | + |
| 108 | +```lua |
| 109 | +import("core.base.bit") |
| 110 | + |
| 111 | +local result = bit.bxor(5, 3) -- Returns 6 |
| 112 | +``` |
| 113 | + |
| 114 | +## bit.bnot |
| 115 | + |
| 116 | +- Bitwise NOT operation |
| 117 | + |
| 118 | +#### Function Prototype |
| 119 | + |
| 120 | +::: tip API |
| 121 | +```lua |
| 122 | +bit.bnot(a: <number>) |
| 123 | +``` |
| 124 | +::: |
| 125 | + |
| 126 | +#### Parameter Description |
| 127 | + |
| 128 | +| Parameter | Description | |
| 129 | +|-----------|-------------| |
| 130 | +| a | Required. Number to negate | |
| 131 | + |
| 132 | +#### Return Value |
| 133 | + |
| 134 | +| Type | Description | |
| 135 | +|------|-------------| |
| 136 | +| number | Returns the bitwise NOT result of a | |
| 137 | + |
| 138 | +#### Usage |
| 139 | + |
| 140 | +```lua |
| 141 | +import("core.base.bit") |
| 142 | + |
| 143 | +local result = bit.bnot(5) -- Returns the bitwise NOT of 5 |
| 144 | +``` |
| 145 | + |
| 146 | +## bit.lshift |
| 147 | + |
| 148 | +- Left shift operation |
| 149 | + |
| 150 | +#### Function Prototype |
| 151 | + |
| 152 | +::: tip API |
| 153 | +```lua |
| 154 | +bit.lshift(a: <number>, b: <number>) |
| 155 | +``` |
| 156 | +::: |
| 157 | + |
| 158 | +#### Parameter Description |
| 159 | + |
| 160 | +| Parameter | Description | |
| 161 | +|-----------|-------------| |
| 162 | +| a | Required. Number to shift | |
| 163 | +| b | Required. Number of bits to shift left | |
| 164 | + |
| 165 | +#### Return Value |
| 166 | + |
| 167 | +| Type | Description | |
| 168 | +|------|-------------| |
| 169 | +| number | Returns a shifted left by b bits | |
| 170 | + |
| 171 | +#### Usage |
| 172 | + |
| 173 | +```lua |
| 174 | +import("core.base.bit") |
| 175 | + |
| 176 | +local result = bit.lshift(5, 2) -- Returns 20 (5 << 2) |
| 177 | +``` |
| 178 | + |
| 179 | +## bit.rshift |
| 180 | + |
| 181 | +- Right shift operation |
| 182 | + |
| 183 | +#### Function Prototype |
| 184 | + |
| 185 | +::: tip API |
| 186 | +```lua |
| 187 | +bit.rshift(a: <number>, b: <number>) |
| 188 | +``` |
| 189 | +::: |
| 190 | + |
| 191 | +#### Parameter Description |
| 192 | + |
| 193 | +| Parameter | Description | |
| 194 | +|-----------|-------------| |
| 195 | +| a | Required. Number to shift | |
| 196 | +| b | Required. Number of bits to shift right | |
| 197 | + |
| 198 | +#### Return Value |
| 199 | + |
| 200 | +| Type | Description | |
| 201 | +|------|-------------| |
| 202 | +| number | Returns a shifted right by b bits | |
| 203 | + |
| 204 | +#### Usage |
| 205 | + |
| 206 | +```lua |
| 207 | +import("core.base.bit") |
| 208 | + |
| 209 | +local result = bit.rshift(20, 2) -- Returns 5 (20 >> 2) |
| 210 | +``` |
| 211 | + |
| 212 | +## bit.tobit |
| 213 | + |
| 214 | +- Convert to 32-bit integer |
| 215 | + |
| 216 | +#### Function Prototype |
| 217 | + |
| 218 | +::: tip API |
| 219 | +```lua |
| 220 | +bit.tobit(x: <number>) |
| 221 | +``` |
| 222 | +::: |
| 223 | + |
| 224 | +#### Parameter Description |
| 225 | + |
| 226 | +| Parameter | Description | |
| 227 | +|-----------|-------------| |
| 228 | +| x | Required. Number to convert | |
| 229 | + |
| 230 | +#### Return Value |
| 231 | + |
| 232 | +| Type | Description | |
| 233 | +|------|-------------| |
| 234 | +| number | Returns x masked to 32-bit integer (0xffffffff) | |
| 235 | + |
| 236 | +#### Usage |
| 237 | + |
| 238 | +```lua |
| 239 | +import("core.base.bit") |
| 240 | + |
| 241 | +local result = bit.tobit(0x12345678) -- Returns value masked to 32 bits |
| 242 | +``` |
| 243 | + |
| 244 | +## bit.tohex |
| 245 | + |
| 246 | +- Convert number to hexadecimal string |
| 247 | + |
| 248 | +#### Function Prototype |
| 249 | + |
| 250 | +::: tip API |
| 251 | +```lua |
| 252 | +bit.tohex(x: <number>, n?: <number>) |
| 253 | +``` |
| 254 | +::: |
| 255 | + |
| 256 | +#### Parameter Description |
| 257 | + |
| 258 | +| Parameter | Description | |
| 259 | +|-----------|-------------| |
| 260 | +| x | Required. Number to convert | |
| 261 | +| n | Optional. Number of hexadecimal digits (default: 8). Use negative for uppercase | |
| 262 | + |
| 263 | +#### Return Value |
| 264 | + |
| 265 | +| Type | Description | |
| 266 | +|------|-------------| |
| 267 | +| string | Returns hexadecimal string representation | |
| 268 | + |
| 269 | +#### Usage |
| 270 | + |
| 271 | +```lua |
| 272 | +import("core.base.bit") |
| 273 | + |
| 274 | +local hex = bit.tohex(255, 2) -- Returns "ff" |
| 275 | +local hex = bit.tohex(255, -2) -- Returns "FF" |
| 276 | +local hex = bit.tohex(255) -- Returns "000000ff" |
| 277 | +``` |
| 278 | + |
0 commit comments