Menu

#468 document binary operators

Programmer's Guide
closed
None
5 - default
2024-06-12
2024-03-14
No

Those were backported into GnuCOBOL 3.2, but as no one passed the information (other than the NEWS entry), this is currently missing in the PG.

From the standard (2023):

A boolean operator specifies the type of boolean operation to be performed on one or two operands, for a unary operator or binary operator, respectively. The following are the boolean operators:

Binary boolean operators Meaning
B-AND AND operation (boolean conjunction)
B-OR Inclusive OR operation (boolean inclusive disjunction)
B-XOR Exclusive OR operation (boolean exclusive disjunction)
Unary boolean operator Meaning
B-NOT Negation operation
Boolean shift operators Meaning
B-SHIFT-L SHIFT LEFT operation
B-SHIFT-LC Circular SHIFT LEFT operation
B-SHIFT-R SHIFT RIGHT operation
B-SHIFT-RC Circular SHIFT RIGHT operation

The shift operators existed in other dialects before they were added to the standard, B-SHIFT-L->B-LEFT + B-SHIFT-R->B-RIGHT; which is also supported by GnuCOBOL (if you don't use a strict dialect that does not have these reserved words). They operate on the binary value (no packing/unpacking done).

The binary operators can be used in computations or expressions. Both sides can be a numeric item or literal (including the special hex notations; see samples in tests/testsuite.src/run_extensions.at.

More quoting:

Evaluation of a boolean expression shall proceed as follows:
a) [... parentheses ...]
b) The precedence of operations at the same level of inclusiveness, is:
1st — negation (B-NOT)
2nd — conjunction (B-AND)
3rd — exclusive disjunction (B-XOR)
4th — inclusive disjunction (B-OR)
The precedence of boolean shift operations (B-SHIFT-L, B-SHIFT-R, B-SHIFT-LC and B-SHIFT-RC) is the same as that of the preceding operation, if any. If the boolean shift operation is not preceded by another operation, the precedence of that operation is the same as B-AND.
c) When the sequence of evaluation is not specified by parentheses, the evaluation of operations with the same precedence shall proceed from left to right.

