Announcement
Collapse
No announcement yet.
Any reason not to use XOR?
Collapse
X
-
Sorry Eric, I didn't see the minus sign to the left of the word - so I see how yours works.
-
FWIW, ISTRUE() and ISFALSE() exist precisely to deal with "NOT didn't work the way I thought it would"
Leave a comment:
-
>For example "IF NOT ExcludeTitle& THEN" reads almost like a plain-English sentence.
Sure, if it's Master Yoda speaking.....
Leave a comment:
-
Gary --
> NOT 0 gives -1
> NOT 1 gives -2
> Did you mean something else?
No.
NOT 0 gives -1
NOT -1 gives 0
So the value of the variable toggles between those two values, which are the "natural" True and False values for all signed integers.
The math in my Menu Set State line has an added minus-sign to produce 0 or positive 8, which are the values that are required by the API.
There's nothing wrong with using XOR, I just think that NOT is more intuitive and human-readable. For example "IF NOT ExcludeTitle& THEN" reads almost like a plain-English sentence.
-- Eric
Leave a comment:
-
Thanks for the response.
While direct usage is the simples, as in:
Code:gLight.nStatus = nLightStatus
The actual code is used in setting a variety of flags, with the code usually sitting in a dll used by third parties. I could redo the code, like:
Code:FUNCTION SetLightStatus( nLightStatus as long ) EXPORT AS LONG FUNCTION = %OK ' Set default response IF (nLightStatus = %LIGHT_ON OR nLightStatus = %LIGHT_OFF ) THEN gLight.nStatus = nLightStatus ELSE FUNCTION = %FAILED_BAD_PARAMETER END IF END FUNCTION
Code:gLight.nStatus = IIF&( nLightStatus = 0, %LIGHT_ON, %LIGHT_OFF )
Anyhow, I take it from your responses that XOR is still used, appears suitable for the job and function nicely.
Thanks,
Pat
Leave a comment:
-
LightOn = %TRUE | %FALSE
I think the question is...."How the heck is that used????"
inquiring minds would like to know
Leave a comment:
-
>But I didn't understand your response. Would you clarify what you meant?
Why mess with XOR when %TRUE or %FALSE will do?
Leave a comment:
-
gLight.nStatus = 0 XOR nLightStatus
If the zero is hard coded, you might as well just use:
gLight.nStatus = nLightStatus
Leave a comment:
-
Sorry MCM,
But I didn't understand your response. Would you clarify what you meant?
Leave a comment:
-
In this test,
NOT 0 gives -1
NOT 1 gives -2
Code:MsgBox Str$(Not 0) MsgBox Str$(Not 1)
Leave a comment:
-
I prefer this syntax:Code:ExcludeTitle& = NOT ExcludeTitle& 'produces -1 or 0 Menu Set State hMenuSyntax, ByCmd %ID_ExcludeTitle, -ExcludeTitle&*8 'produces 8 or 1
-- Eric
Leave a comment:
-
I use it all the time, especially to flip state on a menu.
Code:Case %ID_ExcludeTitle If Cb.CtlMsg = %BN_Clicked Then 'flip check status ExcludeTitle& = ExcludeTitle& Xor 1 Menu Set State hMenuSyntax, ByCmd %ID_ExcludeTitle, ExcludeTitle&*8 End If
Leave a comment:
-
Usually the XOR operator is better in a toggle type situation. Although it can be used as you've shown, it's definitely faster to directly set the value when you don't care about its previous state:
Code:gLight.nStatus = %LIGHT_ON 'or gLight.nStatus = %LIGHT_OFF
Leave a comment:
-
XOR is one of the tools, and if it's the best tool for the job, use it.
Leave a comment:
-
Any reason not to use XOR?
Hello Forum,
I always thought that the simplest way to set a variable to either 0 or 1 was with the XOR function, as in the following snippet:
Code:%LIGHT_ON = 0 %LIGHT_OFF = 1 TYPE Lights nStatus as LONG ' Must be either %LIGHT_ON or %LIGHT_OFF END TYPE SUB SetLightStatus( nLightStatus as long ) gLight.nStatus = 0 XOR nLightStatus END SUB ' Usage: SetLightStatus %LIGHT_ON ' Set light status to on ' or SetLightStatus %LIGHT_OFF ' Set light status to off
However, it has been suggested (by the young "bulls") that XOR is old-fashioned and that I should be using in-built functions like IIF() or SWITCH() or even a simple IF..THEN statement, but I can't see why.
Surely XOR is not that hard to understand and (from memory) it relates directly to the assembler command - XOR - and must be very effiecient.
Is there a better way of doing this in PowerBASIC?
PatTags: None
Leave a comment: