Arithmetic operators

++ post-increment operator

Purpose: Appending ++ to a variable will increase the value of the variable by 1.


Syntax:

 variable++

Parameters:

  • None

Remarks: If a post-incremented variable appears in an expression, the expression is evaluated using the current value of variable and then the variable is incremented by 1.

++ pre-increment operator

Purpose: Prepending ++ to a variable will increase the value of the variable by 1.


Syntax:

 ++variable

Parameters:

  • None

Remarks: If a pre-incremented variable appears in an expression, the variable is incremented by 1 and then the expression is evaluated.

-- post-decrement operator

Purpose: Appending -- to a variable will decrease the value of the variable by 1.


Syntax:

 variable--

Parameters:

  • None

Remarks: If a post-decremented variable appears in an expression, the expression is evaluated using the current value of variable and then the variable is decremented by 1.

-- pre-decrement operator

Purpose: Prepending -- to a variable will decrease the value of the variable by 1.


Syntax:

 --variable

Parameters:

  • None

Remarks: If a pre-decremented variable appears in an expression, the variable is decremented by 1 and then the expression is evaluated.

Example 1:


 DIM AS INTEGER x
 DIM AS INTEGER y
 
 ' Increment operators 
 ' Pre-increment: x is incremented by 1, then y is assigned the value of x 
 x = 1
 y = ++x    ' x is now 2, y is also 2 
 ?  x
 ?  y
 
 ' Post-increment: y is assigned the value of x, then x is incremented by 1 
 x = 1
 y = x++     ' y is 1, x is now 2 
 ?  x
 ?  y
 
 ' Decrement operators 
 ' Pre-decrement: x is decremented by 1, then y is assigned the value of x 
 x = 1
 y = --x     ' x is now 0, y is also 0 
 ?  x
 ?  y
 
 ' Post-decrement: y is assigned the value of x, then x is decremented by 1 
 x = 1
 y = x--     ' y is 1, x is now 0 
 ?  x
 ?  y

Result:


 2
 2
 2
 1
 0
 0
 0
 1

Example 2:


 DIM a
   
 a = 42
 ? a++ 'post increment(print a, 42, then a = a + 1)
 ? a     'print a, 43 

 ? ++a   'pre increment(a = a + 1, then print a, 44)
 ? a     'print a, 44 

 ? a-- 'post decrement(print a, 44, then a = a - 1)
 ? a     'print a, 43 

 ? --a   'pre decrement (a = a - 1, then print a, 42)
 ? a     'print a, 42


Result:


 42
 43
 44
 44
 44
 43
 42
 42

^ exponentiation operator

Example 1:


 DIM a, b

 b = -3
 a = 10 ^ b
 ?  a

Result:


 0.001

Example 2:


 DIM a#
 
 a# = 5 ^ 2
 PRINT a#
 a# = 25 ^ 0.5
 PRINT a#

Result:


 25
 5

