---
title: "Operazioni sui bit"
description: "Operazioni sui bit negli script TapHome: GETBIT, GETBITS, GETBYTE, SETBYTE, SETBIT, SETBITS, conversione BCD, larghezza 8/16/32/64 bit, scorrimento di bit, AND e OR bit a bit con equivalenti Excel."
url: "https://taphome.com/it/docs/script/bit-operations/"
language: it
lastmod: 2026-11-06
generator: hugo
ai:
  topics: ["bit-operations","byte-manipulation","bitwise-operators","scripting-language"]
  questions: ["Come si legge un singolo bit di un numero in TapHome?","Come si estrae un byte da un numero con GETBYTE?","Come si imposta un determinato bit o byte in uno script TapHome?","Come si esegue uno scorrimento di bit a sinistra o a destra in TapHome?","Come funzionano AND e OR bit a bit negli script TapHome?","Quali sono gli equivalenti Excel degli operatori sui bit di TapHome?","Quanti bit usano le funzioni sui bit per un valore a 16 o a 64 bit?","Come si converte un numero in BCD o da BCD?"]
---

# Operazioni sui bit

Manipolazione di bit e byte negli script TapHome — GETBIT, GETBITS, SETBIT, SETBITS, GETBYTE, SETBYTE, TOBCD, FROMBCD, larghezza in bit secondo il tipo e operatori bit a bit con equivalenti Excel.

Ultimo aggiornamento: 2026-11-06

Queste funzioni leggono e modificano singoli bit, campi di bit e byte di un numero intero — di solito parole di stato e registri di allarme letti con `MODBUSR`. Il bit 0 è il bit meno significativo (quello più a destra) e il byte 0 il byte meno significativo.

## Larghezza in bit secondo il tipo

Quanti bit ha un numero per le funzioni sui bit dipende dal suo tipo:

| Tipo di `number` | Bit | `bit` / `startBit` | `index` del byte | Risultato di `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` |
| tutto il resto (`double`, `float`, `int`, `uint`, …) | 32 | 0 – 31 | 0 – 3 | `uint` |

- `MODBUSR`, i letterali decimali, `PARSEJSON` e i valori dei dispositivi sono `double`, quindi contano come 32 bit. Solo le variabili tipizzate, gli elementi delle collezioni e i tipi di dati Modbus a 64 bit hanno un'altra larghezza.
- Un bit o un byte fuori dal tipo è un errore, per esempio `GETBIT(x, 16)` per una variabile `uint16`: *GETBIT - bit must be between 0 and 15*. Un campo di bit che **inizia** all'interno del tipo ma ne supera il limite superiore è consentito — `GETBITS` restituisce i bit esistenti e `SETBITS` scarta il resto.
- Un `double` o un `float` viene arrotondato a un numero intero e deve essere compreso tra −2 147 483 648 e 4 294 967 295; qualsiasi altro valore, `NaN` compreso, è un errore (*value X is out of the 32-bit range*). I valori negativi usano il complemento a due, quindi `GETBITS(-1, 0, 16)` è `65535`.

**Disponibile** — la larghezza secondo il tipo richiede TapHome Next 2026.1.40665+. Le build più vecchie trattano ogni valore come 32 bit: un valore a 64 bit perde la metà superiore e un bit oltre un tipo a 8 o 16 bit restituisce `0`.

## GETBIT

`GETBIT` restituisce il valore di un bit di un numero, `0` o `1`.

```text
GETBIT(number, bit)
```

**Esempio**

```taphome
# legge un singolo bit di un numero
GETBIT(2, 0);
# Result: 0
GETBIT(4, 2);
# Result: 1
# genera l'errore 3 se il bit 3 del registro è impostato
IF(GETBIT(MODBUSR(H, 600, Uint16), 3) = 1, ADDERROR(3, "Overheating"));
```

## GETBITS

`GETBITS` restituisce un campo di bit — `numberOfBits` bit a partire da `startBit` — come numero intero senza segno.

```text
GETBITS(number, startBit, numberOfBits)
```

**Parametri** — `numberOfBits` va da 1 a 32, oppure da 1 a 64 per un `number` a 64 bit.

**Esempio**

```taphome
GETBITS(216, 3, 2);
# Result: 3 (216 = 0b11011000, bits 3 and 4 are 1)
GETBITS(0xFF, 0, 4);
# Result: 15
```

Data e ora compresse in due byte per registro (Komfovent):

