BIT CALC statement

Purpose | Set or reset a bit in an integral class variable (or implied bit-array) based upon the result of an expression.
Syntax | BIT CALC intvar, bitnumber, calcexpr
Remarks | BIT CALC performs like a combination of the BIT SET and BIT RESET statements, offering the choice between set (1) and reset (0) according to the result of a numeric expression.
intvar | An integral class variable (Byte, Word, Double-word, Integer, Long-integer or Quad-integer), or a variable forming the base of an implied bit-array.
bitnumber | An integral class expression or numeric literal that specifies the bit number to adjust. Bit numbers start from zero (0), and extend to the size of the target variable or bit-array. For example, a 16-bit integer variable uses the range 0 to 15. An implied bit-array comprised of a Long-integer array with 100 elements (4 bytes * 100 = 400 bytes = 3200 bits) covers the bit range 0 to 3199.
calcexpr | The value derived from bit zero of calcexpr determines the set or reset action. If bit zero contains a zero (0), the bit in intvar is reset; if bit zero in calcexpr contains a one (1), the bit in intvar is set. This action can be more easily remembered if we consider PowerBASIC performs an implied bitwise AND operation (calcexpr AND 1) to derive the set or reset action. Care must be exercised to ensure that the bit index number (bitnumber) does not exceed the number of bits that can be validly accessed. For example, reading the 17th bit of a 16-bit scalar variable may trigger a General Protection Fault (GPF). Similarly, adjusting the 4097th bit of a bit-array derived from a 128-element DWORD array may cause similar problems. bitnumber is always zero-based, so the 129th bit of an implied bit-array is referenced in the BIT statement with bitnumber equal to 128. For example: x& = BIT(A?(1), 128).
See also | BIT function, BIT statement, BITS functions
Example | DIM dwStatus1 AS DWORD DIM dwStatus2 AS DWORD DIM iBit AS INTEGER DIM sResult1 AS STRING DIM sResult2 AS STRING FOR iBit = 0 TO 31 BIT CALC dwStatus1, iBit, RND(0,1) BIT CALC dwStatus2, iBit, iBit MOD 3 NEXT iBit sResult1 = BIN$(dwStatus1,32) sResult2 = BIN$(dwStatus2,32)
Result | sResult1 = "01001101001110101110111010010101" sResult2 = "10010010010010010010010010010010"