Author Topic: Continuing in the land of AND and the lore of OR ...  (Read 1669 times)

MrBcx

  • Administrator
  • Hero Member
  • *****
  • Posts: 2476
    • View Profile
Continuing in the land of AND and the lore of OR ...
« on: September 14, 2023, 09:43:21 AM »
I would like some feedback on the following suggestion.

ANDALSO and ORELSE are keywords in BCX, VB, and possibly other dialects of BASIC.

They are used to explicitly distinguish logical (AND) from bitwise (AND) operations.

Although both do their job correctly in BCX, I see an opportunity for improvement.

I have been reminded many times on the importance of maintaining consistency in
BCX's statements, functions, and argument lists.  With that particular philosophy in
mind, I would like to propose adding two aliases to BCX that would do the same jobs
as ANDALSO and ORELSE.

ANDALSO's alias would be LAND (Logical AND)

ORELSE's    alias would be LOR  (Logical OR)


These would be logical complements to BCX's longtime bitwise operators:

BAND (Bitwise AND)
BOR   (Bitwise OR)


FRUIT = APPLES BAND ORANGES

FRUIT = APPLES BOR ORANGES

FRUIT = APPLES LAND ORANGES

FRUIT = APPLES LOR ORANGES


The need and justification for multiple forms of AND / OR is that a single
statement can contain both bitwise and logical forms of these operators, making it nearly
impossible to parse the intentions of the programmer with absolute certainty.





jbk

  • Sr. Member
  • ****
  • Posts: 298
    • View Profile
Re: Continuing in the land of AND and the lore of OR ...
« Reply #1 on: September 14, 2023, 10:16:46 AM »
makes sense to me

Robert

  • Hero Member
  • *****
  • Posts: 1314
    • View Profile
Re: Continuing in the land of AND and the lore of OR ...
« Reply #2 on: September 14, 2023, 02:02:39 PM »
MrBcx stated,

Quote
"The need and justification for multiple forms of AND / OR is that a single
statement can contain both bitwise and logical forms of these operators, making it nearly
impossible to parse the intentions of the programmer with absolute certainty."
I would like to expand on this statement by adding that, in PowerBASIC and Microsoft Visual BASIC,
the AND operator does provide capabilities for processing both bitwise and logical (truth)
expressions in a single statement.

Visual BASIC
Quote
"For Boolean comparison, result is the logical conjunction of two Boolean values.

For bitwise operations, result is a numeric value representing the bitwise conjunction of two numeric bit patterns."

the above, quoted from
https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/operators/and-operator

PowerBASIC
Quote
"In an arithmetic expression, AND & OR are always bitwise.

In a boolean expression, they are logical (short-circuit) unless the operands are enclosed in parentheses."

the above, quoted from

https://forum.powerbasic.com/forum/user-to-user-discussions/programming/49344-and-observation

The Visual BASIC and PowerBASIC implementations do differ in that the PowerBASIC logical AND does short-circuit
while the Visual BASIC AND does not. ANDALSO is the Visual BASIC short-circuiting logical operator.

In the same PowerBASIC thread, quoted above, Michael Mattias wrote

"... because if something is not working the way I thought it should work, I don't want to be hunting around to find some statements where I may have mis-remembered the default operator ...."

While I sympathize with Mattias' statement, implementing dual logical/bitwise AND in BCX faces several problems,
so the introduction of LAND and LOR could be helpful.

The above comments, in general, also apply to the OR operator.

 


MrBcx

  • Administrator
  • Hero Member
  • *****
  • Posts: 2476
    • View Profile
Re: Continuing in the land of AND and the lore of OR ...
« Reply #3 on: September 14, 2023, 03:11:24 PM »
Thanks for the comments, so far.  I've added the aliases to the upcoming 8.0.6 lexicon. 

There are several things that I particularly like about my suggestion.

1) Easy to remember (similarity to BOR and BAND)

2) Easy to type

3) Doesn't require adding parenthesis

4) Complex statements and expressions containing bitwise and logical operators will "just work"

5) Does not threaten the current workings of BCX.

6) No one has to use them or change any of their code.
« Last Edit: September 14, 2023, 08:27:31 PM by MrBcx »

MrBcx

  • Administrator
  • Hero Member
  • *****
  • Posts: 2476
    • View Profile
Re: Continuing in the land of AND and the lore of OR ...
« Reply #4 on: September 14, 2023, 10:15:58 PM »
MrBcx stated,

Quote
"The need and justification for multiple forms of AND / OR is that a single
statement can contain both bitwise and logical forms of these operators, making it nearly
impossible to parse the intentions of the programmer with absolute certainty."
I would like to expand on this statement by adding that, in PowerBASIC and Microsoft Visual BASIC,
the AND operator does provide capabilities for processing both bitwise and logical (truth)
expressions in a single statement.
 

One point I'd like to clarify is that BCX does make the AND operator and the OR operator play
dual roles.  The BCX source code (Bc.bas), for example, makes that abundantly clear.  BCX itself
does not use any ANDALSO / ORELSE in the Bc.bas source.  That says something.

In BCX, IF-THEN statements generate &&  and || operators.  Arithmetic statements (as Bob Zale
referred to them) emit the bitwise operators & and |

The critical need that BCX has for the ANDALSO / ORELSE (and soon their aliases LAND/LOR)
is when IF-THEN statements that contain both bitwise and logical comparison clauses
The same is true for "arithmetic statements" that contain both bitwise and logical tests.
In both scenarios, it is best not to assume that BCX will correctly decipher what you're trying to
do.  Help BCX help you by letting it know when you're combining bitwise tests and logical tests.





« Last Edit: September 14, 2023, 10:36:33 PM by MrBcx »

