Author Topic: Toggle = not Toggle that is the question  (Read 4542 times)

0 Members and 1 Guest are viewing this topic.

Arnold

  • Guest
Toggle = not Toggle that is the question
« on: November 27, 2015, 01:58:47 AM »
Hi Charles,

I would like to ask a question about boolean values. I tried out this little test code:

Code: OxygenBasic
  1. 'Toggle.o2bas
  2.  
  3. include "$/inc/minwin.inc"
  4. include "$/inc/console.inc"
  5.  
  6. print "True = " true & cr
  7. print "False = " false & cr & cr
  8.  
  9. bool toggle = not false  'true
  10.  
  11. while 1
  12.   toggle=not toggle
  13.   print "toggle = " toggle & cr
  14.   if not toggle then print "this is: not toggle  (" toggle ")" & cr
  15.   print "Enter ..." & cr & cr
  16.   waitkey()
  17. wend
  18.  

If I run this code the case of 'not toggle' will be displayed correctly. If I apply at the beginning: bool toggle = true then this will not work. (not toggle -> -2).

I suppose there are reasons why you use -1/0 for true/false in boolean types? But why is 1/-2 used for true/false values if I apply the true value of minwin.inc?

Roland

.

Patrice Terrier

  • Guest
Re: Toggle = not Toggle that is the question
« Reply #1 on: November 27, 2015, 06:51:32 AM »
This is a classical behavior.

Better to define is as long

long toggle = 0

toggle = not toggle // then toggle = -1

if toggle is define as toogle = 1, then you will always get a bad result with the NOT operator.


Arnold

  • Guest
Re: Toggle = not Toggle that is the question
« Reply #2 on: November 27, 2015, 07:21:35 AM »
Hi Patrice,

bool toggle
bool toggle = false
bool toggle = -1

these combinations will also work with OxygenBasic besides using long. So there would be enough workarounds. I only wondered if there is a reason for 1/-2 values when using 1 as a value for True.

Roland

Patrice Terrier

  • Guest
Re: Toggle = not Toggle that is the question
« Reply #3 on: November 27, 2015, 08:59:14 AM »
This is because NOT 1 = -2

Arnold

  • Guest
Re: Toggle = not Toggle that is the question
« Reply #4 on: November 27, 2015, 12:33:39 PM »
Thank you for the help. I did not expect this result, but my windows calculator gives the same answer. Although I did not yet find the explanation in Internet for this rule I will keep it in mind for the future.

Roland

Charles Pegge

  • Guest
Re: Toggle = not Toggle that is the question
« Reply #5 on: November 27, 2015, 01:38:37 PM »
Hi Roland & Patrice,

I would add that Oxygen does not use real boolean types. It uses 32 bit integers, in common with C/Windows API.

All Oxygen logic is bitwise, so when performing Boolean logic, use % true -1

some negative binaries and their end bit patterns:

-1  ffff  1111
-2  fffe  1110
-3  fffd  1101
-4  fffc  1100


Mike Lobanovsky

  • Guest
Re: Toggle = not Toggle that is the question
« Reply #6 on: November 27, 2015, 02:06:10 PM »
In order to follow Boolean logic rules, True doesn't have to be defined at all. With False = 0 as in all the languages I'm aware of, the assumption that True is anything that's <> False | 0 is sufficient to express every possible logical combination. The numeric representation of True as -1 for retro-BASICs and other languages with a long historical record is purely traditional and is strictly engine implementation specific; any representation would do as long as it is consistent and taken account of by the engine.

Note that C defines False as long 0, and True is in fact anything that's not False, with the internal numeric representation of True being 1. C++, C#, Visual Basic, and VB.NET follow the same pattern. FBSL follows it too.

Also note that, despite a particular numeric representation of True and False in a given language, many languages would not allow you direct arithmetics (hexa, decimal, octal, or binary) with Booleans or comparison of Boolean values for equality/inequality with numeric values where the sign of their numeric representation might have otherwise affected the result.

For the sake of compatibility with major modern BASICs and C/C++/C# and their derivatives, I'd recommend sticking with the C interpretation of Booleans when designing your own Boolean based branching in your apps.

JRS

  • Guest
Re: Toggle = not Toggle that is the question
« Reply #7 on: November 27, 2015, 09:25:41 PM »
Mike is correct.

Code: [Select]
TRUE = NOT FALSE

Mike Lobanovsky

  • Guest
Re: Toggle = not Toggle that is the question
« Reply #8 on: November 28, 2015, 02:23:56 AM »
Yes, John, and that's the only thing you can safely rely on. Everything else comes from the devil (or the dev). :D

Charles Pegge

  • Guest
Re: Toggle = not Toggle that is the question
« Reply #9 on: November 28, 2015, 02:03:14 PM »

Laws of Form by G Spencer-Brown, provides A very interesting view on the roots of logic.

https://en.wikipedia.org/wiki/Laws_of_Form

In this perspective, conflating operators and operands, True is the null no-action state, and False is the distinctive/transformative state.


JRS

  • Guest
Re: Toggle = not Toggle that is the question
« Reply #10 on: November 28, 2015, 05:54:41 PM »
The belief that anything other than zero is TRUE seems correct in my mind. In Script BASIC, FALSE = 0 and TRUE = -1. TRUE/FALSE are SB keywords with predefined values.

Code: Script BASIC
  1. T = 10
  2. IF T = NOT FALSE THEN
  3.   PRINT T,"\n"
  4. END IF
  5.  


jrs@laptop:~/sb/sb22/test$ scriba notfalse.sb
jrs@laptop:~/sb/sb22/test$


OT

As to the discussion on BP.org about language popularity, PHP and JavaScript are the most widely used languages in todays computing. The desktop is no longer king.

« Last Edit: November 28, 2015, 07:04:33 PM by John »

Arnold

  • Guest
Re: Toggle = not Toggle that is the question
« Reply #11 on: November 29, 2015, 04:15:17 AM »
Thank you again for all the info. I now understand that:

not   0=-1, not  1=-2, not  5=-6, not 17=-18 etc.
not -18=17, not -6=5,  not -2=1,  not -1=0

This looks simple, but it was not for me. After some experimenting I found that False and True defined in minwin.inc should work, sometimes with a small workaround. Therefore for some special cases I will use:

% bTrue=-1 'bitwise True

I think this should be sufficient to catch most of the possible False / True cases.

Roland

Peter

  • Guest
Re: Toggle = not Toggle that is the question
« Reply #12 on: November 29, 2015, 04:36:36 AM »
As to the discussion on BP.org about language popularity, PHP and JavaScript are the most widely used languages in todays computing. The desktop is no longer king.

Now I am slave.  >:(

JRS

  • Guest
Re: Toggle = not Toggle that is the question
« Reply #13 on: November 29, 2015, 04:50:48 AM »
Quote
Now I am slave.

Could be worse. You could be someone's bitch.  :-*

Computed GOTO

In the day ...
Code: [Select]
ON x GOTO 100, 200, 300, 400, ...



Peter

  • Guest
Re: Toggle = not Toggle that is the question
« Reply #14 on: November 29, 2015, 06:07:32 AM »
Quote
Computed GOTO

will do when I'm dead.  ;D