Skip to content

Add Bitwise Operators#21

Open
tapple wants to merge 6 commits into
secondlife:mainfrom
tapple:patch-2
Open

Add Bitwise Operators#21
tapple wants to merge 6 commits into
secondlife:mainfrom
tapple:patch-2

Conversation

@tapple
Copy link
Copy Markdown

@tapple tapple commented Dec 21, 2025

No description provided.

Copy link
Copy Markdown

@monty-linden monty-linden left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two corrections. And maybe a warning for readers.

| 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 |
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be 'bit32.bor(PASSIVE, SCRIPTED)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

| 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 |
Copy link
Copy Markdown

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

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added a note

Copy link
Copy Markdown

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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 |
Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added a note

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants