bmx-ng / bmx-site

BlitzMax website source
3 stars 7 forks source link

Tutorial "Enum" contains a mistake #72

Open MidimasterSoft opened 2 years ago

MidimasterSoft commented 2 years ago

I read the chapter about ENUMs at your homepage: https://blitzmax.org/docs/en/language/enums/ and found this: ScreenshotEnumBug

Here you write that a XOR-operation removes a flag from the variable meetingDays. But this is only correct if the flag is already part of the variable. In cases, where the flag is not there this would cause a complete wrong result.The flag will be added:

SuperStrict 
Enum EDays flags
    Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday
End Enum

Local meetingDays:EDays = EDays.Tuesday | EDays.Thursday

' Remove a flag using bitwise XOr.
meetingDays :~ EDays.Saturday

Print "Meeting days are " + meetingDays.ToString()

The better operation is a combination of AND and XOR. This 100% removes a BIT from a variable:

SuperStrict 
Enum EDays flags
    Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday
End Enum

Local meetingDays:EDays = EDays.Tuesday | EDays.Thursday

' Remove a flag using bitwise XOr.
meetingDays :&~ EDays.Saturday    '   <--------  SEE CHANGE HERE

Print "Meeting days are " + meetingDays.ToString()
woollybah commented 2 years ago

I believe the documentation is correct, because that is how XOR works. If a flag it set, it will unset it. If a flag is unset, it will set it.

The example describes using XOR to remove an enum from a set.

GWRon commented 2 years ago

Then it should possibly inform that "xor" is not simply "unsetting" ... it can have "side effects" (for not yet set flags).

It acts as a "switch/flip" there. So maybe add an example (line in the sample) on how to remove something which "might" be set.

MidimasterSoft commented 2 years ago

Then it should possibly inform that "xor" is not simply "unsetting" ... <

But that's what is written in the Tutorial: "Remove a flag using XOR"

..means for me "For removing a flag use XOR" And this is wrong. XOR swaps the state, but does not remove.

A clear "Removing" can only be done with the combination of AND and XOR. So why not writing:


Remove a flag using bitwise AND and XOr.
meetingDays :&~ EDays.Saturday

I'm only reporting... you decide. Thats ok for me.

GWRon commented 2 years ago

meetingDays :&~ EDays.Saturday

you should write this as: meetingDays :& ~EDays.Saturday as this is what you then do: you want to know what "meetingDays AND (complete enum except saturday)" have in common. (~EDays.Saturday is "Monday|Tuesday|Wednesday|Thursday|Friday|Saturday")

MidimasterSoft commented 2 years ago

... (~EDays.Saturday is "Monday|Tuesday|Wednesday|Thursday|Friday|Saturday")

typo: ~EDays.Saturday is "Monday|Tuesday|Wednesday|Thursday|Friday|Sunday"