Example 3:


  DIM cum#[1000]
  DIM tcont, pd#
  
  tcont = 1
  cum#[tcont] = 17
  pd# = 2
  PRINT(cum#[tcont] ^ (1 / ((tcont + 1) / pd#)) - 1) * 100
 
  PRINT 7 ^ (MOD(13, 7))
  PRINT(SIN(13)) ^ (SIN(13))
  PRINT pd ^ (pd ^ 2)
 
  PRINT pd ^ pd
  PRINT 7 ^ (IMOD(3,2))
 
  PRINT POW(2, POW(2, POW(2, 2)))
  PRINT 2 ^ (2 ^ (2 ^ (2)))
  PRINT pd ^ pd - pd ^ (LOG(pd)) - (SIN(pd)) ^ 7
 

Result:


 1600
 117649
 0.6946632571379008
 16
 4
 7
 65536
 65536
 1.869218636052765

Relational Operators

Relational operators are used to compare two values. The result of the comparison is either "true", nonzero, or "false", zero. This result can then be used to make a decision regarding program flow. Although BCX treats any nonzero value as true, true is usually represented by 1. When arithmetic and relational operators are combined in one expression, the arithmetic operations are always done first.


  Operator   Relation                 Expression

  ==         Equality                    X == Y
  EQUALTO    Equality                    X EQUALTO Y
  <>         Inequality                  X <> Y
  NOTEQUALTO Inequality                  X NOTEQUALTO Y
  <          Less than                   X < Y
  >          Greater than                X > Y
  <=         Less than or equal to       X <= Y
  >=         Greater than or equal to    X >= Y

Example:


 DIM RetStr$
 DIM c
 DIM d
   
 c = 42
 d = 6
 
 ? (d == c)
 
 ? (d EQUALTO c)
 
 ? (d <> c)
 
 ? (d NOTEQUALTO c)
 
 ? (d < c)
 
 ? (d > c)
 
 ? (d <= c)
 
 ? (d >= c)
 
 RetStr$ = BOOL$(d == c)
 ? RetStr$
 RetStr$ = BOOL$(d EQUALTO c)
 ? RetStr$
 RetStr$ = BOOL$(d <> c)
 ? RetStr$
 RetStr$ = BOOL$(d NOTEQUALTO c)
 ? RetStr$
 RetStr$ = BOOL$(d < c)
 ? RetStr$
 RetStr$ = BOOL$(d > c)
 ? RetStr$
 RetStr$ = BOOL$(d <= c)
 ? RetStr$
 RetStr$ = BOOL$(d >= c)
 ? RetStr$

Result:


  0
  0
  1
  1
  1
  0
  1
  0
 False
 False
 True
 True
 True
 False
 True
 False

In the following example, BCX will give a different answer than most dialects of BASIC.


 DIM RetVal%, A%, B%, C%
 
 RetVal% = 3 * ((A% >= B%) + (B% <= C%))
 
 ? RetVal%

Result:


 6

Using BCX, the example above returns an integer value of 6. Using QBASIC, the result is -6. The reason is because most BASIC dialects define TRUE as -1, but in the C language, TRUE is defined as 1. Each parenthetical evaluates to TRUE and in QBASIC, the statement reduces down to 3 * ( -1 + -1). In BCX, the statement reduces down to 3 * (1 + 1).

String Concatenation Operators + and &

A string expression consists of string constants, string variables, and other string expressions combined by string concatenation operators. The act of combining two strings is called concatenation.

In BCX, the plus + symbol or the ampersand & symbol may be used for concatenation.

For example, the following program concatenates the strings A$ and B$.


 DIM A$
 DIM B$

 A$ = "FILE"
 B$ = "NAME"
 PRINT A$ + B$
 PRINT "NEW " & A$ & B$

Result:


 FILENAME
 NEW FILENAME

Remarks:

String concatenation will not work with strings containing an embedded ASCII NULL.

End-Of-Line Continuation Operator _

The end-of-line continuation operator, _ , the underscore, may be used to split a long line of code into multiple lines. The underscore is placed at the end of each segment of the split line except for the last segment.

For example, this line of code


       ShellExecute(0, "open", "http://www.w3.org", "", 0, 1)

could have comments added explaining the parameters and be segmented into this


       ShellExecute(0, _  ' handle to parent window 
               "open", _  ' pointer to string that specifies operation to perform 
  "http://www.w3.org", _  ' pointer to filename string 
                   "", _  ' pointer to string that specifies executable-file parameters 
                    0, _  ' pointer to string that specifies default directory 
                    1)   ' whether file is shown when opened 
 

Remarks:

The underscore, usually, is separated by a space from the last character in the line segment. The prepended space is not necessary when the underscore is formed in the following 2-character pairs.


 ,_
 
 ;_
 
 :_
 
 )_
 
 ]_
 
 >_
 
 $_ 

Example:

The following appears in an internal BCX routine:


 SELECT CASE sWord$
 CASE _
  "function",_
  "sub",_
  "publicfunction",_
  "publicsub",_
  "privatefunction",_
  "privatesub"

String Comparison Operators

String comparisons are made by taking corresponding characters from each string operand and comparing their ASCII codes. If the ASCII codes are the same for all the characters in both strings, the strings are equal. If the ASCII codes differ, the lower code number precedes the higher. If the end of one string is reached during string comparison, the shorter string is smaller if they are equal up to that point. Leading and trailing blanks are significant.

Strings can be compared using the following relational operators:


  Operator   Relation                 Expression

      =     Equality                    X$ = Y$
      <>    Inequality                  X$ <> Y$
      <     Less than                   X$ < Y$
      >     Greater than                X$ > Y$


 <= and >= cannot be used to compare case sensitive strings.

Example 1:


 DIM S AS STRING 
 DIM T AS STRING
 S="Test"
 T="Test"
 IF T$ = S$ THEN 
   PRINT " S equals T"
 ELSE
   PRINT " S does not equal T"
 END IF

Result:


 S equals T

Example 2:


 IF "BCX"   = "bcx" THEN PRINT "True" ELSE PRINT "False"
 IF "BCX"  != "bc" THEN PRINT "True" ELSE PRINT "False"
 IF "BCX" =!  "bc" THEN PRINT "True" ELSE PRINT "False"
 IF "BCX" NOT = "bc" THEN PRINT "True" ELSE PRINT "False"
 IF "BCX" <>   "bc" THEN PRINT "True" ELSE PRINT "False"
 IF "BCX"  >   "bc" THEN PRINT "True" ELSE PRINT "False"
 IF "BCX"  <   "bc" THEN PRINT "True" ELSE PRINT "False"

Result:


 False
 True
 True
 True
 True
 False
 True

String Relational Operator ??

The string relational operator ?? is used to perform an efficient, locale-aware, case-insensitive string comparison in IF...THEN and DO/WHILE/UNTIL/LOOP's.

Example 1:


 DIM AS STRING szNames1,szNames2
 
 szNames1  = "BCX"
 szNames2 = "bcx"
 
 IF szNames1$ ?? szNames2$ THEN
   ?  "Strings are equal!"
 ELSE
   ?  "Strings are not equal!"
 ENDIF
 
 PAUSE

Result:


 Strings are equal!

Example 2:


 IF "BCX"    ?? "bcx" THEN PRINT "True" ELSE PRINT "False"
 IF "BCX"   >?? "bc" THEN PRINT "True" ELSE PRINT "False"
 IF "BCX"    ??> "bc" THEN PRINT "True" ELSE PRINT "False"
 IF "BCX"   <?? "bc" THEN PRINT "True" ELSE PRINT "False"
 IF "BCX"    ??< "bc" THEN PRINT "True" ELSE PRINT "False"
 IF "BCX"   <??> "bc" THEN PRINT "True" ELSE PRINT "False"
 IF "BCX"   !?? "bc" THEN PRINT "True" ELSE PRINT "False"
 IF "BCX"    ??! "bc" THEN PRINT "True" ELSE PRINT "False"
 IF "BCX" NOT ?? "bc" THEN PRINT "True" ELSE PRINT "False"

Result:


 True
 True
 True
 False
 False
 True
 True
 True
 True

Example 2:


 DIM A$, i            
 i = 65              
 WHILE A$ !?? "AbCdEf"
   A$ = A$ + CHR$(i++)
   ?  A$               
 WEND                 

Result:


 A
 AB
 ABC
 ABCD
 ABCDE
 ABCDEF

Example 3:


 DIM i, A$, B$
 B$ = "AAAAA"
 DO UNTIL A$ >?? B$   ' until A$ is greater than or equal to B$ 
   A$ = A$ + "A"
 LOOP
 PRINT A$

Result:


 AAAAAA

Remarks:

The raison d'être for the ?? operator is to simplify code from this:


 IF LCASE$(SomeString$) = LCASE$("I like Cocoa Puffs") THEN ...  

to this

 IF SomeString$ ?? "I like Cocoa Puffs" THEN ...

This is accomplished by the ?? operator instructing BCX to emit the unique bcx_stricmp function instead of strcmp.

As with the = sign, the <, >, <>, and NOT operators may also be used.

You may liberally use whitespace between the symbols: ? < > !.

Bitwise and Logical operators

AND operator

In BCX, the AND operator can function as a logical operator as well as a bitwise operator.

In BCX, within IF ... THEN statements and $IF ... $ENDIF preprocessor directives, AND is tranlated to the 'C' code logical AND operator &&.

Outside IF ... THEN statements and $IF ... $ENDIF preprocessor directives, AND is tranlated to the 'C' code bitwise AND operator &.

Because there are corner cases and ambiguities where both logical and bitwise AND operations may coexist, it is best not to assume that BCX will correctly decipher what you're trying to do and translate AND to the correct 'C' code logical or bitwise operator.

As a general rule, it is best to use the explicitly logical BCX LAND operator instead of AND.

The same rule applies to bitwise operations. It is best to use the explicitly bitwise BCX BAND operator instead of AND.

The Truth Table of AND


 ----------------------------
  Operand1  Operand2  Result
 ----------------------------
    1         1        1
    1         0        0
    0         1        0
    0         0        0
 ----------------------------

It is important not to conflate Bitwise with Logical operators.

They are different but sometimes can produce the same result.

Example 1:


    DIM AS INTEGER R
    DIM AS BOOL P, Q
   ? 
    P = TRUE
    PRINT "P is ", BOOL$(P)
    Q = TRUE
    PRINT "Q is ", BOOL$(Q)
    R = P AND Q
    PRINT "P AND Q is ", BOOL$(R)
    R = P BAND Q
    PRINT "P BAND Q is ", BOOL$(R)
   ? 
    P = TRUE
    PRINT "P is ", BOOL$(P)
    Q = FALSE
    PRINT "Q is ", BOOL$(Q)
    R = P AND Q
    PRINT "P AND Q is ", BOOL$(R)
    R = P BAND Q
    PRINT "P BAND Q is ", BOOL$(R)
   ? 
    P = FALSE
    PRINT "P is ", BOOL$(P)
    Q = TRUE
    PRINT "Q is ", BOOL$(Q)
    R = P AND Q
    PRINT "P AND Q is ", BOOL$(R)
    R = P BAND Q
    PRINT "P BAND Q is ", BOOL$(R)
   ? 
    P = FALSE
    PRINT "P is ", BOOL$(P)
    Q = FALSE
    PRINT "Q is ", BOOL$(Q)
    R = P AND Q
    PRINT "P AND Q is ", BOOL$(R)
    R = P BAND Q
    PRINT "P BAND Q is ", BOOL$(R)
   
    PAUSE

Result:


 P is True
 Q is True
 P AND Q is True
 P BAND Q is True

 P is True
 Q is False
 P AND Q is False
 P BAND Q is False

 P is False
 Q is True
 P AND Q is False
 P BAND Q is False

 P is False
 Q is False
 P AND Q is False
 P BAND Q is False

 Press any key to continue . . ..

As can be seen in Example 1, above, the Truth Table is the same for logical AND, as well as, bitwise AND operators.

In Example 2, below,
the BCX AND 'C' language equivalent, "logical and" &&,
as well as,
the BCX BAND 'C' language equivalent, "bitwise and", &,
operators are operating on the same operands.

In the First comparison, &, as well as, && produce the same result.

The Second comparison does not.

Example 2:


  DIM AS INTEGER R, P, Q

 ? 
  ' First comparison
  P = 0
  PRINT " P is ", P
  Q = 1
  PRINT " Q is ", Q
 ! R = P && Q;
  PRINT " P && Q is ", R
 ! R = P & Q;
  PRINT " P &  Q is ", R

 ? 
  ' Second comparison
  P = 4
  PRINT " P is ", P
  Q = 2
  PRINT " Q is ", Q
 ! R = P && Q;
  PRINT " P && Q is ", R
 ! R = P & Q;
  PRINT " P &  Q is ", R
 
  PAUSE

Result:


 P is  0
 Q is  1
 P && Q is  0
 P &  Q is  0

 P is  4
 Q is  2
 P && Q is  1
 P &  Q is  0

Press any key to continue . . .

Use 'C' language Boolean / Logical operator symbols only in $CCODE demarcated areas, or on lines with inline 'C' code using the ! operator. Do not mix 'C' language Boolean / Logical operator symbols with BCX code.

Logical AND operator

Purpose: The logical AND operator performs a logical conjunction on two scalar operands. The functionality of logical AND is equivalent to the C language && operator which evaluates the second operand only if the first evaluates as nonzero.

ANDALSO and LAND are alias for the logical AND operator.


Syntax 1: Logical

 RetVal = Operand1 AND Operand2

Syntax 2: Logical

 RetVal = Operand1 ANDALSO Operand2

Syntax 3: Logical

 RetVal = Operand1 LAND Operand2

Return Value:

  • Data type: INTEGER
    RetVal The value is 1 if both operands are not 0, otherwise the value is 0.

Parameters:

  • Data type: Scalar
    Operand1 Operand to be evaluated with Operand2.
  • Data type: Scalar
    Operand2 Operand to be evaluated with Operand1.

Bitwise AND operator

Purpose: The bitwise AND operator performs a logical conjunction on two integer operands. The functionality of bitwise AND is equivalent to the C language & operator which compares corresponding bits in two operands and sets the corresponding bit in the result to 1 if both bits are 1.

BAND is an alias for the bitwise inclusive AND operator.


Syntax 1: Bitwise

 RetVal = Operand1 AND Operand2 

Syntax 2: Bitwise

 RetVal = Operand1 BAND Operand2

Return Value:

  • Data type: INTEGER
    RetVal Value of Operand1 ANDed with Operand2.

Parameters:

  • Data type: INTEGER
    Operand1 Number compared bitwise with Operand2.
  • Data type: INTEGER
    Operand2 Number compared bitwise with Operand1.

Remarks:

The BAND operator uses this "truth table":


 --------------------------------------------------------
  Bit in Expression1  Bit in Expression2  Bit in Result
 --------------------------------------------------------
        1                 1                 1
        1                 0                 0
        0                 1                 0
        0                 0                 0
 --------------------------------------------------------

Example 1:


 DIM AS INTEGER int1, int2, int3
 DIM BINstr1$
  
 int1 = 12345
 BINstr1$ = LPAD$(BIN$(int1),32, ASC("0"))
 PRINT BINstr1$
 
 int2 = 67890
 BINstr1$ = LPAD$(BIN$(int2),32, ASC("0"))
 PRINT BINstr1$
  
 int3 = int1% BAND int2
 BINstr1$ = LPAD$(BIN$(int3),32, ASC("0"))
 PRINT BINstr1$

Result:


 00000000000000000011000000111001
 00000000000000010000100100110010
 00000000000000000000000000110000

Example 2:

The following program demonstrates the use of BAND to determine whether a number is even or odd.


 FOR INTEGER it = 0 TO 9
   IF it BAND 1 THEN
     PRINT it, " is odd."
   ELSE
     PRINT it, " is even."
   END IF
 NEXT it

Result:


 0 is even.
 1 is odd.
 2 is even.
 3 is odd.
 4 is even.
 5 is odd.
 6 is even.
 7 is odd.
 8 is even.
 9 is odd.

OR operator

In BCX, the OR operator can function as a logical operator as well as a bitwise operator.

In BCX, within IF ... THEN statements, as well as, $IF ... $ENDIF preprocessor directives, OR is tranlated to the 'C' code logical OR operator ||.

Outside IF ... THEN statements and $IF ... $ENDIF preprocessor directives, OR is tranlated to the 'C' code bitwise OR operator |.

Because there are corner cases and ambiguities where both logical and bitwise OR operations may coexist, it is best not to assume that BCX will correctly decipher what you're trying to do and translate OR to the correct 'C' code logical or bitwise operator.

As a general rule, it is best to use the explicitly logical BCX LOR operator instead of OR.

The same rule applies to bitwise operations. It is best to use the explicitly bitwise BCX BOR operator instead of OR.

The Truth Table of OR


 ----------------------------
  Operand1  Operand2  Result
 ----------------------------
    1         1         1
    1         0         1
    0         1         1
    0         0         0
 ----------------------------

Example:


   DIM AS INTEGER R
   DIM AS BOOL P, Q
  ? 
   P = TRUE
   PRINT "P is ", BOOL$(P)
   Q = TRUE
   PRINT "Q is ", BOOL$(Q)
   R = P OR Q
   PRINT "P OR Q is ", BOOL$(R)
  ? 
   P = TRUE
   PRINT "P is ", BOOL$(P)
   Q = FALSE
   PRINT "Q is ", BOOL$(Q)
   R = P OR Q
   PRINT "P OR Q is ", BOOL$(R)
  ? 
   P = FALSE
   PRINT "P is ", BOOL$(P)
   Q = TRUE
   PRINT "Q is ", BOOL$(Q)
   R = P OR Q
   PRINT "P OR Q is ", BOOL$(R)
  ? 
   P = FALSE
   PRINT "P is ", BOOL$(P)
   Q = FALSE
   PRINT "Q is ", BOOL$(Q)
   R = P OR Q
   PRINT "P OR Q is ", BOOL$(R)
  
   PAUSE

Result:


 P is True
 Q is True
 P OR Q is True

 P is True
 Q is False
 P OR Q is True

 P is False
 Q is True
 P OR Q is True

 P is False
 Q is False
 P OR Q is False

 Press any key to continue . . .

Logical OR operator

Purpose: The logical OR operator performs a inclusive disjunction on two operands. The functionality of logical OR is equivalent to the C language || operator which evaluates the second operand only if the first evaluates as zero.

ORELSE and LOR. are alias for the logical OR operator


Syntax 1: Logical

 RetVal = Operand1 OR Operand2

Syntax 2: Logical

 RetVal = Operand1 ORELSE Operand2

Syntax 3: Logical

 RetVal = Operand1 LOR Operand2

Return Value:

  • Data type: INTEGER
    RetVal Value 0 (zero) if both operands evaluate to 0 (zero), otherwise, the return value is a 1 (one).

Parameters:

  • Data type: Scalar
    Operand1 Operand to be evaluated with Operand2.
  • Data type: Scalar
    Operand2 Operand to be evaluated with Operand1.

Bitwise inclusive OR operator

Purpose: The bitwise inclusive OR operator compares corresponding bits in two operands and sets the corresponding bit in the result to 1 if either bit is 1.

BOR is an alias for the bitwise inclusive OR operator.


Syntax 1: Bitwise

 RetVal = Operand1 OR Operand2

Syntax 2: Bitwise

 RetVal = Operand1 BOR Operand2

Return Value:

  • Data type: INTEGER
    RetVal Value of Operand1 ORed with Operand2.

Parameters:

  • Data type: INTEGER
    Operand1 Number compared bitwise with Operand2.
  • Data type: INTEGER
    Operand2 Number compared bitwise with Operand1.

Remarks:

The bitwise BOR operator uses this "truth table":


 --------------------------------------------------------
  Bit in Expression1  Bit in Expression2  Bit in Result
 --------------------------------------------------------
        1                 1                 1
        1                 0                 1
        0                 1                 1
        0                 0                 0
 --------------------------------------------------------

Example:


 DIM AS INTEGER int1, int2, int3
 DIM BINstr1$
  
 int1 = 12345
 BINstr1$ = LPAD$(BIN$(int1),32, ASC("0"))
 PRINT BINstr1$
 
 int2 = 67890
 BINstr1$ = LPAD$(BIN$(int2),32, ASC("0"))
 PRINT BINstr1$
  
 int3 = int1% BOR int2
 BINstr1$ = LPAD$(BIN$(int3),32, ASC("0"))
 PRINT BINstr1$

Result:


 00000000000000000011000000111001
 00000000000000010000100100110010
 00000000000000010011100100111011

Logical NOT operator

Purpose: NOT negates the Boolean value of an operand.


Syntax:

 RetVal = NOT Operand

Return Value:

  • Data type: INTEGER
    RetVal 0 is returned if the logical value of the operand does not evaluate to 0, otherwise, the return value is a 1.

Parameters:

  • Data type: BOOL
    Operand Boolean value of Operand, 0 (zero) or 1 (one).

Remarks:

Example:


  DIM AS BOOL Proposition
  
  Proposition = TRUE
  PRINT "Proposition is ", BOOL$(Proposition)
 
  Proposition = NOT Proposition
  PRINT "Proposition is ", BOOL$(Proposition)
 
  Proposition = NOT Proposition
  PRINT "Proposition is ", BOOL$(Proposition)

Result:


 Proposition is True
 Proposition is False
 Proposition is True

Bitwise BNOT operator

Purpose: BNOT is a bitwise inversion operator, commonly known as "One's Complement". BNOT inverts binary bits in an integer expression to 0 (zero) if 1 (one) and to 1 (one) if 0 (zero).


Syntax:

 RetVal = BNOT Operand

Return Value:

  • Data type: INTEGER
    RetVal Bit inverted Operand.

Parameters:

  • Data type: INTEGER
    Operand Number to be bit inverted.

Remarks:

 The BNOT operator uses this "truth table":


 ---------------------------------------------------------
  Bit in Expression  Bit in Result
 ---------------------------------------------------------
      1                 0
      0                 1
 ---------------------------------------------------------

Example:


 DIM int1%, int2%
 DIM BINstr1$, BINstr2$
 
 int1% = 12345
 BINstr1$ = LPAD$(BIN$(int1%),32, ASC("0"))
 PRINT BINstr1$
 
 int2% = BNOT int1%
 BINstr2$ = BIN$(int2%)
 PRINT BINstr2$

