BIT statement

Purpose | Manipulate individual bits of an integral class variable (or in an implied bit-array), for storing values such as TRUE/FALSE (flag) settings quickly and efficiently.
Syntax | BIT {SET | RESET | TOGGLE} intvar, bitnumber

Remarks

intvar must be one of the integral-class variable types: Byte, Word, Integer, Double-word, Long-integer, or Quad-integer.

The allowable range for the parameter bitnumber is the same as that of a Long-integer, making it possible to have implicit bit-arrays of more than 2 billion bits in size. Bits 0 to 15 are in the first word starting at intvar, bits 16-31 are in the next word, and so forth.

Implied bit-arrays are considered to start at the memory position of the variable intvar. For example, if intvar is itself an array variable, it is possible to access bits in any of the following elements of the array. See the array examples below.

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, adjusting the 17th bit of a 16-bit scalar variable may cause a subtle memory corruption problem, and/or 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: BIT SET A?(1), 128.

SET | Sets the indicated bit to one.
RESET | Sets the indicated bit to zero.
TOGGLE | Toggles the indicated bit: one becomes zero; zero becomes one.
See also | BIT CALC statement, BIT function, BITS functions

Example

x% = 7

BIT SET x%, 2 ' Sets the 3rd bit (bit 2) to 1

BIT RESET x%, 10 ' Sets the 11th bit (bit 10) to 0

BIT TOGGLE x%, 5 ' Toggle bit 5

[statements]

DIM z%(1 TO 2000) ' 32000 element bit-array

BIT SET z%(1), 37 ' Sets bit 5 of 3rd word to a 1

BIT TOGGLE z%(1),0 ' Toggle lowest bit in 1st word

BIT RESET z%(2000), 15 ' Clear the MSB of integer array element

' 2000 (bit 31999 of the implied bit array,

' numbered 0 to 31999)

BIT RESET z%(1), 31999 ' Clear the MSB of element 2000

' (this is equivalent to the previous line)