-
Notifications
You must be signed in to change notification settings - Fork 6
Add Bitwise Operators #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 4 commits
ac9cfc9
eb69c06
64f29f5
78e6bf1
8cdd7ba
e333a7b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -93,6 +93,12 @@ local items: {number} = {1, 2, 3} -- tables, not lists | |
| | 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 | | ||
| | Bitwise OR | `PASSIVE | SCRIPTED` | `bit32.bor(PASSIVE | SCRIPTED)` | No `|` operator | | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be 'bit32.bor(PASSIVE, SCRIPTED)
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
| | 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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added a note |
||
|
|
||
| ### Control Flow | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
btest() gives a boolean result - band() is wanted here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in my experience,
bandis more a footgun, because, most of the time, you want to put it in anifblock. and onlybtestmakes sense in anifstatement. (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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added a note
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
btestseems to be the one that matches the main use of&in LSL: