Viene mostrato il Markdown grezzo: non è stato possibile caricare il visualizzatore.
## Perché è importante Apri le impostazioni del tuo router di casa e vedrai numeri come `192.168.1.10`. Guarda i dettagli della tua scheda Wi-Fi e troverai qualcosa come `00:00:5e:00:53:01`. Scegli un colore in una pagina web e viene scritto `#FF8800`. Sembrano codici segreti, ma sono tutti la stessa cosa: numeri normali scritti in un modo che a un computer riesce facile gestire. Non serve essere bravi in matematica per leggerli. Serve una sola abilità: saper contare in un altro modo. In questa lezione la impari passo dopo passo. I moduli successivi sull'indirizzamento IP e sul subnetting si basano direttamente su di essa, quindi qualche minuto di pratica ora ti evita molta confusione più avanti. ## Obiettivi di apprendimento - Spiegare che cosa sono un bit e un byte e perché i computer usano il sistema binario. - Convertire numeri tra decimale e binario (da 0 a 255). - Convertire numeri tra binario ed esadecimale e riconoscere l'esadecimale in un indirizzo MAC. - Leggere un indirizzo IPv4 come quattro numeri a 8 bit. ## Concetti chiave ### Due stati: bit e byte Pensa a un interruttore della luce. È acceso o spento, e non c'è nulla in mezzo. Dentro un computer ci sono miliardi di minuscoli interruttori elettronici, e ognuno trasporta un segnale oppure no. Distinguere «acceso» da «spento» è facile e molto affidabile, anche quando il segnale è debole o disturbato. Distinguere dieci livelli diversi sarebbe molto più difficile e causerebbe errori. Ecco perché i computer usano solo due stati. Scriviamo i due stati con le cifre `1` (acceso) e `0` (spento). Una di queste cifre è un **bit**, abbreviazione di *binary digit* (cifra binaria). Un singolo bit può dire solo sì o no, quindi i computer raggruppano i bit. Un gruppo di 8 bit è un **byte**. Quanti valori diversi può contenere un byte? Ogni bit ha 2 possibilità e i bit sono 8, quindi ci sono 2 × 2 × 2 × 2 × 2 × 2 × 2 × 2 = **256** combinazioni diverse. Le usiamo per contare da 0 a 255. (I valori sono 256, ma il più grande è 255, perché si comincia a contare da 0.) Un numero scritto solo con le cifre 0 e 1 si chiama numero **binario**. I numeri che usi ogni giorno, con le cifre da 0 a 9, sono numeri **decimali**. > [!CHECK] > Quanti valori diversi può contenere un byte (8 bit)? > > - [ ] 8 > - [ ] 255 > - [x] 256 > - [ ] 1.024 > > Ciascuno degli 8 bit ha 2 stati possibili, quindi ci sono 2 alla ottava = 256 combinazioni. Rappresentano i numeri da 0 a 255, ed è per questo che 255 è il valore massimo, non la quantità di valori. ### Valori posizionali: come si conta davvero Sai già leggere il binario. Solo che non lo sai ancora, perché funziona esattamente come il decimale. Guarda il numero decimale 407: | Valore posizionale | 100 | 10 | 1 | |---|---|---|---| | Cifra | 4 | 0 | 7 | Il numero significa 4 × 100 + 0 × 10 + 7 × 1 = 407. Ogni colonna vale **10 volte** la colonna alla sua destra, perché il decimale ha dieci cifre (da 0 a 9). Quando una colonna finisce le cifre, si passa alla successiva: dopo il 9 viene il 10. Il binario ha solo due cifre, quindi ogni colonna vale **2 volte** la colonna alla sua destra. Per un byte ci sono 8 colonne: | Valore posizionale | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 | |---|---|---|---|---|---|---|---|---| Partendo da destra si raddoppia ogni volta: 1, 2, 4, 8, 16, 32, 64, 128. Impara a memoria questa riga: la userai di continuo. Una cifra `1` in una colonna significa «conta questa colonna»; uno `0` significa «salta». <figure class="diagram" style="--dg-w: 1040px"><svg xmlns="http://www.w3.org/2000/svg" class="dg" viewBox="0 0 1040 340" width="1040" height="340" role="img" aria-labelledby="binhex-place-values-it-title binhex-place-values-it-desc" font-family="Inter, system-ui, -apple-system, Segoe UI, Roboto, sans-serif" xml:lang="it"> <title id="binhex-place-values-it-title">Valori posizionali binari di un byte</title> <desc id="binhex-place-values-it-desc">Un byte è fatto di otto bit. Le colonne valgono 128, 64, 32, 16, 8, 4, 2 e 1 da sinistra a destra. Il byte di esempio è 1 0 1 1 0 0 1 0. Le colonne che contengono un 1 sono 128, 32, 16 e 2, quindi contano; le altre vengono saltate. Sommando le colonne che contano si ottiene 128 + 32 + 16 + 2 = 178.</desc> <rect class="dg-bg" x="0.5" y="0.5" width="1039" height="339" rx="16" fill="#ffffff" stroke="#e3e3dd"/> <g class="dg-l1"><path class="dg-stroke" d="M154 38V30H1010V38" stroke="#0369a1" stroke-width="2" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g> <g class="dg-l1"><rect class="dg-bg-plain" x="522.04" y="18" width="119.92" height="24" rx="12" fill="#ffffff"/><text x="582" y="34.5" font-size="13" class="dg-ink" fill="#0369a1" font-weight="600" text-anchor="middle">1 byte = 8 bit</text></g> <text x="24" y="93" font-size="14" class="dg-text" fill="#17171c" font-weight="700">Valore posizionale</text> <text x="24" y="159" font-size="14" class="dg-text" fill="#17171c" font-weight="700">Bit</text> <text x="24" y="225" font-size="14" class="dg-text" fill="#17171c" font-weight="700">Conta?</text> <rect class="dg-card" x="154" y="60" width="100" height="52" rx="10" fill="#f0f0ec" stroke="#cfcfc8"/> <text x="204" y="94" font-size="22" class="dg-text" fill="#17171c" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">128</text> <g class="dg-l1"><rect class="dg-solid" x="154" y="126" width="100" height="52" rx="10" fill="#0369a1"/><text x="204" y="162" font-size="26" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">1</text></g> <text x="204" y="226" font-size="18" class="dg-ink" fill="#15803d" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">128</text> <rect class="dg-card" x="262" y="60" width="100" height="52" rx="10" fill="#f0f0ec" stroke="#cfcfc8"/> <text x="312" y="94" font-size="22" class="dg-text" fill="#17171c" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">64</text> <rect class="dg-card" x="262" y="126" width="100" height="52" rx="10" fill="#ffffff" stroke="#cfcfc8" stroke-width="1.5"/><text x="312" y="162" font-size="26" class="dg-muted" fill="#5c5c68" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">0</text> <text x="312" y="226" font-size="18" class="dg-muted" fill="#5c5c68" font-weight="600" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">-</text> <rect class="dg-card" x="370" y="60" width="100" height="52" rx="10" fill="#f0f0ec" stroke="#cfcfc8"/> <text x="420" y="94" font-size="22" class="dg-text" fill="#17171c" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">32</text> <g class="dg-l1"><rect class="dg-solid" x="370" y="126" width="100" height="52" rx="10" fill="#0369a1"/><text x="420" y="162" font-size="26" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">1</text></g> <text x="420" y="226" font-size="18" class="dg-ink" fill="#15803d" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">32</text> <rect class="dg-card" x="478" y="60" width="100" height="52" rx="10" fill="#f0f0ec" stroke="#cfcfc8"/> <text x="528" y="94" font-size="22" class="dg-text" fill="#17171c" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">16</text> <g class="dg-l1"><rect class="dg-solid" x="478" y="126" width="100" height="52" rx="10" fill="#0369a1"/><text x="528" y="162" font-size="26" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">1</text></g> <text x="528" y="226" font-size="18" class="dg-ink" fill="#15803d" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">16</text> <rect class="dg-card" x="586" y="60" width="100" height="52" rx="10" fill="#f0f0ec" stroke="#cfcfc8"/> <text x="636" y="94" font-size="22" class="dg-text" fill="#17171c" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">8</text> <rect class="dg-card" x="586" y="126" width="100" height="52" rx="10" fill="#ffffff" stroke="#cfcfc8" stroke-width="1.5"/><text x="636" y="162" font-size="26" class="dg-muted" fill="#5c5c68" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">0</text> <text x="636" y="226" font-size="18" class="dg-muted" fill="#5c5c68" font-weight="600" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">-</text> <rect class="dg-card" x="694" y="60" width="100" height="52" rx="10" fill="#f0f0ec" stroke="#cfcfc8"/> <text x="744" y="94" font-size="22" class="dg-text" fill="#17171c" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">4</text> <rect class="dg-card" x="694" y="126" width="100" height="52" rx="10" fill="#ffffff" stroke="#cfcfc8" stroke-width="1.5"/><text x="744" y="162" font-size="26" class="dg-muted" fill="#5c5c68" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">0</text> <text x="744" y="226" font-size="18" class="dg-muted" fill="#5c5c68" font-weight="600" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">-</text> <rect class="dg-card" x="802" y="60" width="100" height="52" rx="10" fill="#f0f0ec" stroke="#cfcfc8"/> <text x="852" y="94" font-size="22" class="dg-text" fill="#17171c" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">2</text> <g class="dg-l1"><rect class="dg-solid" x="802" y="126" width="100" height="52" rx="10" fill="#0369a1"/><text x="852" y="162" font-size="26" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">1</text></g> <text x="852" y="226" font-size="18" class="dg-ink" fill="#15803d" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">2</text> <rect class="dg-card" x="910" y="60" width="100" height="52" rx="10" fill="#f0f0ec" stroke="#cfcfc8"/> <text x="960" y="94" font-size="22" class="dg-text" fill="#17171c" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">1</text> <rect class="dg-card" x="910" y="126" width="100" height="52" rx="10" fill="#ffffff" stroke="#cfcfc8" stroke-width="1.5"/><text x="960" y="162" font-size="26" class="dg-muted" fill="#5c5c68" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">0</text> <text x="960" y="226" font-size="18" class="dg-muted" fill="#5c5c68" font-weight="600" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">-</text> <rect class="dg-panel" x="24" y="252" width="992" height="64" rx="12" fill="#f0f0ec"/> <g class="dg-ok"><g class="dg-icon" transform="translate(42 270) scale(1.1667)" fill="none" stroke="#15803d" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M20 6 9 17l-5-5"/></g></g> <text x="84" y="281" font-size="22" class="dg-text" fill="#17171c" font-weight="700" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">128 + 32 + 16 + 2 = 178</text> <text x="84" y="303" font-size="13" class="dg-muted" fill="#5c5c68" font-weight="500">Somma i valori delle colonne che contengono un 1</text> </svg></figure> *Figura 1. Per leggere un numero binario, somma i valori delle colonne che contengono un 1.* ### Da binario a decimale Per trasformare un numero binario in decimale, scrivilo sotto la riga dei valori posizionali e somma le colonne che contengono un 1. **Esempio pratico: `00101101`** | Valore posizionale | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 | |---|---|---|---|---|---|---|---|---| | Bit | 0 | 0 | 1 | 0 | 1 | 1 | 0 | 1 | Le colonne con un 1 sono 32, 8, 4 e 1. Sommale: 32 + 8 + 4 + 1 = **45**. Quindi `00101101` è 45 in decimale. Due casi facili da ricordare: `00000000` è 0 e `11111111` è 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255, il valore più grande che un byte può contenere. ### Da decimale a binario Per fare il percorso inverso, scorri le colonne da sinistra a destra. A ogni colonna chiediti: «Questo valore posizionale ci sta in quello che mi resta?» Se sì, scrivi `1` e sottrailo. Se no, scrivi `0`. **Esempio pratico: 200** | Passo | Valore posizionale | Ci sta in quello che resta? | Bit | Resta dopo | |---|---|---|---|---| | 1 | 128 | Sì (200 ≥ 128) | 1 | 200 − 128 = 72 | | 2 | 64 | Sì (72 ≥ 64) | 1 | 72 − 64 = 8 | | 3 | 32 | No (8 < 32) | 0 | 8 | | 4 | 16 | No | 0 | 8 | | 5 | 8 | Sì (8 ≥ 8) | 1 | 8 − 8 = 0 | | 6 | 4 | No | 0 | 0 | | 7 | 2 | No | 0 | 0 | | 8 | 1 | No | 0 | 0 | Leggendo la colonna Bit dall'alto in basso si ottiene `11001000`. Controlla tornando indietro: 128 + 64 + 8 = 200. Controlla sempre: bastano cinque secondi. > [!TIP] > Scrivi sempre tutti gli 8 bit, aggiungendo zeri a sinistra (`101` diventa `00000101`). Nelle reti i numeri vengono quasi sempre mostrati come byte completi. > [!CHECK] > Quanto vale `00001010` in decimale? > > - [ ] 5 > - [x] 10 > - [ ] 12 > - [ ] 1010 > > Le colonne con un 1 sono 8 e 2, e 8 + 2 = 10. Le cifre binarie `1010` non significano «milledieci»; ogni cifra vale quanto vale la sua colonna. ### Esadecimale: un modo più breve per scrivere i bit Il binario è esatto, ma lungo: un byte sono 8 cifre e un indirizzo MAC ne sarebbe 48. Copiando tanti zeri e uni è facile sbagliare. L'**esadecimale** (o *hex*) è una scorciatoia. Ha **16** cifre invece di 10: le solite da 0 a 9, poi le lettere da A a F per i valori da dieci a quindici. | Cifra hex | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F | |---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---| | Valore | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | Le lettere possono essere maiuscole o minuscole: `5E` e `5e` sono lo stesso numero. Perché 16? Perché 16 è esattamente 2 × 2 × 2 × 2. Una cifra hex contiene esattamente **4 bit**, e 4 bit (un **nibble**) possono contenere i valori da 0 a 15. Quindi un byte, che è fatto di 8 bit, corrisponde sempre a esattamente **due cifre hex**. Per convertire un byte da binario a hex, dividilo in due gruppi di 4 bit e converti ogni gruppo separatamente, usando i valori posizionali 8, 4, 2, 1: **Esempio pratico: `10110010`** - Il primo gruppo `1011` = 8 + 2 + 1 = 11, che è la cifra hex **B**. - Il secondo gruppo `0010` = 2, che è la cifra **2**. - Il byte è `B2` in hex. Per tornare indietro, trasforma ogni cifra hex in 4 bit. In `C8`, `C` = 12 = `1100` e `8` = `1000`, quindi `C8` è `11001000`: lo stesso 200 della sezione precedente. <figure class="diagram" style="--dg-w: 1040px"><svg xmlns="http://www.w3.org/2000/svg" class="dg" viewBox="0 0 1040 476" width="1040" height="476" role="img" aria-labelledby="binhex-hex-digits-it-title binhex-hex-digits-it-desc" font-family="Inter, system-ui, -apple-system, Segoe UI, Roboto, sans-serif" xml:lang="it"> <title id="binhex-hex-digits-it-title">Un byte sono due cifre esadecimali</title> <desc id="binhex-hex-digits-it-desc">Il byte 1 0 1 1 0 0 1 0 viene diviso in due gruppi di quattro bit. Il primo gruppo, 1011, vale 8 + 2 + 1 = 11, cioè la cifra esadecimale B. L'ultimo gruppo, 0010, vale 2, cioè la cifra 2. Quindi il byte è B2 in esadecimale. Una striscia sotto elenca le sedici cifre esadecimali con i loro valori: da 0 a 9 valgono da 0 a 9, poi A vale 10, B vale 11, C vale 12, D vale 13, E vale 14 e F vale 15.</desc> <rect class="dg-bg" x="0.5" y="0.5" width="1039" height="475" rx="16" fill="#ffffff" stroke="#e3e3dd"/> <g class="dg-l1"><rect class="dg-tint" x="92.0" y="64" width="100" height="50" rx="10" fill="#e1edf4" stroke="#81b4d0" stroke-width="1.2"/><text x="142" y="98" font-size="26" class="dg-ink" fill="#0369a1" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">1</text></g> <g class="dg-l1"><rect class="dg-tint" x="200.0" y="64" width="100" height="50" rx="10" fill="#e1edf4" stroke="#81b4d0" stroke-width="1.2"/><text x="250" y="98" font-size="26" class="dg-ink" fill="#0369a1" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">0</text></g> <g class="dg-l1"><rect class="dg-tint" x="308.0" y="64" width="100" height="50" rx="10" fill="#e1edf4" stroke="#81b4d0" stroke-width="1.2"/><text x="358" y="98" font-size="26" class="dg-ink" fill="#0369a1" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">1</text></g> <g class="dg-l1"><rect class="dg-tint" x="416.0" y="64" width="100" height="50" rx="10" fill="#e1edf4" stroke="#81b4d0" stroke-width="1.2"/><text x="466" y="98" font-size="26" class="dg-ink" fill="#0369a1" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">1</text></g> <g class="dg-l5"><rect class="dg-tint" x="524.0" y="64" width="100" height="50" rx="10" fill="#f7e3ec" stroke="#de8cae" stroke-width="1.2"/><text x="574" y="98" font-size="26" class="dg-ink" fill="#be185d" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">0</text></g> <g class="dg-l5"><rect class="dg-tint" x="632.0" y="64" width="100" height="50" rx="10" fill="#f7e3ec" stroke="#de8cae" stroke-width="1.2"/><text x="682" y="98" font-size="26" class="dg-ink" fill="#be185d" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">0</text></g> <g class="dg-l5"><rect class="dg-tint" x="740.0" y="64" width="100" height="50" rx="10" fill="#f7e3ec" stroke="#de8cae" stroke-width="1.2"/><text x="790" y="98" font-size="26" class="dg-ink" fill="#be185d" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">1</text></g> <g class="dg-l5"><rect class="dg-tint" x="848.0" y="64" width="100" height="50" rx="10" fill="#f7e3ec" stroke="#de8cae" stroke-width="1.2"/><text x="898" y="98" font-size="26" class="dg-ink" fill="#be185d" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">0</text></g> <g class="dg-l1"><path class="dg-stroke" d="M92.0 50H516.0" stroke="#0369a1" stroke-width="2" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g> <g class="dg-l1"><rect class="dg-bg-plain" x="254.96" y="26" width="98.08" height="24" rx="12" fill="#ffffff"/><text x="304" y="42.5" font-size="13" class="dg-ink" fill="#0369a1" font-weight="600" text-anchor="middle">Primi 4 bit</text></g> <g class="dg-l5"><path class="dg-stroke" d="M524.0 50H948.0" stroke="#be185d" stroke-width="2" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g> <g class="dg-l5"><rect class="dg-bg-plain" x="683.32" y="26" width="105.36" height="24" rx="12" fill="#ffffff"/><text x="736" y="42.5" font-size="13" class="dg-ink" fill="#be185d" font-weight="600" text-anchor="middle">Ultimi 4 bit</text></g> <g class="dg-l1"><path class="dg-stroke" d="M304.0 122V160" stroke="#0369a1" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g> <g class="dg-l1"><path class="dg-stroke" d="M298 155l6 8 6-8" stroke="#0369a1" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g> <g class="dg-l1"><rect class="dg-solid" x="264.0" y="172" width="80" height="72" rx="12" fill="#0369a1"/><text x="304" y="224" font-size="44" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">B</text></g> <text x="304" y="268" font-size="15" class="dg-ink" fill="#0369a1" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">1011 = 8 + 2 + 1 = 11 = B</text> <g class="dg-l5"><path class="dg-stroke" d="M736.0 122V160" stroke="#be185d" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g> <g class="dg-l5"><path class="dg-stroke" d="M730 155l6 8 6-8" stroke="#be185d" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g> <g class="dg-l5"><rect class="dg-solid" x="696.0" y="172" width="80" height="72" rx="12" fill="#be185d"/><text x="736" y="224" font-size="44" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">2</text></g> <text x="736" y="268" font-size="15" class="dg-ink" fill="#be185d" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">0010 = 2 = 2</text> <text x="520" y="306" font-size="14" class="dg-muted" fill="#5c5c68" font-weight="600" text-anchor="middle">Il byte 10110010 scritto in esadecimale è B2</text> <rect class="dg-panel" x="24" y="328" width="992" height="124" rx="12" fill="#f0f0ec"/> <text x="44" y="354" font-size="11" class="dg-muted" fill="#5c5c68" font-weight="700" letter-spacing="1.2">LE 16 CIFRE ESADECIMALI E I LORO VALORI</text> <g class="dg-l1"><rect class="dg-tint" x="43" y="370" width="54" height="40" rx="8" fill="#e1edf4" stroke="#81b4d0" stroke-width="1.2"/><text x="70" y="397" font-size="22" class="dg-ink" fill="#0369a1" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">0</text></g> <text x="70" y="432" font-size="14" class="dg-muted" fill="#5c5c68" font-weight="600" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">0</text> <g class="dg-l1"><rect class="dg-tint" x="103" y="370" width="54" height="40" rx="8" fill="#e1edf4" stroke="#81b4d0" stroke-width="1.2"/><text x="130" y="397" font-size="22" class="dg-ink" fill="#0369a1" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">1</text></g> <text x="130" y="432" font-size="14" class="dg-muted" fill="#5c5c68" font-weight="600" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">1</text> <g class="dg-l1"><rect class="dg-tint" x="163" y="370" width="54" height="40" rx="8" fill="#e1edf4" stroke="#81b4d0" stroke-width="1.2"/><text x="190" y="397" font-size="22" class="dg-ink" fill="#0369a1" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">2</text></g> <text x="190" y="432" font-size="14" class="dg-muted" fill="#5c5c68" font-weight="600" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">2</text> <g class="dg-l1"><rect class="dg-tint" x="223" y="370" width="54" height="40" rx="8" fill="#e1edf4" stroke="#81b4d0" stroke-width="1.2"/><text x="250" y="397" font-size="22" class="dg-ink" fill="#0369a1" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">3</text></g> <text x="250" y="432" font-size="14" class="dg-muted" fill="#5c5c68" font-weight="600" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">3</text> <g class="dg-l1"><rect class="dg-tint" x="283" y="370" width="54" height="40" rx="8" fill="#e1edf4" stroke="#81b4d0" stroke-width="1.2"/><text x="310" y="397" font-size="22" class="dg-ink" fill="#0369a1" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">4</text></g> <text x="310" y="432" font-size="14" class="dg-muted" fill="#5c5c68" font-weight="600" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">4</text> <g class="dg-l1"><rect class="dg-tint" x="343" y="370" width="54" height="40" rx="8" fill="#e1edf4" stroke="#81b4d0" stroke-width="1.2"/><text x="370" y="397" font-size="22" class="dg-ink" fill="#0369a1" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">5</text></g> <text x="370" y="432" font-size="14" class="dg-muted" fill="#5c5c68" font-weight="600" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">5</text> <g class="dg-l1"><rect class="dg-tint" x="403" y="370" width="54" height="40" rx="8" fill="#e1edf4" stroke="#81b4d0" stroke-width="1.2"/><text x="430" y="397" font-size="22" class="dg-ink" fill="#0369a1" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">6</text></g> <text x="430" y="432" font-size="14" class="dg-muted" fill="#5c5c68" font-weight="600" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">6</text> <g class="dg-l1"><rect class="dg-tint" x="463" y="370" width="54" height="40" rx="8" fill="#e1edf4" stroke="#81b4d0" stroke-width="1.2"/><text x="490" y="397" font-size="22" class="dg-ink" fill="#0369a1" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">7</text></g> <text x="490" y="432" font-size="14" class="dg-muted" fill="#5c5c68" font-weight="600" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">7</text> <g class="dg-l1"><rect class="dg-tint" x="523" y="370" width="54" height="40" rx="8" fill="#e1edf4" stroke="#81b4d0" stroke-width="1.2"/><text x="550" y="397" font-size="22" class="dg-ink" fill="#0369a1" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">8</text></g> <text x="550" y="432" font-size="14" class="dg-muted" fill="#5c5c68" font-weight="600" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">8</text> <g class="dg-l1"><rect class="dg-tint" x="583" y="370" width="54" height="40" rx="8" fill="#e1edf4" stroke="#81b4d0" stroke-width="1.2"/><text x="610" y="397" font-size="22" class="dg-ink" fill="#0369a1" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">9</text></g> <text x="610" y="432" font-size="14" class="dg-muted" fill="#5c5c68" font-weight="600" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">9</text> <g class="dg-l4"><rect class="dg-tint" x="643" y="370" width="54" height="40" rx="8" fill="#f8e8e2" stroke="#e0a086" stroke-width="1.2"/><text x="670" y="397" font-size="22" class="dg-ink" fill="#c2410c" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">A</text></g> <text x="670" y="432" font-size="14" class="dg-muted" fill="#5c5c68" font-weight="600" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">10</text> <g class="dg-l4"><rect class="dg-tint" x="703" y="370" width="54" height="40" rx="8" fill="#f8e8e2" stroke="#e0a086" stroke-width="1.2"/><text x="730" y="397" font-size="22" class="dg-ink" fill="#c2410c" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">B</text></g> <text x="730" y="432" font-size="14" class="dg-muted" fill="#5c5c68" font-weight="600" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">11</text> <g class="dg-l4"><rect class="dg-tint" x="763" y="370" width="54" height="40" rx="8" fill="#f8e8e2" stroke="#e0a086" stroke-width="1.2"/><text x="790" y="397" font-size="22" class="dg-ink" fill="#c2410c" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">C</text></g> <text x="790" y="432" font-size="14" class="dg-muted" fill="#5c5c68" font-weight="600" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">12</text> <g class="dg-l4"><rect class="dg-tint" x="823" y="370" width="54" height="40" rx="8" fill="#f8e8e2" stroke="#e0a086" stroke-width="1.2"/><text x="850" y="397" font-size="22" class="dg-ink" fill="#c2410c" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">D</text></g> <text x="850" y="432" font-size="14" class="dg-muted" fill="#5c5c68" font-weight="600" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">13</text> <g class="dg-l4"><rect class="dg-tint" x="883" y="370" width="54" height="40" rx="8" fill="#f8e8e2" stroke="#e0a086" stroke-width="1.2"/><text x="910" y="397" font-size="22" class="dg-ink" fill="#c2410c" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">E</text></g> <text x="910" y="432" font-size="14" class="dg-muted" fill="#5c5c68" font-weight="600" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">14</text> <g class="dg-l4"><rect class="dg-tint" x="943" y="370" width="54" height="40" rx="8" fill="#f8e8e2" stroke="#e0a086" stroke-width="1.2"/><text x="970" y="397" font-size="22" class="dg-ink" fill="#c2410c" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">F</text></g> <text x="970" y="432" font-size="14" class="dg-muted" fill="#5c5c68" font-weight="600" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">15</text> </svg></figure> *Figura 2. Un byte sono due cifre hex: converti ogni gruppo di 4 bit separatamente.* > [!NOTE] > I numeri hex a volte si scrivono con un prefisso come `0x` (`0xB2`) o `#` (`#FF8800`) per far capire che sono esadecimali. Il prefisso non fa parte del numero. Alcuni contesti, come un indirizzo MAC, non usano alcun prefisso. > [!CHECK] > Il byte `11110000` viene diviso in due gruppi di 4 bit. Quale numero hex è? > > - [ ] `0F` > - [x] `F0` > - [ ] `E0` > - [ ] `1F` > > Il primo gruppo `1111` = 15 = F e il secondo gruppo `0000` = 0, quindi il byte è `F0`. Mantieni i gruppi nello stesso ordine dei bit. ### Dove incontri questi numeri Ora sai leggere tre cose che prima erano misteriose. **Un indirizzo IPv4** come `192.168.1.10` è fatto di **quattro byte**, scritti in decimale e separati da punti. Questo stile si chiama *decimale puntato* (*dotted decimal*). Ognuno dei quattro numeri è un byte, quindi è compreso tra 0 e 255. Ecco perché `192.168.1.256` non può esistere: 256 non sta in un byte. In totale un indirizzo IPv4 ha 4 × 8 = 32 bit. I computer gestiscono i 32 bit; le persone leggono i quattro numeri decimali. **Un indirizzo MAC** (Media Access Control, controllo di accesso al mezzo) è l'indirizzo hardware di una scheda di rete, usato nella tua rete locale. Sono **sei byte**, ciascuno scritto con due cifre hex, di solito separati da due punti o trattini. L'indirizzo `00:00:5e:00:53:01` è fatto di sei byte: `00`, `00`, `5e`, `00`, `53`, `01`. È uno degli indirizzi riservati agli esempi nella documentazione (RFC 7042), quindi nessuna scheda reale lo possiede. **Un colore web** come `#FF8800` sono **tre byte** in hex: rosso `FF` (255), verde `88` (136) e blu `00` (0). Rosso al massimo, un po' più di metà verde e niente blu danno l'arancione. È un esempio comodo perché puoi scrivere un altro numero hex e vedere il colore cambiare. <figure class="diagram" style="--dg-w: 1040px"><svg xmlns="http://www.w3.org/2000/svg" class="dg" viewBox="0 0 1040 412" width="1040" height="412" role="img" aria-labelledby="binhex-where-it-title binhex-where-it-desc" font-family="Inter, system-ui, -apple-system, Segoe UI, Roboto, sans-serif" xml:lang="it"> <title id="binhex-where-it-title">Dove incontri i byte: indirizzi IP, indirizzi MAC e colori web</title> <desc id="binhex-where-it-desc">Tre righe. Un indirizzo IPv4 come 192.168.1.10 è fatto di quattro byte, ognuno scritto in decimale da 0 a 255 e separati da punti. Un indirizzo MAC come 00:00:5e:00:53:01 è fatto di sei byte, ognuno scritto con due cifre esadecimali e separati da due punti. Un colore web come #FF8800 è fatto di tre byte in esadecimale per rosso, verde e blu, cioè 255, 136 e 0, con un campione di colore arancione accanto.</desc> <rect class="dg-bg" x="0.5" y="0.5" width="1039" height="411" rx="16" fill="#ffffff" stroke="#e3e3dd"/> <rect class="dg-card" x="24" y="24" width="992" height="112" rx="12" fill="#ffffff" stroke="#cfcfc8"/> <g class="dg-l1"><g class="dg-icon" transform="translate(42 42) scale(1.2500)" fill="none" stroke="#0369a1" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"/><path d="M12 2a14.5 14.5 0 0 0 0 20 14.5 14.5 0 0 0 0-20"/><path d="M2 12h20"/></g></g> <g class="dg-l1"><text x="84" y="64" font-size="15" class="dg-ink" fill="#0369a1" font-weight="700">Indirizzo IPv4</text></g> <text x="42" y="92" font-size="13" class="dg-muted" fill="#5c5c68" font-weight="500">4 byte, ognuno scritto in decimale</text> <text x="42" y="112" font-size="13" class="dg-muted" fill="#5c5c68" font-weight="500">(da 0 a 255), separati da punti</text> <g class="dg-l1"><rect class="dg-tint" x="548" y="54" width="90" height="50" rx="10" fill="#e1edf4" stroke="#81b4d0" stroke-width="1.2"/><text x="593" y="88" font-size="24" class="dg-ink" fill="#0369a1" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">192</text></g> <text x="651" y="90" font-size="26" class="dg-text" fill="#17171c" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">.</text> <g class="dg-l1"><rect class="dg-tint" x="664" y="54" width="90" height="50" rx="10" fill="#e1edf4" stroke="#81b4d0" stroke-width="1.2"/><text x="709" y="88" font-size="24" class="dg-ink" fill="#0369a1" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">168</text></g> <text x="767" y="90" font-size="26" class="dg-text" fill="#17171c" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">.</text> <g class="dg-l1"><rect class="dg-tint" x="780" y="54" width="90" height="50" rx="10" fill="#e1edf4" stroke="#81b4d0" stroke-width="1.2"/><text x="825" y="88" font-size="24" class="dg-ink" fill="#0369a1" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">1</text></g> <text x="883" y="90" font-size="26" class="dg-text" fill="#17171c" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">.</text> <g class="dg-l1"><rect class="dg-tint" x="896" y="54" width="90" height="50" rx="10" fill="#e1edf4" stroke="#81b4d0" stroke-width="1.2"/><text x="941" y="88" font-size="24" class="dg-ink" fill="#0369a1" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">10</text></g> <rect class="dg-card" x="24" y="150" width="992" height="112" rx="12" fill="#ffffff" stroke="#cfcfc8"/> <g class="dg-l2"><g class="dg-icon" transform="translate(42 168) scale(1.2500)" fill="none" stroke="#15803d" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M12 20h.01"/><path d="M2 8.82a15 15 0 0 1 20 0"/><path d="M5 12.86a10 10 0 0 1 14 0"/><path d="M8.5 16.43a5 5 0 0 1 7 0"/></g></g> <g class="dg-l2"><text x="84" y="190" font-size="15" class="dg-ink" fill="#15803d" font-weight="700">Indirizzo MAC</text></g> <text x="42" y="218" font-size="13" class="dg-muted" fill="#5c5c68" font-weight="500">6 byte, ognuno scritto con due cifre</text> <text x="42" y="238" font-size="13" class="dg-muted" fill="#5c5c68" font-weight="500">esadecimali, separati da due punti</text> <g class="dg-l2"><rect class="dg-tint" x="472" y="180" width="64" height="50" rx="10" fill="#e3f0e8" stroke="#8ac09e" stroke-width="1.2"/><text x="504" y="214" font-size="24" class="dg-ink" fill="#15803d" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">00</text></g> <text x="549" y="216" font-size="26" class="dg-text" fill="#17171c" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">:</text> <g class="dg-l2"><rect class="dg-tint" x="562" y="180" width="64" height="50" rx="10" fill="#e3f0e8" stroke="#8ac09e" stroke-width="1.2"/><text x="594" y="214" font-size="24" class="dg-ink" fill="#15803d" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">00</text></g> <text x="639" y="216" font-size="26" class="dg-text" fill="#17171c" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">:</text> <g class="dg-l2"><rect class="dg-tint" x="652" y="180" width="64" height="50" rx="10" fill="#e3f0e8" stroke="#8ac09e" stroke-width="1.2"/><text x="684" y="214" font-size="24" class="dg-ink" fill="#15803d" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">5e</text></g> <text x="729" y="216" font-size="26" class="dg-text" fill="#17171c" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">:</text> <g class="dg-l2"><rect class="dg-tint" x="742" y="180" width="64" height="50" rx="10" fill="#e3f0e8" stroke="#8ac09e" stroke-width="1.2"/><text x="774" y="214" font-size="24" class="dg-ink" fill="#15803d" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">00</text></g> <text x="819" y="216" font-size="26" class="dg-text" fill="#17171c" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">:</text> <g class="dg-l2"><rect class="dg-tint" x="832" y="180" width="64" height="50" rx="10" fill="#e3f0e8" stroke="#8ac09e" stroke-width="1.2"/><text x="864" y="214" font-size="24" class="dg-ink" fill="#15803d" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">53</text></g> <text x="909" y="216" font-size="26" class="dg-text" fill="#17171c" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">:</text> <g class="dg-l2"><rect class="dg-tint" x="922" y="180" width="64" height="50" rx="10" fill="#e3f0e8" stroke="#8ac09e" stroke-width="1.2"/><text x="954" y="214" font-size="24" class="dg-ink" fill="#15803d" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">01</text></g> <rect class="dg-card" x="24" y="276" width="992" height="112" rx="12" fill="#ffffff" stroke="#cfcfc8"/> <g class="dg-l4"><g class="dg-icon" transform="translate(42 294) scale(1.2500)" fill="none" stroke="#c2410c" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M15 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V7Z"/><path d="M14 2v4a2 2 0 0 0 2 2h4"/><path d="M10 9H8"/><path d="M16 13H8"/><path d="M16 17H8"/></g></g> <g class="dg-l4"><text x="84" y="316" font-size="15" class="dg-ink" fill="#c2410c" font-weight="700">Colore web</text></g> <text x="42" y="344" font-size="13" class="dg-muted" fill="#5c5c68" font-weight="500">3 byte in esadecimale dopo il simbolo #:</text> <text x="42" y="364" font-size="13" class="dg-muted" fill="#5c5c68" font-weight="500">rosso, verde, blu (255, 136, 0)</text> <text x="638" y="339" font-size="30" class="dg-text" fill="#17171c" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">#</text> <g class="dg-l4"><rect class="dg-tint" x="656" y="306" width="90" height="50" rx="10" fill="#f8e8e2" stroke="#e0a086" stroke-width="1.2"/><text x="701" y="340" font-size="24" class="dg-ink" fill="#c2410c" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">FF</text></g> <g class="dg-l4"><rect class="dg-tint" x="756" y="306" width="90" height="50" rx="10" fill="#f8e8e2" stroke="#e0a086" stroke-width="1.2"/><text x="801" y="340" font-size="24" class="dg-ink" fill="#c2410c" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">88</text></g> <g class="dg-l4"><rect class="dg-tint" x="856" y="306" width="90" height="50" rx="10" fill="#f8e8e2" stroke="#e0a086" stroke-width="1.2"/><text x="901" y="340" font-size="24" class="dg-ink" fill="#c2410c" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">00</text></g> <rect x="960" y="306" width="50" height="50" rx="10" fill="#ff8800" stroke="#cfcfc8"/> </svg></figure> *Figura 3. Notazioni diverse, stessa idea: pochi byte scritti in modo leggibile.* > [!CHECK] > Perché `10.20.30.300` non è un indirizzo IPv4 valido? > > - [ ] Perché ha troppi punti > - [x] Perché 300 è maggiore di 255, il valore massimo di un byte > - [ ] Perché gli indirizzi non possono iniziare con 10 > - [ ] Perché gli indirizzi IPv4 devono essere scritti in hex > > Ognuno dei quattro numeri è un byte, quindi deve essere compreso tra 0 e 255. Il decimale puntato è il modo usuale di scrivere gli indirizzi IPv4. ## Come funziona Mettendo tutto insieme, ecco l'intero percorso di un indirizzo, `192.168.1.10`, da ciò che leggi a ciò che il computer memorizza. 1. Leggi quattro numeri decimali: 192, 168, 1 e 10. 2. Ognuno è un byte. Con il metodo delle sottrazioni: 192 = 128 + 64 = `11000000`; 168 = 128 + 32 + 8 = `10101000`; 1 = `00000001`; 10 = 8 + 2 = `00001010`. 3. Il computer memorizza i quattro byte uno dopo l'altro, come 32 bit: `11000000 10101000 00000001 00001010`. 4. Se volessi una forma più breve, scriveresti ogni byte in hex: `C0 A8 01 0A`. Lungo la strada non cambia nulla. È lo stesso numero, scritto in tre modi diversi. Scegli il modo più adatto a chi legge: bit per il computer, decimale per gli indirizzi IP, hex per le cose compatte come gli indirizzi MAC. > [!CHECK] > Quale affermazione è vera su `192.168.1.10`, `11000000.10101000.00000001.00001010` e `C0.A8.01.0A`? > > - [ ] Sono tre indirizzi diversi > - [x] Sono lo stesso indirizzo scritto in decimale, binario e hex > - [ ] Solo il primo può essere usato in una rete > - [ ] La forma hex è un altro indirizzo, più corto > > Decimale, binario ed esadecimale sono solo notazioni diverse. Il valore memorizzato nel computer è lo stesso. ## Esempio pratico Convertiamo l'indirizzo `203.0.113.5`, uno degli esempi riservati alla documentazione (RFC 5737). | Decimale | Valori posizionali usati | Binario | |---|---|---| | 203 | 128 + 64 + 8 + 2 + 1 | `11001011` | | 0 | nessuno | `00000000` | | 113 | 64 + 32 + 16 + 1 | `01110001` | | 5 | 4 + 1 | `00000101` | Controlliamo 203: 203 − 128 = 75; 75 − 64 = 11; 11 − 8 = 3; 3 − 2 = 1; 1 − 1 = 0. I bit che ci stanno sono 128, 64, 8, 2 e 1, che danno `11001011`. L'indirizzo completo in binario è `11001011.00000000.01110001.00000101`. ## Errori comuni - **Leggere il binario come un numero decimale** — `1010` non è milledieci; è 10. Usa sempre le colonne. - **Dimenticare gli zeri iniziali** — `101` e `00000101` sono lo stesso numero, ma nelle reti servono tutti gli 8 bit per mantenere allineate le colonne. - **Confondere le lettere e i valori hex** — da `A` a `F` sono i valori da 10 a 15, non da 1 a 6. E `10` in hex è 16, non dieci. - **Pensare che l'hex sia un numero diverso** — `FF` e `255` sono lo stesso valore. Cambia la notazione, non il numero. - **Dare per scontato che un numero sia decimale quando non lo è** — `10` può significare dieci (decimale), due (binario) o sedici (hex). Lo dicono il contesto o un prefisso come `0x`. ## Considerazioni sulla sicurezza Binario ed esadecimale non sono di per sé un tema di sicurezza, ma alcune abitudini aiutano. Quando copi un indirizzo IP o MAC in una regola del firewall o in una lista di controllo degli accessi, una sola cifra sbagliata fa puntare la regola a un altro dispositivo, quindi ricontrolla rileggendolo byte per byte. Ricorda anche che un indirizzo MAC non è un segreto né una prova di identità: si può cambiare via software, quindi non fare mai affidamento solo su di esso per decidere chi può accedere a una rete. Infine, i dump esadecimali dei dati non sono cifrati. Scrivere qualcosa in hex o in binario ne cambia solo l'aspetto, non chi può leggerlo. ## Riepilogo - I computer usano due stati (acceso e spento), scritti 1 e 0. Una cifra è un bit; 8 bit formano un byte, che contiene 256 valori (da 0 a 255). - Le colonne binarie valgono 128, 64, 32, 16, 8, 4, 2, 1. Per passare da binario a decimale, somma le colonne con un 1. Per passare da decimale a binario, sottrai, da sinistra a destra, il più grande valore posizionale che ci sta. - L'esadecimale ha 16 cifre (da 0 a 9 e da A a F). Una cifra hex sono 4 bit, quindi un byte sono due cifre hex. - Un indirizzo IPv4 sono quattro byte in decimale, separati da punti. Un indirizzo MAC sono sei byte in hex. Un colore web sono tre byte in hex. ## Glossario | Termine | Definizione | |---|---| | bit | Una cifra binaria: un singolo 0 o 1. La più piccola unità di dati di un computer. | | byte | Un gruppo di 8 bit. Può contenere 256 valori diversi, da 0 a 255. | | binario | Un sistema numerico con solo due cifre, 0 e 1. Ogni colonna vale il doppio di quella alla sua destra. | | decimale | Il sistema numerico di tutti i giorni, con dieci cifre, da 0 a 9. Ogni colonna vale dieci volte quella alla sua destra. | | esadecimale (hex) | Un sistema numerico con 16 cifre, da 0 a 9 e da A a F. Una cifra hex rappresenta 4 bit. | | nibble | Un gruppo di 4 bit, cioè mezzo byte e esattamente una cifra hex. | | indirizzo MAC | Media Access Control address: l'indirizzo hardware di sei byte di una scheda di rete, scritto in hex, per esempio `00:00:5e:00:53:01`. | | decimale puntato | Il modo usuale di scrivere un indirizzo IPv4: quattro byte in decimale separati da punti, per esempio `192.168.1.10`. | ## Verifica la comprensione 1. Converti 77 in binario, poi in hex. Controlla la risposta binaria risommando le colonne. 2. Un amico dice: «`11111111` è un numero enorme, quindi non sta in un byte». Che numero è, e il tuo amico ha ragione? 3. Nell'indirizzo MAC `00:00:5e:00:53:01`, quanti byte ci sono e quanto vale in decimale il terzo byte? ## Riferimenti - [RFC 791](https://www.rfc-editor.org/rfc/rfc791): Internet Protocol (dove è definito l'indirizzo IPv4 a 32 bit) - [RFC 7042](https://www.rfc-editor.org/rfc/rfc7042): IANA considerations for IEEE 802 parameters (l'intervallo di indirizzi MAC per la documentazione) - [RFC 5737](https://www.rfc-editor.org/rfc/rfc5737): IPv4 address blocks reserved for documentation