Bit operations
TapHome script bit and byte manipulation — GETBIT, GETBITS, SETBIT, SETBITS, GETBYTE, SETBYTE, TOBCD, FROMBCD, bit width by type and bitwise operators with Excel equivalents.
These functions read and change single bits, bit fields and bytes of a whole number — typically status words and alarm registers read with MODBUSR. Bit 0 is the least significant (rightmost) bit and byte 0 the least significant byte.
Bit width by type
How many bits a number has for the bit functions depends on its type:
Type of number | Bits | bit / startBit | Byte index | Result of GETBITS, SETBIT, SETBITS, SETBYTE |
|---|---|---|---|---|
int8, uint8 | 8 | 0 – 7 | 0 | uint |
int16, uint16 | 16 | 0 – 15 | 0 – 1 | uint |
int64, uint64 | 64 | 0 – 63 | 0 – 7 | uint64 |
everything else (double, float, int, uint, …) | 32 | 0 – 31 | 0 – 3 | uint |
MODBUSR, decimal literals,PARSEJSONand device values aredouble, so they count as 32 bits. Only typed variables, collection elements and the 64-bit Modbus data types have another width.- A bit or byte outside the type is an error, for example
GETBIT(x, 16)for auint16variable: GETBIT - bit must be between 0 and 15. A bit field that starts inside the type but reaches over its top is allowed —GETBITSreturns the bits that exist andSETBITSdrops the rest. - A
doubleorfloatis rounded to a whole number and must be between −2 147 483 648 and 4 294 967 295; anything else,NaNincluded, is an error (value X is out of the 32-bit range). Negative values use two’s complement, soGETBITS(-1, 0, 16)is65535.
Available — the width by type is used from Core 2026.1, build 40661. Older cores, including earlier 2026.1 builds, treat every value as 32 bits: a 64-bit value loses its upper half and a bit above an 8-bit or 16-bit type returns 0.
GETBIT
GETBIT returns the value of one bit of a number, 0 or 1.
| |
Example
GETBIT(2, 0);
# Result: 0
GETBIT(4, 2);
# Result: 1
IF(GETBIT(MODBUSR(H, 600, Uint16), 3) = 1, ADDERROR(3, "Overheating"));GETBITS
GETBITS returns a bit field — numberOfBits bits starting at startBit — as an unsigned whole number.
| |
Parameters — numberOfBits is 1 – 32, or 1 – 64 for a 64-bit number.
Example
GETBITS(216, 3, 2);
# Result: 3 (216 = 0b11011000, bits 3 and 4 are 1)
GETBITS(0xFF, 0, 4);
# Result: 15A date and time packed as two bytes per register (Komfovent):
VAR time := MODBUSR(H, 28, Uint16);
VAR date := MODBUSR(H, 30, Uint16);
GETBITS(time, 8, 8) + ":" + GETBITS(time, 0, 8) + " "
+ GETBITS(date, 0, 8) + "." + GETBITS(date, 8, 8) + "." + MODBUSR(H, 29, Uint16);SETBIT
SETBIT returns the number with one bit set to 0 (when value is 0) or to 1 (any other value).
| |
Example
SETBIT(1, 1, 1);
# Result: 3
SETBIT(4, 2, 0);
# Result: 0
uint64 alarms := 0;
alarms := SETBIT(alarms, 40, 1);
GETBIT(alarms, 40);
# Result: 1 (a 64-bit variable has bits 0 to 63)SETBITS
SETBITS returns the number with the bit field of numberOfBits bits starting at startBit replaced by value.
| |
Example
SETBITS(192, 4, 2, 3);
# Result: 240
SETBITS(192, 5, 2, 3);
# Result: 224Errors — Given number of bits (2) is not enought for storing the value 4. when value does not fit into the field (the message is spelled this way).
GETBYTE
GETBYTE returns one byte of a number (0 – 255) as a uint.
| |
Example
GETBYTE(256, 0);
# Result: 0
GETBYTE(256, 1);
# Result: 1
GETBYTE(259, 0);
# Result: 3SETBYTE
SETBYTE returns the number with the byte at index replaced by value (0 – 255).
| |
Example
SETBYTE(256, 0, 255);
# Result: 511
SETBYTE(259, 1, 2);
# Result: 515Errors — Given value (X) is bigger than maximum byte value.; Given value is less than 0.
TOBCD
TOBCD converts a whole number to packed BCD — each decimal digit in its own 4 bits — and returns it as a uint; up to 8 digits are kept.
| |
Example
TOBCD(1234);
# Result: 4660 (0x1234)FROMBCD
FROMBCD converts a packed BCD value back to an ordinary number.
| |
Example
FROMBCD(0x1234);
# Result: 1234Errors — FROMBCD - number 26 is not in BCD format and the position of the digit when a group of 4 bits is greater than 9 (here 0x1A).
Bitwise operators and Excel equivalents
The operators work on whole numbers; a double operand is converted to int64 first and a Null operand gives Null. The full list and priorities are on Operators.
| Operator | Example | Result | Excel |
|---|---|---|---|
<< left bit shift | 8 << 2 | 32 | BITLSHIFT(number, shift_amount) |
>> right bit shift | 32 >> 2 | 8 | BITRSHIFT(number, shift_amount) |
& bitwise AND | 3 & 1 | 1 | BITAND(number1, number2) |
| bitwise OR | 2 | 1 | 3 | BITOR(number1, number2) |
^ bitwise XOR | 5 ^ 1 | 4 | BITXOR(number1, number2) |
The bitwise operators bind more loosely than comparisons:
a & 1 > 0meansa & (1 > 0). Always use parentheses.
VAR status := MODBUSR(H, 5, Uint16);
IF((status & 0x04) > 0, "Door open", "Door closed");See the example of bit operations in Google Sheets, or try the interactive tool at bitwisecmd.com.