BIT function

Purpose

Return the value of a particular bit in an integral-class variable (or in an implied bit-array)

Syntax

flag = BIT(intvar, bitnumber)

Remarks

The BIT function is used to determine the value of one particular bit in an integral-class variable or implied bit-array.

intvar

The parameter intvar must be a variable, not an expression.  The BIT function returns either 0 or 1 to indicate the value of the specified bit.

bitnumber

The bit in question.  The allowable range for the parameter is the same as that of a Long-integer.  This makes it possible to have implicit bit-arrays of more than 2 billion bits in size.  For such arrays, 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, 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).

The first bit is the least-significant bit, which is bit number zero.  For example:

See also

BIT CALC statementBIT statement, 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)