flags and mask = {0b0110,0b1000}
All the previous examples have shown the bit number argument passed as a number. It may, of
course, be a variable instead, and you can improve program readability by choosing appropriate
variable names for the flags. As an example, suppose that we have a program which may or may not
calculate four sums, three limits and two integrals. We want to keep track of which operations have
been done. We need a total of 9 flags, so we can save them all in one integer. At a slight expense in
RAM usage, we can assign the flag numbers to variables like this:
0→sum1stat ©Status flag names for sum1,
1→sum2stat ©...sum2,
2→sum3stat ©...sum3
3→sum4stat ©...and sum4
4→lim1stat ©Status flag names for limit1,
5→lim2stat ©...limit2
6→lim3stat ©...and limit 3
7→int1stat ©Status flag names for integral1
8→int2stat ©...and integral2
Now when we refer to one of the status flags, we use its name instead of the number, for example:
bitset(status,lim1stat)→status
would set flag 4 in status. To test whether or not the second integral has been calculated, we would
use
if bittst(status,int2stat) then
... {block} ...
endif
All four of the bit manipulation functions do some simple error checking:
! The flags argument must be an integer or a list of integers. Any other type will return an error
message.
! The bit number must be greater than -1, and one less than the total number of flags. If the flags
argument is an integer, the bit number must be less than 32.
The same error message is used regardless of which error occurs. The error message is a string of the
form "name() err", where name is the name of the function. Since one error message is used for all
errors, there is some ambiguity as to which condition caused the error. This is not a serious flaw,
because there are only two possible error causes. As usual, the calling routine can can use gettype()
to determine the type of the returned result; if it is a string, then an error occurred.
Some possible errors are not detected. If the flags are passed as a list, the type of the list elements are
not checked, that is, you must ensure that the list elements are integers. The functions do not
determine if the argument is an integer, only that it is a number. Both integers and floating-point
numbers have a type of "NUM", so the functons cannot distinguish between them.
A warning message is shown in the status line, when the bit number is 31 (for a single integer flag
argument), or is the most significant bit of an integer in a list argument. The warning message is
"Warning: operation requires and returns 32 bit value". This warning does not indicate a flaw in the
functions, nor does it affect the operation.
3 - 25