Description
The bit function returns an integer value corresponding to the bit position supplied( i.e. the value if that bit position is ON ). It assumes the specified bit position ( bitPos ) is set to "1" ( i.e. bit set "ON"), and all other bits set to "0" ( i.e. bit set "OFF").
Bit positions are counted from right to left: a bitPos value of zero corresponds to the rightmost ("least significant") bit. The maximum allowable value for bitPos is 31 for 4 byte integers and 63 for 8 byte integers, which corresponds to the leftmost bit in an integer value.
So for example,
bit position 0 has a value of 1 ( 20)
bit position 1 has a value of 2 ( 21)
bit position 2 has a value of 4 ( 22)
bit position 3 has a value of 8 ( 23)
bit position 4 has a value of 16 ( 24)
etc. all the way to either bit position 31 ( or 63 for 8 byte integers ) with the values increasing by powers of two as shown.
If bitPos is a variable instead of a integer literal ( i.e. 1,2,3 etc. ), it can be either an integer or floating point variable.
In the case of a floating point variable it uses only the whole number and not the fractional portion ( i.e. if a variable contains 3.65 only the 3 is used )
bit is useful in conjunction with "bitwise operators" like and/or for setting and testing the values of particular bits in a quantity.
Note
A short hand version of bit is possible by assigning the bitPos to a constant and suffixing the constant with a percent symbol ( "%" )
If _myBitPosition is the constant name, then you can use _myBitPosition% (note the "%") as a synonym for bit(bitPos).
The following expression evaluates as _true (1) if bit "n" is set in testValue: (testValue and bit(n)) != 0
The following assignment sets bit "n" in testValue to 1: testValue = (testValue or bit(n))
The following assignment resets bit "n" in testValue to 0: testValue = (testValue and not bit(n))