Bit functions

The Bit functions are BitAnd, BitOr, BitNot, BitSet, BitReset, BitTest, and BitXOr. They perform bitwise operations on integers.

Syntax

BitAnd | BitOr | BitXOr (integer1, integer2)
BitSet | BitReset | BitTest (integer, bit.number)
BitNot (integer [,bit.number])

integer1 and integer2 are integers to be compared. If either integer is a null value, a null value is returned. Decimal places are truncated before the evaluation.

integer is the integer to be evaluated. If integer is a null value, a null value is returned. Decimal places are truncated before the evaluation.

bit.number is the number of the bit to act on. Bits are counted from right to left starting with 0. If bit.number is a null value, the program fails with a runtime error.

Remarks

The Bit functions operate on a 32-bit twos-complement word. Do not use these functions if you want your code to be portable, as the top bit setting might differ on other hardware.

BitAnd compares two integers bit by bit. For each bit, it returns bit 1 if both bits are 1; otherwise it returns bit 0.

BitOr compares two integers bit by bit. For each bit, it returns bit 1, if either or both bits is 1; otherwise it returns bit 0.

BitXOr compares two integers bit by bit. For each bit, it returns bit 1 if only one of the two bits is 1; otherwise it returns bit 0.

BitTest tests if the specified bit is set. It returns 1 if the bit is set; 0 if it is not.

BitNot inverts the bits in an integer, that is, changes bit 1 to bit 0, and vice versa. If bit.number is specified, that bit is inverted; otherwise all bits are inverted.

BitSet sets the specified bit to 1. If it is already 1, it is not changed.

BitReset resets the specified bit to 0. If it is already 0, it is not changed.

Examples

BitAnd

Result = BitAnd(6, 12)         ;* Result is 4
*  (bin) (dec) BitAnd (bin) (dec) gives (bin) (dec)
*   110     6              1100     12           100    4

BitNot

Result = BitNot(6)             ;* Result is -7
Result = BitNot(15, 0)         ;* Result is 14
Result = BitNot(15, 1)         ;* Result is 13
Result = BitNot(15, 2)         ;* Result is 11
* (bin) (dec) BitNot  bit#  gives (bin) (dec)
*  110   6     (all)              1...1001    7
* 1111   15      0                    1110   14
* 1111   15      1                    1101   13
* 1111   15      2                    1011   11 
     

BitOr

Result = BitOr(6, 12)          ;* Result is 14
*  (bin) (dec) BitOr (bin) (dec) gives (bin) (dec)
*   110     6                 1100    12         1110 14

BitReset

Result = BitReset(29, 0)               ;* Result is 28
Result = BitReset(29, 3)               ;* Result is 21
Result = BitReset(2, 1)               ;* Result is 0
Result = BitReset(2, 0)                ;* Result is 2
*  (bin) (dec) BitReset bit#  gives (bin) (dec)
*  11101    29              0         11100    28
*  11101    29              3         10101    21
*    10      2               1            00     0
*    10      2               0            10     2

BitSet

Result = BitSet(20, 0)               ;* Result is 21
Result = BitSet(20, 3)               ;* Result is 28
Result = BitSet(2, 0)               ;* Result is 3
Result = BitSet(2, 1)               ;* Result is 2
*  (bin) (dec) BitReset bit#  gives (bin) (dec)
*  10100   20             0        10101    21
*  10100   20             2        11100    28
*     10    2             0           11     3
*     10    2             1           10     2

BitTest

Result = BitTest(11, 0)        ;* Result is 1
Result = BitTest(11, 1)        ;* Result is 1
Result = BitTest(11, 2)        ;* Result is 0
Result = BitTest(11, 3)        ;* Result is 1
*  (bin) (dec) BitTest bit#  is:
*  1011    11           0     1
*  1011    11           1     1
*  1011    11           2     0
*  1011    11           3     1

BitXOr

Result = BitXor(6, 12)         ;* Result is 10
*  (bin) (dec) BitXOr (bin) (dec) gives (bin) (dec)
*   110     6         1100    12        1010    10