than 32 flags, you can use lists of integers. This method of using a single bit for a flag has these
advantages, compared to using a single true/false variable for each status item:
! Less RAM is used, if the program requires many status flags.
! Large groups of flags can be quickly initialized.
! Fewer variable names may be needed, since each group of flags has only one name, and the bits
are specified by a number.
! You can combine related status flags into single variables or lists, which makes it easier to
manipulate them.
! You can quickly perform status tests on groups of flags with a mask.
There are four basic operations we need to perform on flags: set a flag, clear a flag, invert the status of
a flag, and test to determine if a flag is set or cleared. While these operations can be performed most
efficiently with C or assembly programs, it is possible to code these operations in TIBasic, as these
four functions show:
Return true if bit# in VarOrList is set, otherwise return false.Test a bit: bittst(VarOrList,bit#)
Invert bit# in VarOrList: if bit# is 0, set it to 1; if 1, set it to zeroInvert a bit: bitnot(VarOrList,bit#)
Clear bit# in VarOrList to 0Clear a bit: bitclr(VarOrList,bit#)
Set bit# in VarOrList to 1Set a bit: bitset(VarOrList,bit#)
ResultsFunction
For all four routines, VarOrList is a 32-bit integer variable or constant, or a list of 32-bit variables or
constants. bit# is the number of the bit to manipulate or test. Bits are numbered starting with zero, so
the bits in a 32-bit integer are numbered from zero to 31. Bit number 0 is the least significant bit, and bit
number 31 is the most significant bit.
The 89/92+ use a prefix convention to specify 32-bit integers. The two prefixes are '0b' and '0h'. '0b'
specifies a binary (base-2) integer consisting of digits 0 and 1. '0h' specifies a hexadecimal (base-16)
integer consisting of digits 0 to 9 and A to F. If no prefix is used, the integer is represented as a
base-10 number.
To demonstrate the functions, suppose our program needs to keep track of eight status flags, which
we store in a variable called status. To clear all the flags, use
0→status
To set all the flags, use
0hFF→status
Note that I use the 0h prefix to indicate that FF is a hexadecimal (base-16) integer. You could also use
255→status
since, in binary (base-2) representation, both numbers are 0b11111111.
Suppose that all the flags are cleared, or set to zero. To set bit 0, use
bitset(status,0)→status
then status would be, in binary, 0b00000001.
3 - 22