Add Bitwise Operators#21
Conversation
| | Integer division | `7 / 4` → `1` | `7 // 4` → `1` | Use `//` not `/` | | ||
| | String length | `llStringLength(s)` | `#s` | Native operator | | ||
| | Bitwise AND | `llGetAgentInfo(id) & AGENT_TYPING` | `bit32.btest(ll.GetAgentInfo(id), AGENT_TYPING)` | No `&` operator | | ||
| | Bitwise OR | `PASSIVE | SCRIPTED` | `bit32.bor(PASSIVE | SCRIPTED)` | No `|` operator | |
There was a problem hiding this comment.
Should be 'bit32.bor(PASSIVE, SCRIPTED)
| | Power | `llPow(2, 3)` | `2 ^ 3` | Native operator | | ||
| | Integer division | `7 / 4` → `1` | `7 // 4` → `1` | Use `//` not `/` | | ||
| | String length | `llStringLength(s)` | `#s` | Native operator | | ||
| | Bitwise AND | `llGetAgentInfo(id) & AGENT_TYPING` | `bit32.btest(ll.GetAgentInfo(id), AGENT_TYPING)` | No `&` operator | |
There was a problem hiding this comment.
btest() gives a boolean result - band() is wanted here
There was a problem hiding this comment.
in my experience, band is more a footgun, because, most of the time, you want to put it in an if block. and only btest makes sense in an if statement. (I even wrote a linter rule about this because it bit me so hard: luau-lang/luau#2184)
There was a problem hiding this comment.
Perhaps for idiomatic Lua, there are two modes of expression for bitwise AND:
b32.band()where the resulting value is important(b32.btest() [and/or] b32.btest() [and/or] b32.btest() ...)where a conditional test is being constructed. Test varies whether you want all mask bits set or any mask bit set.
Anyway, changes accepted.
There was a problem hiding this comment.
btest seems to be the one that matches the main use of & in LSL:
> flags = bit32.bor(CONTROL_UP, CONTROL_LEFT)
> bit32.btest(flags, CONTROL_UP)
true
> bit32.btest(flags, CONTROL_LEFT)
true
> bit32.btest(flags, CONTROL_DOWN)
false| | Bitwise NOT | `~0` | `bit32.bnot(0)` | No `~` operator | | ||
| | Bitwise eXclusive OR | `6 ^ 3` | `bit32.bxor(6, 3)` | `^` means Power instead | | ||
| | Bitwise Shift Left | `1 << 2` | `bit32.lshift(1, 2)` | No `<<` operator | | ||
| | Bitwise Shift Right | `0x80000000 >> 2` | `bit32.arshift(0x80000000, 2)` | No `>>` operator | |
There was a problem hiding this comment.
In LSL, this is an arithmetic shift on signed, 32-bit value, not bitwise on unsigned. '0x80000000 >> 2' produces the result -536870912. In Luau, 'bit32.arshift(0x80000000, 2)' is a pseudo-signed operator producing the result 3758096384. This is bit correct though not value correct but that's expected here. May wish to warn the reader.
No description provided.