Codifica dei numeri
Codifica dei numeri
Codifica dei numeri
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Complemento a 2<br />
• La rappresentazione in complemento a 2 è quella adottata dai calcolatori<br />
• Il bit più significativo corrisponde al segno (0 positivo, 1 negativo)<br />
• MIPS: Numeri naturali (con segno) di 32 bit:<br />
0000 0000 0000 0000 0000 0000 0000 0000 two = 0 ten<br />
0000 0000 0000 0000 0000 0000 0000 0001 two = + 1 ten<br />
0000 0000 0000 0000 0000 0000 0000 0010 two = + 2 ten<br />
...<br />
0111 1111 1111 1111 1111 1111 1111 1110 two = + 2,147,483,646 ten<br />
0111 1111 1111 1111 1111 1111 1111 1111 two = + 2,147,483,647 ten<br />
1000 0000 0000 0000 0000 0000 0000 0000 two = – 2,147,483,648 ten<br />
1000 0000 0000 0000 0000 0000 0000 0001 two = – 2,147,483,647 ten<br />
1000 0000 0000 0000 0000 0000 0000 0010 two = – 2,147,483,646 ten<br />
...<br />
1111 1111 1111 1111 1111 1111 1111 1101 two = – 3 ten<br />
1111 1111 1111 1111 1111 1111 1111 1110 two = – 2 ten<br />
1111 1111 1111 1111 1111 1111 1111 1111 two = – 1 ten<br />
Complemento a 2<br />
• Il valore corrispondente alla rappresentazione <strong>dei</strong> <strong>numeri</strong> positivi è quella<br />
solita<br />
• Per quanto riguarda i <strong>numeri</strong> negativi, per ottenerne direttamente il valore,<br />
basta considerare<br />
– il bit di segno (=1) con peso -2 n-1<br />
– tutti gli altri bit in posizione i con peso 2 i<br />
• Dimostrazione:<br />
– -|N| viene rappresentato in complemento a 2 dal<br />
numero unsigned 2 n -|N|<br />
– se supponiamo che 2 n -|N| corrisponda alla n-upla<br />
1 d n-2 … d 1 d 0 ⇒<br />
2 n -|N| = 2 n-1 + 2 n-2 * d n-2 + … + 2 1 * d 1 + 2 0 * d 0<br />
-|N| = -2 n + 2 n-1 + 2 n-2 * d n-2 + … + 2 1 * d 1 + 2 0 * d 0<br />
-|N| = -2 n-1 + 2 n-2 * d n-2 + … + 2 1 * d 1 + 2 0 * d 0<br />
maxint<br />
minint<br />
Arch. Elab. - S. Orlando 17<br />
Arch. Elab. - S. Orlando 19<br />
Complemento a 2<br />
• Rappresentazione di <strong>numeri</strong> in complemento a 2 su n bit<br />
• Su n bit sono rappresentabili 2 n <strong>numeri</strong> unsigned (da 0 a 2 n - 1)<br />
• Numeri signed<br />
– 0: 0……….0<br />
– 2 n-1 -1 <strong>numeri</strong> positivi:<br />
• 1 (0 ………01)<br />
• 2 n-1 -1 (massimo) (01……..11)<br />
– 2 n-1 <strong>numeri</strong> negativi<br />
• -|N| rappresentato dal numero unsigned: 2 n -|N|<br />
• -1: 2 n -1 (1....…...1)<br />
• -2 n-1 (minimo): 2 n - 2 n-1 = 2 n-1 (10....…0)<br />
Complemento a 2<br />
Arch. Elab. - S. Orlando 18<br />
• Dato un numero positivo N, con bit di segno uguale a 0<br />
• Per ottenere la rappresentazione in complemento a 2 di -N è<br />
possibile impiegare equivalentemente<br />
– Alg. 1: inverti tutti i bit (ovvero Complementa a uno) e somma 1<br />
– Alg. 2: inverti tutti i bit a sinistra della cifra “1” meno significativa<br />
Arch. Elab. - S. Orlando 20