# Operators

## 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 postdecremented 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 predecremented 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#
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

Note: The keyword IS can be used in place
of the = equality relational operator.

IS     Equality                    X IS Y

The LET keyword can precede statements
containing the equality relational operator(=).

LET A% = 6 * 6

```

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).

Example:

```
DIM c
DIM d

c = 42
d = 6

IF d < c  THEN ? "It works!"
IF d > c  THEN ? "error"
IF d <> c THEN ? "It works!"
IF d = c  THEN ? "error"
IF d <= c THEN ? "It works!"
IF d >= c THEN ? "error"

```

Result:

```
It works!
It works!
It works!

```

### 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 fragment combines the string variables A\$ and B\$ to produce the value FILENAME:

```
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. The underscore must be separated by a space from the last character in the line 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 folder
1)    ' whether file is shown when opened

```

The end-of-line continuation operator will not work within a quoted literal string. For example,

```
\$ONEXIT "echo Do not _
do this !"

```

will cause the following BCX translator generated error

```
Error!
D:\t\snip.bas
Unmatched Quotes at line 4 in Module: snip.bas
Original line
\$ONEXIT "echo Do not _
==============
Current Tokens
==============
1 \$ONEXIT
2 "echo Do not _
is a STRING LITERAL
===============
Original Tokens
===============
1 \$ONEXIT
2 "echo Do not _
is a string literal

```

### 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

```

\$CASE_OFF and \$CASE_ON directives

Purpose: When performing string comparisons, \$CASE_OFF changes the default behavior to case insensitive until BCX encounters a \$CASE_ON statement which restores the default, case sensitive, behavior.

 Syntax 1: ``` string comparisons are case sensitive \$CASE_OFF string comparisons are case insensitive \$CASE_ON string comparisons are case sensitive ```

Remarks: Case sensitive string comparisons are the default in all BASIC language dialects.

Example:

```
IF "dog" = "DoG" THEN PRINT "Dogs are equal" ELSE PRINT "Dogs are not equal"

\$CASE_OFF  ' Turn off case-sensitivity when comparing strings
IF "dog" = "DoG" THEN PRINT "Dogs are equal" ELSE PRINT "Dogs are not equal"

\$CASE_ON   ' Turn on case-sensitivity when comparing strings
IF "dog" = "DoG" THEN PRINT "Dogs are equal" ELSE PRINT "Dogs are not equal"

```

Result:

```
Dogs are not equal
Dogs are equal
Dogs are not equal

```

### 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\$) = "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: ? < > !.

## Logical operators

The BCX NOT, AND, OR, ANDALSO, and ORELSE logical operators perform byte-level operations on operands which are considered to have only one value, either FALSE, that is, equal to zero, or TRUE, not equal to zero.

NOT operator

Purpose: NOT is used to check the logical negation of its operand, returning TRUE if the operand is FALSE, and FALSE if the operand is TRUE.

 Syntax: ``` BoolVal = NOT Operand ``` Parameters: BoolVal Returned value, zero or not zero. Operand Operand to be evaluated.

Remarks:

A space must be before and after the NOT operator.

Example:

```
DIM AS BOOL bResult1, bResult2, bX1, bX2
bX1 = TRUE
bX2 = FALSE
bResult1 = NOT bX1
PRINT bResult1
bResult2 = NOT bX2
PRINT bResult2

```

Result:

```
0
1

```

AND operator

Purpose: The AND logical operator performs a logical conjunction on two operands returning TRUE if both operands evaluate to TRUE otherwise returning FALSE.

 Syntax: ``` BoolVal = Operand1 AND Operand2 ``` Parameters: BoolVal Returned value, zero or not zero. Operand1 Operand to be evaluated with Operand2.

Remarks:

A space must be before and after the AND operator.

Example:

```
DIM AS BOOL bResult1, bResult2, bX1, bX2
bX1 = TRUE
bX2 = FALSE
bResult1 = bX1 AND bX2
PRINT bResult1
bX2 = TRUE
bResult2 = bX1 AND bX2
PRINT bResult2

```