Robert

  • Hero Member
  • *****
  • Posts: 1314
    • View Profile
Re: Continuing in the land of AND and the lore of OR ...
« Reply #5 on: September 16, 2023, 03:07:34 PM »
MrBcx stated,

Quote
"The need and justification for multiple forms of AND / OR is that a single
statement can contain both bitwise and logical forms of these operators, making it nearly
impossible to parse the intentions of the programmer with absolute certainty."
I would like to expand on this statement by adding that, in PowerBASIC and Microsoft Visual BASIC,
the AND operator does provide capabilities for processing both bitwise and logical (truth)
expressions in a single statement.
 

One point I'd like to clarify is that BCX does make the AND operator and the OR operator play
dual roles.  The BCX source code (Bc.bas), for example, makes that abundantly clear.  BCX itself
does not use any ANDALSO / ORELSE in the Bc.bas source.  That says something.

In BCX, IF-THEN statements generate &&  and || operators.  Arithmetic statements (as Bob Zale
referred to them) emit the bitwise operators & and |

The critical need that BCX has for the ANDALSO / ORELSE (and soon their aliases LAND/LOR)
is when IF-THEN statements that contain both bitwise and logical comparison clauses
The same is true for "arithmetic statements" that contain both bitwise and logical tests.
In both scenarios, it is best not to assume that BCX will correctly decipher what you're trying to
do.  Help BCX help you by letting it know when you're combining bitwise tests and logical tests.

Hi MrBCX:

The help file Logical / Bitwise Operators sections have been restructured as follows

AND / OR Operators

AND, OR are in the leading section with your above quoted comments

Your explanation and a suggestion to explicitly specify Logical or Bitwise Operators will help stop problems occurring from the myriad of corner-cases which break your IF / THEN rule such as this, for example,

if a coder had an expectation that this code

Code: [Select]
  IF (mask AND (NOT mask >> 1)) THEN
would translate to

Code: [Select]
  if (mask & (~mask >> 1))
they would be disappointed.

Using the specific bitwise operators

Code: [Select]
IF (mask BAND (BNOT mask >> 1)) THEN
produces a reasonable translation

Code: [Select]
#define BNOT ~(int)
...
if((mask  & (BNOT  mask>>1))){

F.W.I.W., here is the functional C snipprt from which the above example was taken

Code: [Select]
int is_netmask_valid(uint32_t mask)
{
    if (mask == 0) return 0;
    if (mask & (~mask >> 1)) {
        return 0;
    } else {
        return 1;
    }
}

Logical Operators

LAND
LOR
NOT

ANDALSO, ORELSE will be mentioned as alias in Remarks.

Bitwise Operators

BAND
BOR
XOR
BNOT



MrBcx

  • Administrator
  • Hero Member
  • *****
  • Posts: 2476
    • View Profile
Re: Continuing in the land of AND and the lore of OR ...
« Reply #6 on: September 16, 2023, 04:53:51 PM »
ISTRUE() and ISFALSE() might also qualify as honorable mentions in that section.

Their creation was inspired by the PowerBasic functions of the same names.  The
late great Bob Zale likely created those to help mitigate the kinds of things this
thread centers around.

Perhaps place a "Related Functions" link to: 

https://bcxbasiccoders.com/webhelp/html/bool$function.htm

« Last Edit: September 16, 2023, 05:02:12 PM by MrBcx »

Robert

  • Hero Member
  • *****
  • Posts: 1314
    • View Profile
Re: Continuing in the land of AND and the lore of OR ...
« Reply #7 on: September 24, 2023, 06:51:09 PM »
It may be of some comfort to know that others share the AND/OR quandary. How many others ? Thousands ! And now, AND and OR have been sent to the United States Supreme court to be judged.

22-340 PULSIFER V. UNITED STATES

DECISION BELOW: 39 F.4th 1018

CERT. GRANTED 2/27/2023

QUESTION PRESENTED:
The "safety valve" provision of the federal sentencing statute requires a district court to ignore any statutory mandatory minimum and instead follow the Sentencing Guidelines if a defendant was convicted of certain nonviolent drug crimes and can meet five sets of criteria.
See 18 U.S.C. § 3553(f)(1)-(5). Congress amended the first set of criteria, in§ 3553(f)(1), in the First Step Act of 2018, Pub. L. No. 115-391, § 402, 132 Stat. 5194, 5221, broad criminal justice and sentencing reform legislation designed to provide a second chance for nonviolent offenders. A defendant satisfies § 3553(f)(1), as amended, if he "does not have-(A) more than 4 criminal history points, excluding any criminal history points resulting from a 1-point offense, as determined under the sentencing guidelines; (B) a prior 3-point offense, as determined under the sentencing guidelines; and (C) a prior 2-point violent offense, as determined under the sentencing guidelines." 18 U.S.C. § 3553(f)(1) (emphasis added).

The question presented is whether the "and" in 18 U.S.C. § 3553(f)(1) means "and," so that a defendant satisfies the provision so long as he does not have (A) more than 4 criminal history points, (B) a 3-point offense, and (C) a 2-point offense (as the Ninth Circuit holds), or whether the "and" means "or," so that a defendant satisfies the provision so long as he does not have (A) more than 4 criminal history points, (B) a 3- point offense, or (C) a 2-point violent offense (as the Seventh and Eighth Circuits hold).




MrBcx

  • Administrator
  • Hero Member
  • *****
  • Posts: 2476
    • View Profile
Re: Continuing in the land of AND and the lore of OR ...
« Reply #8 on: September 24, 2023, 08:02:49 PM »
And when the Justices get through with that case, they can slice and dice the word,"is" again.

https://slate.com/news-and-politics/1998/09/bill-clinton-and-the-meaning-of-is.html