Skip to content

Add bitwise operations support - #68

Merged
scr-oath merged 9 commits into
vadv:masterfrom
dborovcanin:add-bitwise
Jun 6, 2025
Merged

Add bitwise operations support#68
scr-oath merged 9 commits into
vadv:masterfrom
dborovcanin:add-bitwise

Conversation

@dborovcanin

@dborovcanin dborovcanin commented Jun 4, 2025

Copy link
Copy Markdown
Contributor

Add support for bitwise operations: and, or, xor, not, lshift, rshift.

@dborovcanin
dborovcanin force-pushed the add-bitwise branch 2 times, most recently from 2a77c7e to a2d27f3 Compare June 4, 2025 15:33
Signed-off-by: Dusan Borovcanin <borovcanindusan1@gmail.com>

@scr-oath scr-oath left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Would you consider adding the Preload to https://github.com/vadv/gopher-lua-libs/blob/master/plugin/preload.go so that anyone who wants to just load everything can do so?

Comment thread bit/api.go Outdated
Comment thread bit/README.md Outdated
Comment thread bit/api.go Outdated
Comment thread bit/api.go
Comment thread bit/api.go Outdated
Comment thread bit/api.go
Comment thread bit/api.go
Comment thread bit/test/test_api.lua Outdated
Comment thread bit/README.md Outdated
Comment thread bit/api.go
return
}

func intToU32(i int) (uint32, error) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

question: Why why not 64 bit?

@dborovcanin dborovcanin Jun 5, 2025

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Mainly because numbers in Lua 5.1 are 64-bit floating-point, and Go uint64 overflows them so Go 64-bit uint cannot be presented properly in Lua 5.1 floating-point.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

makes sense; it's a little disappointing that it can't use the full size… I wonder if we should suffix methods with 32 to indicate that, but I won't press that and we can consider this resolved

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I can add a suffix if you insist. I was considering that, but I didn't do it because it's impossible to have <operation>u64 and u8 or u16 can be handled with the existing version. Maybe we can add a suffix in the future if we support different versions (possibly even u64), but that is unlikely to happen.

Signed-off-by: Dusan Borovcanin <borovcanindusan1@gmail.com>
Signed-off-by: Dusan Borovcanin <borovcanindusan1@gmail.com>
Signed-off-by: Dusan Borovcanin <borovcanindusan1@gmail.com>
Signed-off-by: Dusan Borovcanin <borovcanindusan1@gmail.com>
Signed-off-by: Dusan Borovcanin <borovcanindusan1@gmail.com>
Signed-off-by: Dusan Borovcanin <borovcanindusan1@gmail.com>
Signed-off-by: Dusan Borovcanin <borovcanindusan1@gmail.com>
Signed-off-by: Dusan Borovcanin <borovcanindusan1@gmail.com>
Comment thread bit/test/test_api.lua
Comment on lines +5 to +28
local tests = {
{
input1 = -3,
input2 = 23,
expected = nil,
err = "cannot convert negative int -3 to uint32",
},
{
input1 = 4294967296,
input2 = 23,
expected = nil,
err = "int 4294967296 overflows uint32",
},
{
input1 = 1,
input2 = 0,
expected = 0,
},
{
input1 = 111,
input2 = 222,
expected = 78,
}
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

praise: I like this style - I was thinking of doing it - Github copilot did the positional thing for me so I left it, but associative arrays are more descriptive!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks! I completely missed your PR on my branch, so I did it this way, similarly to the tests you linked in the previous comments (there's a bit of repetition, but I hope that's acceptable).

Comment thread bit/api.go
return
}

func intToU32(i int) (uint32, error) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

makes sense; it's a little disappointing that it can't use the full size… I wonder if we should suffix methods with 32 to indicate that, but I won't press that and we can consider this resolved

@scr-oath

scr-oath commented Jun 5, 2025

Copy link
Copy Markdown
Collaborator

Did you have more you wanted to add? I can merge this, but as you said that it wasn't ready before, just wanted to give you another chance to self-review and/or update before I merge… just give me the go and I'll merge/cut a release.

@vadv vadv left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

thank you!

@dborovcanin

Copy link
Copy Markdown
Contributor Author

Did you have more you wanted to add? I can merge this, but as you said that it wasn't ready before, just wanted to give you another chance to self-review and/or update before I merge… just give me the go and I'll merge/cut a release.

This is ready unless there are further comments. I could add a few more operations (e.g., rol, ror like in bitop), but that doesn’t seem necessary for now.

@scr-oath
scr-oath merged commit ccb2795 into vadv:master Jun 6, 2025
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.

3 participants