```taphome
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` restituisce il numero con un bit impostato a 0 (quando `value` è 0) o a 1 (qualsiasi altro `value`).

```text
SETBIT(number, bit, value)
```

**Esempio**

```taphome
# imposta il bit 1 di 1 a 1, poi il bit 2 di 4 a 0
SETBIT(1, 1, 1);
# Result: 3
SETBIT(4, 2, 0);
# Result: 0
# imposta il bit 40 di una variabile a 64 bit e lo rilegge
uint64 alarms := 0;
alarms := SETBIT(alarms, 40, 1);
GETBIT(alarms, 40);
# Result: 1 (a 64-bit variable has bits 0 to 63)
```

## SETBITS

`SETBITS` restituisce il numero in cui il campo di `numberOfBits` bit a partire da `startBit` è sostituito da `value`.

```text
SETBITS(number, startBit, numberOfBits, value)
```

**Esempio**

```taphome
SETBITS(192, 4, 2, 3);
# Result: 240
SETBITS(192, 5, 2, 3);
# Result: 224
```

**Errori** — *Given number of bits (2) is not enought for storing the value 4.* quando `value` non entra nel campo (il messaggio è scritto proprio così).

## GETBYTE

`GETBYTE` restituisce un byte di un numero (0 – 255) come `uint`.

```text
GETBYTE(number, index)
```

**Esempio**

```taphome
# l'indice 0 è il byte più basso, l'indice 1 il successivo
GETBYTE(256, 0);
# Result: 0
GETBYTE(256, 1);
# Result: 1
# 259 è 0x0103, quindi il byte 0 vale 3
GETBYTE(259, 0);
# Result: 3
```

## SETBYTE

`SETBYTE` restituisce il numero in cui il byte in posizione `index` è sostituito da `value` (0 – 255).

```text
SETBYTE(number, index, value)
```

**Esempio**

```taphome
SETBYTE(256, 0, 255);
# Result: 511
SETBYTE(259, 1, 2);
# Result: 515
```

**Errori** — *Given value (X) is bigger than maximum byte value.*; *Given value is less than 0.*

## TOBCD

`TOBCD` converte un numero intero in BCD compresso — ogni cifra decimale nei propri 4 bit — e lo restituisce come `uint`; si conservano fino a 8 cifre.

```text
TOBCD(number)
```

**Esempio**

```taphome
TOBCD(1234);
# Result: 4660 (0x1234)
```

## FROMBCD

`FROMBCD` riconverte un valore BCD compresso in un numero normale.

```text
FROMBCD(number)
```

**Esempio**

```taphome
FROMBCD(0x1234);
# Result: 1234
```

**Errori** — *FROMBCD - number 26 is not in BCD format* e la posizione della cifra quando un gruppo di 4 bit è maggiore di 9 (qui `0x1A`).

## Operatori bit a bit ed equivalenti in Excel

Gli operatori lavorano sui numeri interi; un operando `double` viene prima convertito in `int64` e un operando `Null` dà `Null`. L'elenco completo e le priorità sono nella pagina [Operatori](https://taphome.com/it/docs/script/operators/).

| Operatore | Esempio | Risultato | Excel |
|---|---|---|---|
| `<<` scorrimento di bit a sinistra | `8 << 2` | `32` | `BITLSHIFT(number, shift_amount)` |
| `>>` scorrimento di bit a destra | `32 >> 2` | `8` | `BITRSHIFT(number, shift_amount)` |
| `&` AND bit a bit | `3 & 1` | `1` | `BITAND(number1, number2)` |
| `\|` OR bit a bit | `2 \| 1` | `3` | `BITOR(number1, number2)` |
| `^` XOR bit a bit | `5 ^ 1` | `4` | `BITXOR(number1, number2)` |

> [!WARNING]
> Gli operatori bit a bit legano meno strettamente dei confronti: `a & 1 > 0` significa `a & (1 > 0)`. Usa sempre le parentesi.

```taphome
VAR status := MODBUSR(H, 5, Uint16);
IF((status & 0x04) > 0, "Door open", "Door closed");
```

Guarda l'esempio di operazioni sui bit in [Google Sheets](https://docs.google.com/spreadsheets/d/1hF5FMpGMJbgYh-YLwWrq2n186_ATyGyLUb689__IhLY/edit?usp=sharing), oppure prova lo strumento interattivo su [bitwisecmd.com](http://bitwisecmd.com/).