Result:


 00000000000000000011000000111001
 11111111111111111100111111000110

Bitwise exclusive XOR operator

Purpose: The bitwise XOR operator compares corresponding bits in two operands and sets the corresponding bit in the result to 1 if the bits differ.


Syntax:

 RetVal = Operand1 XOR Operand2

Return Value:

  • Data type: INTEGER
    RetVal Value of Operand1 XORed with Operand2

Parameters:

  • Data type: INTEGER
    Operand1 Number to be XORed with Operand2.
  • Data type: INTEGER
    Operand2 Number to be XORed with Operand1.

Remarks:

The bitwise XOR operator uses this "truth table":


 ---------------------------------------------------------
  Bit in Expression1  Bit in Expression2  Bit in Result
 ---------------------------------------------------------
        1                 1                 0
        1                 0                 1
        0                 1                 1
        0                 0                 0
 ---------------------------------------------------------

Example 1:


 DIM AS INTEGER int1, int2, int3
 DIM BINstr1$
  
 int1 = 12345
 BINstr1$ = LPAD$(BIN$(int1),32, ASC("0"))
 PRINT BINstr1$
 
 int2 = 67890
 BINstr1$ = LPAD$(BIN$(int2),32, ASC("0"))
 PRINT BINstr1$
  
 int3 = int1% XOR int2
 BINstr1$ = LPAD$(BIN$(int3),32, ASC("0"))
 PRINT BINstr1$

Result:


 00000000000000000011000000111001
 00000000000000010000100100110010
 00000000000000010011100100001011

Example 2:


 DIM Counter%, LenStr1%
 DIM Str1$
 
 Str1$ = "tI esreveR"
 LenStr1% = LEN(Str1$) - 1
 
 DO WHILE Counter% < LenStr1%
   Str1[Counter] = Str1[Counter] XOR Str1[LenStr1]
   Str1[LenStr1] = Str1[LenStr1] XOR Str1[Counter]
   Str1[Counter] = Str1[Counter] XOR Str1[LenStr1]
   LenStr1--
   Counter++
 LOOP
 
 PRINT Str1$

Result:


 Reverse It

Remarks:

The most rapid string reversal implementation is found in the BCX REVERSE$ function.

Bit Shift operators

Purpose: These operators shift the first integer expression left or right by the number of places in the second integer expression.

SHL left shift operator


Syntax:

 RetVal = IntNum SHL NumPlaces

Return Value:

  • Data type: INTEGER
    RetVal IntNum shifted left NumPlaces.

Parameters:

  • Data type: INTEGER
    IntNum Number to be shifted left. 
  • Data type: INTEGER
    NumPlaces Number of places to the left that IntNum is to be shifted.

Remarks: Vacated right bits are set to 0.

SHR right shift operator


Syntax:

 RetVal% = IntNum AS INTEGER SHR NumPlaces AS INTEGER

Return Value:

  • Data type: INTEGER
    RetVal Returned integer value.

Parameters:

  • Data type: INTEGER
    IntNum Integer number to be shifted right.
  • Data type: INTEGER
    NumPlaces Number of places to the right that IntNum% is to be shifted.

Remarks: Vacated left bits are set to 0 if the integer type is unsigned. Otherwise, they are filled with copies of the sign bit.

Example:


 DIM value%

 value% = 32768 SHL 1 'Shift left 1 place
 PRINT value% 
 
 value% = 32768 SHR 1 'Shift right 1 place
 PRINT value% 

Result:


 65536
 16384

