cardillan / mindcode

A high level language for Mindustry Logic (mlog) and Mindustry Schematics.
http://mindcode.herokuapp.com/
MIT License
84 stars 13 forks source link

Is missing: the `Bitwise not operator` #32

Closed schittli closed 1 year ago

schittli commented 3 years ago

Good evening

Unfortunately, I had overlooked that mindode does not understand the bitwise not operator. The Mindustry Game calls it Flip in its GUI, but the Mindustry Logic command is called as usual op not:

set TestVar 0xf
op not Result TestVar b
print TestVar
print Result

// Printed output (as expected): 
15
-16

Thanks a lot, kind regards, Thomas

cardillan commented 1 year ago

I've checked it right now, and at this moment both ! and not operators produce bitwise not:

a = !b
a = not b
print(a)

produces

op not a b 0
op not a b 0
print a

I'd suggest to close this issue.

On the other hand, Mindcode doesn't have boolean not. Neither has Mindustry Logic, but it could be compiled into equals variable false.

Seeing that Mindcode already has boolean operators and and or and binary operators & and |, it would look natural to me to have ! as a binary not and not as a boolean not. Thoughts?

Edited to add: I've just found out that I use ! habitually as a boolean not (is there actually any language that uses ! as a bitwise not?). I propose to have both not and ! be treated as binary operators and introduce ~ as the bitwise one. It would be a breaking change, though. :(

@francois, what's your opinion on this? Shall I create an issue for this?

francois commented 1 year ago

What is bitwise not? Is it equivalent to XOR with 0xffff ? I'm not against the change, I'm wondering how it would become useful. Are you packing bits in your variables, to save on variables?

cardillan commented 1 year ago

This is actually a horrible mess.

There's an instruction in Mindustry v6, which in the GUI is called "flip":

result = flip x

When pasted to the clipboard, this operation is called "not":

op not result x 0

Tooltip text for the operation says "Bitwise flip", whatever that means. I have no idea how it works behind the scenes, considering that Mindustry variables are doubles, but I guess that the value is numerically converted to long when performing any binary operation. Anyway, flip(0) = -1 and flip(1) = -2, so I guess it is XOR 0xffff... in some shape or form.

I understood the original issue as saying that there isn't a way to call this flip operation from Mindcode, but it isn't true, since both ! and not produce the op flip/not instruction.

I suggest closing this issue. All changes necessary to bring Mindcode closer to other high-level languages regarding the bitwise and boolean negations are described in #72.

cardillan commented 1 year ago

Operators have been revamped as part of the March 2023 update. Currently there are both bitwise and boolean NOT operators (see https://github.com/francois/mindcode/blob/main/SYNTAX.markdown#unary-and-binary-operators).