Visual FoxPro functions for bit manipulation

Visual FoxPro can manipulate numbers up to the size of a 32-bit integer as patterns of bits. Here is a list of some of the more useful boolean commands and functions. Many of them accept extra optional parameters. Look in the FoxPro Help system for more information about the syntax and use of these functions.

Boolean operators

The simplest boolean operator is NOT. This takes a single number as a parameter. Every bit in the output number is the opposite of its equivalent in the input parameter. Every zero will be replaced by a one; every one will be replaced by a zero. FoxPro performs this operation with the BITNOT() function:

BITNOT(<n1>) returns <n1> with every bit inverted.

Three other boolean operators take two input parameters and return a single number:

  • AND will produce a 1 only if both input bits are 1.
  • OR will produce a 1 if either or both input bits are 1.
  • XOR will produce a 1 if either but not both input bits are 1.

FoxPro implements these operators with these three functions:

BITAND(<n1>,<n2>) return <n1> AND <n2>.
BITOR(<n1>,<n2>) return <n1> OR <n2>.
BITXOR(<n1>,<n2>) return <n1> XOR <n2>.

Note that each of these three functions can take more than two parameters.

Other bitwise operators

The following FoxPro commands change or test an individual bit of a single number:

BITCLEAR(<n1>,<n2>) return <n1> with bit <n2> set to 0.
BITSET(<n1>,<n2>) return <n1> with bit <n2> set to 1.
BITTEST(<n1>,<n2>) return .T. if bit <n2> of <n1> is 1.

The following commands shift the bits of a number left or right:

BITLSHIFT(<n1>,<n2>) return <n1> with all bits shifted <n2> places to the left. The leftmost <n2> bits are set to 0.
BITRSHIFT(<n1>,<n2>) return <n1> with all bits shifted <n2> places to the right. The rightmost <n2> bits are set to 0.