Example 2:


   $COMMENT
    --------------------------------------------------------------------------------
    FUNctions with bits!  by Jeff Nope  jeffnope@hotmail.com
    Feel free to use these, you just can't hold me responsible,
    and give credit where credits due :-)
    --------------------------------------------------------------------------------
    All BitNum's are zero based
    --------------------------------------------------------------------------------
    a = BitClr(a, BitNum)  
    
    Clears a particular bit in variable a (to a value of 0).
    --------------------------------------------------------------------------------
    a = BitSet(a, BitNum)  
    
    Sets a particular bit in variable a (to a value of 1).
    --------------------------------------------------------------------------------
    b = BitTst(a, BitNum)  
    
    Determines whether a given bit is set in a.
    
    function result
    b = TRUE if the specified bit is set (that is, has a value of 1) and
    b = FALSE if the bit is cleared (that is, has a value of 0).
    --------------------------------------------------------------------------------
    a = BitTgl(a, BitNum)
    
    Toggles a particular bit in variable a
    --------------------------------------------------------------------------------
    z$ = dec2bin$(a, length)
    Converts variable a into a string representing the binary value of a. 
    The length variable is optional for formating, for example 8 gives a byte.
    This function is the opposite of BIN2DEC(z$) a BCX built in function.
    --------------------------------------------------------------------------------
   $COMMENT
     
 DIM a, b, i
 DIM z$
 z$ = "101001"
     
 ?  "z$ = " & z$
 ?  "a = BIN2DEC (z$)"
 a = BIN2DEC (z$) 'BCX built in function 
 ?  "a =" & a
     
 ?
     
 ? "z$ = dec2bin$(a, 8) (with optional length):"
 z$ = dec2bin$(a, 8) 'the optional length of 8, will cause leading zero's here 
 ?  "z$ = " & z$ & "b"
     
 ?
     
 ? "z$ = dec2bin$(a) (without the optional length):"
 z$ = dec2bin$(a)
 ?  "z$ = " & z$ & "b"
     
 ?
     
 ?  "a = BitSet(a, 1)"
 a = BitSet(a, 1)   'set bit 1 in variable a 
 z$ = dec2bin$(a, 8) 'the optional length of 8, will cause leading zero's here 
 ?  "a = " & a & " : " & z$ & "b"
     
 ?
     
 ?  "a = BitClr(a, 1)"
 a = BitClr(a, 1)
 z$ = dec2bin$(a, 8) 'the optional length of 8, will cause leading zero's here 
 ?  "a = " & a & " : " & z$ & "b"
     
 ?
     
 ?  "a = BitTgl(a, 1)"
 a = BitTgl(a, 1)   'make sure it can toggle a bit 
 z$ = dec2bin$(a, 8) 'the optional length of 8, will cause leading zero's here 
 ?  "a = " & a & " : " & z$ & "b"
     
 ?
     
 ?  "a = BitTgl(a, 1)"
 a = BitTgl(a, 1)   'make sure it can toggle a bit back 
 z$ = dec2bin$(a, 8) 'the optional length of 8, will cause leading zero's here 
 ?  "a = " & a & " : " & z$ & "b"
     
 ?
     
     
 ?  "b = BitTst(a, i) loop:"
 FOR i = 7 TO 0 STEP -1 'loop for 8 bits starting at the MSB 
   b = BitTst(a, i)    'Test the i'th bit in a 
   ? b;                 'print the bits one at a time 
 NEXT
     
 END
     
 FUNCTION BitTst(aTst, BitNum) 'BitNum is zero based 
   DIM tmp
   tmp = aTst BAND 1 SHL BitNum
   IF tmp > 0 THEN tmp = 1
   FUNCTION = tmp
 END FUNCTION
     
 FUNCTION BitSet(aSet, BitNum) 'BitNum is zero based 
   DIM tmp
   tmp = aSet BOR 1 SHL BitNum
   FUNCTION = tmp
 END FUNCTION
     
 FUNCTION BitClr(aClr, BitNum) 'BitNum is zero based 
   DIM tmp
   tmp = aClr BAND ((1 SHL BitNum) XOR 0xFFFFFFFF) 'the XOR with all 1's causes all bits to toggle 
   FUNCTION = tmp
 END FUNCTION
     
 FUNCTION BitTgl(aTgl, BitNum) 'BitNum is zero based 
   DIM tmp
   tmp = aTgl XOR 1 SHL BitNum 'the XOR with 1 causes bit to toggle 
   FUNCTION = tmp
 END FUNCTION
     
 FUNCTION dec2bin$ OPTIONAL (ad2b, length = 0) 'optionally formated 
   DIM i, tmp, t, T$
     
   IF length = 0 THEN 'we don't know the length here 
     tmp = ad2b
     WHILE tmp > 0
       tmp = tmp SHR 1
       length++
     WEND
   END IF
     
   t = 1
     
   FOR i = 1 TO length 'we know the length here 
     tmp = ad2b BAND t    'logical AND to test the bit 
     IF tmp = 0 THEN
       T$ = "0" & T$
     ELSE
       T$ = "1" & T$
     END IF
     t = t SHL 1
   NEXT
     
   FUNCTION = T$
 END FUNCTION

Result:


 z$ = 101001
 a = BIN2DEC (z$)
 a = 41

 z$ = dec2bin$(a,8) (with optional length):
 z$ = 00101001b

 z$ = dec2bin$(a) (without the optional length):
 z$ = 101001b

 a = BitSet(a,1)
 a =  43 : 00101011b

 a = BitClr(a,1)
 a =  41 : 00101001b

 a = BitTgl(a,1)
 a =  43 : 00101011b

 a = BitTgl(a,1)
 a =  41 : 00101001b

 b = BitTst(a,i) loop:
  0 0 1 0 1 0 0 1

Assignment Operators

= assignment operator

The assignment operator, =, assigns a value to a variable.

In BCX, a literal decimal numeric value can be assigned to a variable, as in this example


 DIM Number
 
 Number = 20200628
 
 PRINT Number

Result:


 20200628

In BCX, a literal hexadecimal numeric value can be assigned to a variable, as in this example


 DIM Number
 
 Number = 0x1343CB4
 
 PRINT Number

Result:


 20200628

In BCX, a literal octal numeric value can be assigned to a variable, as in this example


 DIM Number
 
 Number = %0115036264
 
 PRINT Number

Result:


 20200628

In BCX, a literal binary numeric value can be assigned to a variable, as in this example


 $BCXVERSION "7.4.7 (2020/06/29)"

 DIM Number
 
 Number = 0b00000001001101000011110010110100
 
 PRINT Number

Result:


 20200628

Remarks:

The keyword IS can be used in place of the = equality assignment operator, as in this example


 DIM Number
 
 Number IS 20200628
 
 PRINT Number