Discussion

  • Simon Sobisch

    Simon Sobisch - 2024-03-14

    @dilodilo Do you think we need more information here?

     
    • Eugenio Di Lorenzo

      Wow, This is very cool.

      I think a new Chapter should be inserted after 2.2.4. Arithmetic Expressions.
      The new Chapter will be: 2.2.5 Boolean Expression.

      Insert first of all the Syntax:

      Boolean-Expression Syntax

      [ Operand-1 ] { Boolean-Operator } Operand-2

      and in this new chapter insert the exact text of your previous post
      (be careful ... in your post there is a typo error: B-SHIFT-L SHIFT must be B-SHIFT-L)

      I would then add the following table:

      Examples of Boolean Operations
      +-----------------------------------------------------------------+ 
      | Boolean operation     |operand-1| Operator   |operand-2| Result |
      +-----------------------+---------+------------+---------+--------+
      | Conjunction           |  1100   | B-AND      |  0101   |  0100  |
      | Inclusive disjunction |  1100   | B-OR       |  0101   |  1101  |
      | Exclusive disjunction |  1100   | B-XOR      |  0101   |  1001  |
      | Negation              |    -    | B-NOT      |  1100   |  0011  |
      | Shift Left            |    -    | B-SHIFT-L  |  1101   |  1010  |
      | Shift Left Circuar    |    -    | B-SHIFT-LC |  1101   |  1011  |
      | Shift Right           |    -    | B-SHIFT-R  |  1101   |  0110  |
      | Shift Right Circuar   |    -    | B-SHIFT-RC |  1101   |  1110  |
      +-----------------------------------------------------------------+
      

      PS. Change also "Appendix C1 - Grouped Word Lists by feature and function"

      • Add 4 new operators B-SHIFT-L, B-SHIFT-LC, B-SHIFT-R and B-SHIFT-RC
      • Set Yes instead of No for all Boolean operators now supported.
       
  • Eugenio Di Lorenzo

    I have prepared this test program.
    Please, could verify that the results (see screenshot) are correct ?

           >>SOURCE FORMAT IS FREE
    IDENTIFICATION DIVISION.
    PROGRAM-ID. TESTBOOLEAN.
    DATA DIVISION.
    WORKING-STORAGE SECTION.
    01 A pic 9 comp-x value B'00000101'.
    01 B pic 9 comp-x value B'00000011'.
    01 c pic 9        value LOW-VALUE. 
    
    PROCEDURE DIVISION.
      display space
      display '   a          b     c        a                 b          c    '
      display '  --- ------ ---   ---    -------- -------- --------   --------'
      display '   '  a  '  initial ' b '     ' c '     ' FUNCTION BIT-Of(a) ' initial  ' FUNCTION BIT-Of(b) '   ' FUNCTION BIT-OF(c)
      display space   display space
      display '   a operator b     c        a     operator    b          c    '
      display '  --- ------ ---   ---    -------- -------- --------   --------'
      compute c = a B-AND b 
      display '   '  a  '   AND    ' b '  =  ' c '     '  FUNCTION BIT-Of(a)  '   AND    ' FUNCTION BIT-Of(b) ' = ' FUNCTION BIT-OF(c)
      compute c = a B-OR b 
      display '   '  a  '   OR     ' b '  =  ' c '     '  FUNCTION BIT-Of(a)  '   OR     ' FUNCTION BIT-Of(b) ' = ' FUNCTION BIT-OF(c)
      compute c = a B-XOR b 
      display '   '  a  '   XOR    ' b '  =  ' c '     '  FUNCTION BIT-Of(a)  '   XOR    ' FUNCTION BIT-Of(b) ' = ' FUNCTION BIT-OF(c)
      compute c = B-NOT b 
      display '   -'    '   NOT    ' b '  =  ' c '         -   '              '   NOT    ' FUNCTION BIT-Of(b) ' = ' FUNCTION BIT-OF(c)
    
      accept omitted 
      STOP RUN.
    
     
  • Eugenio Di Lorenzo

    Second test program for "SHIFT" operators.

    @Vincent: in the programmers guide you entered the B-SHIFT-xx operators as intrinsic functions. I believe this is incorrect.
    I ask Simon for confirmation.
    For example B-SHIFT-L is an "operator" like B-AND for example it is not an intrinsic function.

    The following program uses the B-SHIFT operators but I ask Simon if this example is correct.
    I believe that B-SHIFT operators should work like B-NOT i.e. with only one operand.
    However, if I put only one operand I get an error message from the compiler?!?!?
    By putting two operands with the B-SHIFT-L operator for example, I get a result that doesn't match.
    I think a clarification from Simon is needed.

           >>SOURCE FORMAT IS FREE
    IDENTIFICATION DIVISION.
    PROGRAM-ID. TESTBOOLEANSHIFT.
    DATA DIVISION.
    WORKING-STORAGE SECTION.
    01 A pic 9 comp-x value B'00000001'.
    01 B pic 9 comp-x value B'00000101'.
    01 c pic 9        value LOW-VALUE.
    
    PROCEDURE DIVISION.
      display space
      display '   a            b     c        a                   b          c    '
      display '  --- ------   ---   ---    -------- ---------- --------   --------'
      display '   '  a  '  initial   ' b '     ' c '     ' FUNCTION BIT-Of(a)   ' initial    ' FUNCTION BIT-Of(b) '   ' FUNCTION BIT-OF(c)
      display space   display space
      display '   a operator   b     c        a     operator      b          c    '
      display '  --- ------   ---   ---    -------- ---------- --------   --------'
      compute c = a B-SHIFT-L  b
      display '   '  a  ' B-SHIFT-L  ' b '  =  ' c '     '  FUNCTION BIT-Of(a)  ' B-SHIFT-L  ' FUNCTION BIT-Of(b) ' = ' FUNCTION BIT-OF(c)
      compute c = a B-SHIFT-LC b
      display '   '  a  ' B-SHIFT-LC ' b '  =  ' c '     '  FUNCTION BIT-Of(a)  ' B-SHIFT-LC ' FUNCTION BIT-Of(b) ' = ' FUNCTION BIT-OF(c)
      compute c = a B-SHIFT-R  b
      display '   '  a  ' B-SHIFT-R  ' b '  =  ' c '     '  FUNCTION BIT-Of(a)  ' B-SHIFT-R  ' FUNCTION BIT-Of(b) ' = ' FUNCTION BIT-OF(c)
      compute c = a B-SHIFT-RC b
      display '   '  a  ' B-SHIFT-RC ' b '  =  ' c '     '  FUNCTION BIT-Of(a)  ' B-SHIFT-RC ' FUNCTION BIT-Of(b) ' = ' FUNCTION BIT-OF(c)
    
      accept omitted
      STOP RUN.
    
     
  • Vincent (Bryan) Coen

    So what is the state of the other four namely
    B-AND
    B-OR
    B-XOR
    B-NOT

    These are currently in under functions so I "assume" that they are wrong as well and should only be in 2.2.4 -ish and removed from 8. Correct ?
    .

     
    • Eugenio Di Lorenzo

      There are no other boolean B-AND, B-OR ... in intrinsic functions chapter...
      They are all operators

      Please delete >> MORE text needed here? - PLACE HOLDER << and insert:

      Operands must be defined as PIC X(n) COMP-X or PIC 9(n) COMP-5.

       

      Last edit: Eugenio Di Lorenzo 2024-03-15
      • Vincent (Bryan) Coen

        OK, its me mixing BIT-xxxx with B-xxx.

        Please ignore.

        On 15/03/2024 17:46, Eugenio Di Lorenzo wrote:

        There are no other boolean B-AND, B-OR ... in intrinsic functions...
        They are all operators

        Attachments:

         
        • Eugenio Di Lorenzo

          Ok thanks, now I think it's ok.

          A small correction: the syntax you wrote

          [ Operand-1 ] { Boolean-Operator } [ Operand-2 ]

          it should be :

          [ Operand-1 ] { Boolean-Operator } Operand-2

          as Operand-2 must always be present.
          Only Operand-1 can be missing when using the B-NOT operator which requires only Operand-2.

           
          • Simon Sobisch

            Simon Sobisch - 2024-03-16

            I'd suggest to specify a choice instead:

            operand-1 boolean-operator operand-2
            B-NOT operand-3
            

            This way there are no optional operands and one does not have to make extra rules about B-NOT.

             
  • Vincent (Bryan) Coen

    Nag, nag, nag - it is all done - well until the next complaint :)

     
  • Vincent (Bryan) Coen

    • status: open --> closed
    • assigned_to: Vincent (Bryan) Coen
     

Log in to post a comment.