Result:

```
0
1

```

OR operator

Purpose: The OR logical operator performs a logical disjunction on two operands returning FALSE if both operands evaluate to FALSE otherwise returning TRUE.

 Syntax: ``` BoolVal = Operand1 OR Operand2 ``` Parameters: BoolVal Returned value, zero or not zero. Operand1 Operand to be evaluated with Operand2.

Remarks:

A space must be before and after the OR operator.

Example:

```
DIM AS BOOL bResult1, bResult2, bX1, bX2
bX1 = FALSE
bX2 = FALSE
bResult1 = bX1 OR bX2
PRINT bResult1
bX2 = TRUE
bResult2 = bX1 OR bX2
PRINT bResult2

```

Result:

```
0
1

```

ANDALSO and ORELSE operators

ANDALSO and ORELSE were added to the BCX lexicon for the convenience of users of other BASIC dialects which have these operators. The functionality of ANDALSO is equivalent to the C language && operator which will evaluate the second operand only if the first evaluates as TRUE (non zero). The functionality of ORELSE is equivalent to the C language || operator which will evaluate the second operand only if the first evaluates as FALSE (zero).

Example:

```
DIM AS BOOL bResult1, bResult2, bX1, bX2
DIM szBuf\$
bX1 = TRUE
bX2 = FALSE
bResult1 = bX1 ANDALSO bX2
bResult2 = bX1 ORELSE bX2
PRINT "bX1 bX2 bX1 ANDALSO bX2 bX1 ORELSE bX2"
sprintf(szBuf,"%3i %3i     %2i               %2i",bX1,bX2,bResult1, bResult2)
PRINT szBuf
bResult1 = bX1 AND bX2
bResult2 = bX1 OR bX2
PRINT "bX1 bX2 bX1 AND     bX2 bX1 OR     bX2"
sprintf(szBuf,"%3i %3i     %2i               %2i",bX1,bX2,bResult1, bResult2)
PRINT szBuf
bResult1 = bX1 && bX2
bResult2 = bX1 || bX2
PRINT "bX1 bX2 bX1 &&      bX2 bX1 ||     bX2"
sprintf(szBuf,"%3i %3i     %2i               %2i",bX1,bX2,bResult1, bResult2)
PRINT szBuf
PAUSE

```

Result:

```
bX1 bX2 bX1 ANDALSO bX2 bX1 ORELSE bX2
1   0      0                1
bX1 bX2 bX1 AND     bX2 bX1 OR     bX2
1   0      0                1
bX1 bX2 bX1 &&      bX2 bX1 ||     bX2
1   0      0                1

```

### Truth Table for Logical Operators

When using the operators NOT, AND, OR, XOR, a space must be before and after the operator, like this,

```
IF (c > 64 AND c < 91) OR (c > 96 AND c < 123) THEN

```

Each operator returns results as indicated below. A "T" indicates a true value and an "F" indicates a false value. Operators are listed in order of operator precedence.

```
---------------------------------------------------------
Values of      Value Returned by Logical Operator
---------------------------------------------------------
X    X    X
NOT  AND  OR   XOR
X     Y          X    Y    Y    Y
---------------------------------------------------------
T     T          F    T    T    F

T     F          F    F    T    T

F     T          T    F    T    T

F     F          T    F    F    F
---------------------------------------------------------

```

## Bitwise operators

BNOT operator

 Syntax: ``` RetVal% = BNOT Number ``` Parameters: RetVal% Returned value. Number Integer number.

Remarks:

BNOT is a bitwise inversion operator, commonly known as "One's Complement". BNOT inverts binary bits in a numeric expression to 0 if 1 and to 1 if 0. 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
PRINT BINstr1\$

int2% = BNOT int1%
BINstr2\$ = BIN\$(int2%)
PRINT BINstr2\$

```

Result:

```
00000000000000000011000000111001
11111111111111111100111111000110