Result:


 20200628

The LET keyword can precede statements containing the, =, equality assignment operator.


 DIM Number
 
 LET Number = 20200628
 
 PRINT Number

Result:


 20200628

+= plus equals operator

Purpose: A shorthand form of variable = variable + numeric-expression.


Syntax:

 variable += numeric-expression

Parameters:

  • None

-= minus equals operator

Purpose: A shorthand form of variable = variable - numeric-expression.


Syntax:

 variable -= numeric-expression

Parameters:

  • None

*= multiplied by equals operator

Purpose: A shorthand form of variable = variable * numeric-expression.


Syntax:

 variable *= numeric-expression

Parameters:

  • None

/= divided by equals operator

Purpose: A shorthand form of variable = variable / numeric-expression.


Syntax:

 variable /= numeric-expression

Parameters:

  • None

Example:


 DIM a
 a = 42
 
 a += 2   'same as a = a + 2
 ? a
 
 a -= 2   'same as a = a - 2
 ? a
 
 a *= 2   'same as a = a * 2
 ? a
 
 a /= 2   'same as a = a / 2
 ? a

Result:


 44
 42
 84
 42

Order of Operations

When several BCX operators occur in the same statement, they are executed in the following order:

Order
of
Operations
Operator
Symbol
Operation Direction
of
Evaluation
1st () Parentheses or function call Left to right
[] Array element
. TYPE or UNION member
-> Pointer reference to member
++ Post increment variable++
-- Post decrement variable--
2nd SIZEOF Size of object in bytes Right to left
& Address of
* Contents of
+ Unary plus
- Unary minus
~ Bitwise complement ("ones complement")
BNOT Bitwise complement ("ones complement")
! Logical complement ("Negation")
NOT Logical complement ("Negation")
++ Pre increment ++variable
-- Pre decrement --variable
3rd (Data Type) Cast (C-style type conversion) Right to left
4th * Multiply Left to right
/ Divide
% Remainder
5th + Add Left to right
- Subtract
6th SHL Bitwise left shift Left to right
SHR Bitwise right shift
7th < Scalar less than Left to right
<= Scalar less than or equal to
> Scalar greater than
>= Scalar greater than or equal to
8th = Scalar equal Left to right
EQUALTO Scalar equal
<> Scalar not equal
>< Scalar not equal
NOTEQUALTO Scalar not equal
9th BAND Bitwise conjunction on two binary integers Left to right
& Bitwise conjunction on two binary integers
10th XOR Exclusive logical disjunction on two expressions. Left to right
11th BOR Bitwise disjunction on two binary integers Left to right
| Bitwise disjunction on two binary integers
12th AND Logical conjunction on two Boolean expressions Left to right
ANDALSO Logical conjunction on two Boolean expressions
&& Logical conjunction on two Boolean expressions
13th OR Logical disjunction on two Boolean expressions Left to right
ORELSE Logical disjunction on two Boolean expressions
|| Logical disjunction on two Boolean expressions
14th = Direct assignment Right to left
*= Assignment by product
/= Assignment by quotient
+= Assignment by sum
-= Assignment by difference
SHL= Assignment by Bitwise left shift
SHR= Assignment by Bitwise right shift
&= Assignment by Bitwise AND
|= Assignment by Bitwise OR
15th , Sequential expression Left to right

If the operations are different and are of the same level, the leftmost one is executed first and the rightmost last.

The order of operations in the following example


 DIM a%
 
 a% = 42 + 6 * 4 / 2 - 1
 
 PRINT a%

is as follows:


 1. 6 * 4 (= 24)
 2. 24 / 2 (= 12)
 3. 12 + 42 (= 54)
 4. 54 - 1 (= 53)

The above example can be expressed unambiguously, with parentheses, as


 DIM a%
 
 a% = (42 + ((6 * 4) / 2)) - 1
 
 PRINT a%

BCX does not support the following assignment operators


 ^=  assignment by bitwise XOR
 
 %=  assignment by remainder.
  

In BCX the ^ symbol is used as the exponentiation operator, while in the C language, the ^ symbol is used for the bitwise exclusive or (XOR) operator. The shortcut %= does not work because, in BCX the % symbol is used as the data type identifier appended to an INTEGER variable. To use either, inline C code must be used, for example,


 ! a^=2;
 
 ! a%=2;

BCX does not support the \ (backslash) integer division operator.


 Dividend \ Divisor

In some other BASIC dialects an integer division operation retains the integer (whole-number) part of the division and discards the fractional part. In QuickBASIC, the integer division operation rounds off the numbers to be operated on before the calculation is performed.

In this QuickBASIC code


 PRINT 9.6 \ 2.4

the number 9.6 is rounded up to 10 and 2.4 is rounded down to 2. Therefore, the result of the example above is 5, not 4.

Here is a function snippet to perform QuickBASIC style integer division in BCX.


 PRINT INTDIV(9.6, 2.4)
 
 FUNCTION INTDIV(dividend AS DOUBLE, divisor AS DOUBLE) AS INTEGER
   FUNCTION = INT(ROUND(dividend, 0) / ROUND(divisor, 0))
 END FUNCTION

Result:


 5