Skip to content
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

Implement bitwise negation on numeric types #457

Open
sjbarag opened this issue Jul 10, 2020 · 0 comments
Open

Implement bitwise negation on numeric types #457

sjbarag opened this issue Jul 10, 2020 · 0 comments
Labels
bug Any difference between this BrightScript implementation and RBI, or otherwise unexpected behavior interpreter Affects this project's tree-walking interpreter stdlib Affects the standard library included with this BrightScript implementation

Comments

@sjbarag
Copy link
Owner

sjbarag commented Jul 10, 2020

RBI supports bitwise negation of integers (and possibly floating-point/double values; needs confirmation), but brs currently doesn't. Let's make that work! Some details from a comment on a PR:

' in RBI
print not  0 ' => -1
print not  1 ' => -2
print not -1 ' =>  0
print not  2 ' => -3
print not -2 ' =>  1

which looks like RBI is using a two's complement representation for numbers and does something slightly unexpected with it. not someInteger in RBI seems to take someInteger, convert to a binary representation, perform a bitwise negation on it (all 0s become 1s, all 1s become 0s), then convert back to a decimal representation in the Two's complement understanding. So for not 0 (using just 4-bits for now because I'm lazy), we have:

not 0 (decimal) == not 0b0000 == 0b1111 == -1 (decimal in two's complement)

How strange! In the not 2 * 3 example, we get:

not (2 * 3) == not 6 (decimal) == not 0b0110 == 0b1001 == -7 (decimal in two's complement)
@sjbarag sjbarag added bug Any difference between this BrightScript implementation and RBI, or otherwise unexpected behavior interpreter Affects this project's tree-walking interpreter stdlib Affects the standard library included with this BrightScript implementation labels Jul 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Any difference between this BrightScript implementation and RBI, or otherwise unexpected behavior interpreter Affects this project's tree-walking interpreter stdlib Affects the standard library included with this BrightScript implementation
Projects
None yet
Development

No branches or pull requests

1 participant