```

BAND operator

 Syntax: ``` RetVal% = Number1 BAND Number2 ``` Parameters: RetVal% Returned value. Number1 Integer number. Number2 Integer number.

Remarks:

The BAND bitwise operator compares corresponding bits in two numeric expressions and sets the corresponding bit in the result to 1 if both bits are 1. 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:

```
DIM a, b
a = 1
b = a BAND 1
?  b
b = a BAND 0
?  b

```

Result:

```
1
0

```

Example:

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

```
DIM A%
INPUT "Number ? "; A%

IF A% BAND 1 THEN
PRINT A%, " is odd"
ELSE
PRINT A%, " is even"
END IF

```

BOR operator

 Syntax: ``` RetVal% = Number1 BOR Number2 ``` Parameters: RetVal% Returned value. Number1 Integer number. Number2 Integer number.

Remarks:

The bitwise BOR operator compares corresponding bits in numeric-expression1 and numeric-expression2, then sets the corresponding bit in the result according to the following table:

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

```

Example:

```
DIM a, b
a = 0
b = a BOR 1
?  b
b = a BOR 0
?  b

```

Result:

```
1
0

```

XOR operator

 Syntax: ``` RetVal% = Number1 XOR Number2 ``` Parameters: RetVal% Returned value. Number1 Integer number. Number2 Integer number.

Remarks:

The logical "exclusive or"(sometimes called the difference detector) operator compares corresponding bits in Number1 and Number2 then sets the corresponding bit in RetVal% according to the following 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 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 parameter left (<<) or right (>>) by the number of places in the second parameter. Both parameters must be integers.

<< left shift operator

 Syntax: ``` RetVal% = IntNum% << NumPlaces% ``` Parameters: RetVal% Returned integer value. IntNum% Integer number to be shifted left. NumPlaces% Number of places to the left that IntNum% is to be shifted.

Remarks: Vacated right bits are set to 0.

>> right shift operator

 Syntax: ``` RetVal% = IntNum% >> NumPlaces% ``` Parameters: RetVal% Returned integer value. IntNum% Integer number to be shifted right. 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 << 1 'Shift left 1 place
PRINT value%

value% = 32768 >> 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(a, BitNum) 'BitNum is zero based
DIM tmp
tmp = a BAND 1 << BitNum
IF tmp > 0 THEN tmp = 1
FUNCTION = tmp
END FUNCTION

FUNCTION BitSet(a, BitNum) 'BitNum is zero based
DIM tmp
tmp = a BOR 1 << BitNum
FUNCTION = tmp
END FUNCTION

FUNCTION BitClr(a, BitNum) 'BitNum is zero based
DIM tmp
tmp = a BAND ((1 << BitNum) XOR 0xFFFFFFFF) 'the XOR with all 1's causes all bits to toggle
FUNCTION = tmp
END FUNCTION

FUNCTION BitTgl(a, BitNum) 'BitNum is zero based
DIM tmp
tmp = a XOR 1 << BitNum  'the XOR with 1 causes bit to toggle
FUNCTION = tmp
END FUNCTION

FUNCTION dec2bin\$ OPTIONAL (a, length = 0)  'optionally formated
DIM i, tmp, t, T\$

IF length = 0 THEN  'we don't know the length here
tmp = a
WHILE tmp > 0
tmp = tmp >> 1
length++
WEND
END IF

t = 1

FOR i = 1 TO length 'we know the length here
tmp = a BAND t    'logical AND to test the bit
IF tmp = 0 THEN
T\$ = "0" & T\$
ELSE
T\$ = "1" & T\$
END IF
t = t << 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

```

+= 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
* Contents of
+ Unary plus
- Unary minus
~ Bitwise complement ("ones complement")
BNOT Bitwise complement ("ones complement")
++ 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 << Bitwise left shift Left to right
>> 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
<<= Assignment by Bitwise left shift
>>= Assignment by Bitwise right shift
&= Assignment by Bitwise AND
|= Assignment by Bitwise OR
15th , Sequential expression Left to right

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;

```

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%

```