Lua Bitwise Operators
Lua 5.3 added native bitwise operators that work on the bits of 64-bit integers. They are used for flags and permissions, colour packing, hashing, binary file formats and network protocols.
| Operator | Name | Example (binary) |
|---|---|---|
& |
AND | 0b1100 & 0b1010 → 0b1000 |
| |
OR | 0b1100 | 0b1010 → 0b1110 |
~ |
XOR (binary) | 0b1100 ~ 0b1010 → 0b0110 |
~ |
NOT (unary) | ~0 → -1 |
<< |
shift left | 1 << 4 → 16 |
>> |
shift right (logical, fills with zeros) | 256 >> 4 → 16 |
Lua has no binary literals, so the examples use decimal or hex numbers.
local a, b = 12, 10 -- 1100 and 1010 in binary
print(a & b, a | b, a ~ b)
print(~0, ~a)
print(1 << 4, 256 >> 4, 0xFF << 8)8 14 6
-1 -13
16 16 65280
~ means XOR with two operands and NOT with one; ~= is still "not equal".
Showing bits
A small helper makes bit patterns visible:
local function bits(n, width)
local out = {}
for i = width - 1, 0, -1 do
out[#out + 1] = (n >> i) & 1
end
return table.concat(out)
end
print(bits(12, 8), bits(10, 8))
print(bits(12 & 10, 8), bits(12 | 10, 8), bits(12 ~ 10, 8))
print(bits(5 << 2, 8))00001100 00001010
00001000 00001110 00000110
00010100
Flags in one integer
Each bit can store a yes/no option. Combine flags with |, test with &, clear with & ~flag, toggle with ~:
local READ, WRITE, EXEC = 1, 2, 4 -- 001, 010, 100
local perms = READ | WRITE
local function describe(p)
return ((p & READ ~= 0) and "r" or "-")
.. ((p & WRITE ~= 0) and "w" or "-")
.. ((p & EXEC ~= 0) and "x" or "-")
end
print(perms, describe(perms))
perms = perms | EXEC -- add
print(perms, describe(perms))
perms = perms & ~WRITE -- remove
print(perms, describe(perms))
perms = perms ~ READ -- toggle
print(perms, describe(perms))3 rw-
7 rwx
5 r-x
4 --x
Precedence works in your favour here: in Lua, comparison operators bind more loosely than the bitwise ones, so p & READ ~= 0 means (p & READ) ~= 0. (In C it is the other way round, a classic bug.) Adding parentheses never hurts, though.
Packing colours
A 24-bit RGB colour like #FF8800 stores red, green and blue in one number. Shifts and masks take it apart and put it back together:
local function unpackRGB(c)
return (c >> 16) & 0xFF, (c >> 8) & 0xFF, c & 0xFF
end
local function packRGB(r, g, b)
return (r << 16) | (g << 8) | b
end
local orange = 0xFF8800
print(unpackRGB(orange))
local teal = packRGB(0, 128, 128)
print(string.format("#%06X", teal))255 136 0
#008080
Rules for operands
- Operands are converted to integers. Floats work only if they have an exact integer value (
2.0is fine,2.5is an error). - Shifts by 64 or more give
0; negative shift amounts shift the other way. >>is a logical shift: it fills with zeros, so negative numbers become large positive ones.
print(6.0 & 3, 1 << 64, 16 >> -1)
print(-1 >> 60)
print(pcall(function() return 2.5 | 1 end))2 0 32
15
false /app/output.s:3: number has no integer representation
Test yourself
What is 5 & 3?
1768
What does the binary operator ~ mean, as in a ~ b?
- Not equal
- Bitwise NOT
- Bitwise XOR
- Approximately equal
How do you clear the flag WRITE from perms?
perms - WRITEperms & ~WRITEperms | ~WRITEperms ~= WRITE