20.05.2013 Views

numeri interi relativi in complemento a uno

numeri interi relativi in complemento a uno

numeri interi relativi in complemento a uno

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Materiale di approfondimento:<br />

<strong>numeri</strong> <strong><strong>in</strong>teri</strong> <strong>relativi</strong> <strong>in</strong><br />

<strong>complemento</strong> a <strong>uno</strong><br />

Federico Cerutti<br />

AA. 2011/2012<br />

Modulo di Elementi di Informatica e Programmazione<br />

http://apollo.<strong>in</strong>g.unibs.it/fip/<br />

2011 Federico Cerutti


Complemento a <strong>uno</strong><br />

2 n-1 -1<br />

−(2 n-1 -1)<br />

01111111<br />

00000010<br />

00000001<br />

00000000<br />

<strong>numeri</strong> positivi<br />

+2<br />

+1<br />

+0<br />

10000000 11111111 −0<br />

11111110 −1<br />

11111101 −2<br />

<strong>numeri</strong> negativi<br />

Elementi di Informatica e Programmazione, A.A. 2011/12 Federico Cerutti 2


Complemento a <strong>uno</strong><br />

Def<strong>in</strong>izione<br />

Nella codifica <strong>in</strong> <strong>complemento</strong> a <strong>uno</strong>, i <strong>numeri</strong> positivi si codificano<br />

normalmente secondo la codifica b<strong>in</strong>aria naturale, i <strong>numeri</strong> negativi come il<br />

<strong>complemento</strong> a <strong>uno</strong> del positivo corrispondente. Avendo a disposizione n bit<br />

per la rappresentazione dei <strong>numeri</strong> <strong><strong>in</strong>teri</strong> <strong>relativi</strong>, il <strong>complemento</strong> a <strong>uno</strong> di<br />

un numero b<strong>in</strong>ario N è def<strong>in</strong>ito come 2 n − 1 − N<br />

Esempio<br />

Con un codice a 8 bit il numero (+21)10 è rappresentato da (00010101)2, il<br />

numero (−21)10 da (11101010)2.<br />

Il bit più significativo di un numero positivo è 0, quello di un numero<br />

negativo è 1.<br />

Il valore 0 ha due rappresentazioni dist<strong>in</strong>te corrispondenti a +0 e −0,<br />

ovvero con un codice a 8 bit 00000000 e 11111111.<br />

Con un codice a n bit, i valori rappresentabili vanno da −(2 n−1 − 1) a<br />

+(2 n−1 − 1).<br />

Elementi di Informatica e Programmazione, A.A. 2011/12 Federico Cerutti 3


Complemento a <strong>uno</strong><br />

Esempio<br />

Sia N = 10010100. Il <strong>complemento</strong> a <strong>uno</strong> di C1 di N è:<br />

11111111 −<br />

C1 = 10010100<br />

01101011<br />

=<br />

REGOLA PRATICA EQUIVALENTE<br />

Si complementano (<strong>in</strong>vertono) i valori di tutti i bit.<br />

N.B. Anche nel caso del <strong>complemento</strong> a <strong>uno</strong> valgono simmetria del<br />

<strong>complemento</strong> a <strong>uno</strong> e la regola sull’estensione del segno.<br />

Elementi di Informatica e Programmazione, A.A. 2011/12 Federico Cerutti 4


Aritmetica <strong>in</strong> <strong>complemento</strong> a <strong>uno</strong><br />

Addizione: l’addizione di due <strong>numeri</strong> rappresentati <strong>in</strong><br />

<strong>complemento</strong> a 1 dà il risultato corretto sommando al risultato<br />

ottenuto il riporto (a patto che il risultato sia entro il range dei<br />

<strong>numeri</strong> rappresentabili)<br />

Si ha overflow se i riporti generati nelle due posizioni più<br />

significative (tenendo conto anche di quelli generati nella somma<br />

del riporto!) sono diversi. In modo equivalente, si possono<br />

controllare i segni come nel caso del <strong>complemento</strong> a due.<br />

Sottrazione: per sottrarre un numero basta sommare il suo<br />

<strong>complemento</strong> a <strong>uno</strong>.<br />

Elementi di Informatica e Programmazione, A.A. 2011/12 Federico Cerutti 5


Aritmetica <strong>in</strong> <strong>complemento</strong> a <strong>uno</strong>: esempi<br />

Esempio<br />

Calcolare (−99)10 + (−44)10 con i <strong>numeri</strong> codificati <strong>in</strong> b<strong>in</strong>ario con<br />

<strong>complemento</strong> a <strong>uno</strong> con 8 bit (N.B. i <strong>numeri</strong> codificabili <strong>in</strong><br />

<strong>complemento</strong> a <strong>uno</strong> con 8 bit appartengono all’<strong>in</strong>tervallo [−127, 127]).<br />

(99)10 ↦ (01100011)2; (−99)10 ↦ (10011100)2. (44)10 ↦ (00101100)2;<br />

(−44)10 ↦ (11010011)2.<br />

10011100 +<br />

11100011 =<br />

(−99)10 + (−44)10 ↦<br />

(1) 01111111<br />

00000001<br />

10000000<br />

11<br />

+<br />

=<br />

(10000000)2 = (−01111111)2 ↦ (−127)10.<br />

Elementi di Informatica e Programmazione, A.A. 2011/12 Federico Cerutti 6


Aritmetica <strong>in</strong> <strong>complemento</strong> a <strong>uno</strong>: esempi<br />

Esempio<br />

Calcolare (−99)10 + (−54)10 con i <strong>numeri</strong> codificati <strong>in</strong> b<strong>in</strong>ario con<br />

<strong>complemento</strong> a <strong>uno</strong> con 8 bit.<br />

(99)10 ↦ (01100011)2; (−99)10 ↦ (10011100)2. (54)10 ↦ (00110110)2;<br />

(−54)10 ↦ (11001001)2.<br />

10011100 +<br />

11001001 =<br />

(−99)10 + (−54)10 ↦<br />

(1) 01100101<br />

00000001<br />

01100110<br />

10<br />

+<br />

=<br />

(01100110)2 ↦ (102)10???<br />

Elementi di Informatica e Programmazione, A.A. 2011/12 Federico Cerutti 7


Complemento a <strong>uno</strong><br />

Esercizio da appello<br />

Rappresentare i <strong>numeri</strong> −27 e −9 <strong>in</strong> <strong>complemento</strong> a <strong>uno</strong> con 8 bit e con 6<br />

bit. Eseguire la somma <strong>in</strong> entrambe le rappresentazioni e commentare i<br />

rispettivi risultati.<br />

Soluzione<br />

A 6 bit<br />

(27)10 ↦ (011011)2; (−27)10 ↦ (100100)2. (9)10 ↦ (001001)2;<br />

(−9)10 ↦ (110110)2.<br />

(−27)10 + (−9)10 ↦<br />

100100 +<br />

110110 =<br />

(1) 011010 +<br />

000001 =<br />

011011<br />

10<br />

Ho overflow perché i riporti sono diversi (oppure: perché sommando due<br />

negativi ottengo un positivo).<br />

Elementi di Informatica e Programmazione, A.A. 2011/12 Federico Cerutti 8


Complemento a <strong>uno</strong><br />

Esercizio da appello<br />

Rappresentare i <strong>numeri</strong> −27 e −9 <strong>in</strong> <strong>complemento</strong> a <strong>uno</strong> con 8 bit e con 6<br />

bit. Eseguire la somma <strong>in</strong> entrambe le rappresentazioni e commentare i<br />

rispettivi risultati.<br />

Soluzione<br />

A 8 bit<br />

I corrispettivi <strong>numeri</strong> a 8 bit li ottengo estendendo il segno:<br />

(−27)10 ↦ (11100100)2; (−9)10 ↦ (11110110)2.<br />

11100100 +<br />

11110110 =<br />

(−27)10 + (−9)10 ↦<br />

(1) 11011010<br />

00000001<br />

11011011<br />

11<br />

+<br />

=<br />

Non c’è overflow perché i due riporti sono uguali.<br />

Elementi di Informatica e Programmazione, A.A. 2011/12 Federico Cerutti 9


Complemento a <strong>uno</strong><br />

Esercizio da appello<br />

Rappresentare i <strong>numeri</strong> decimali −65 e 18 <strong>in</strong> notazione b<strong>in</strong>aria <strong>in</strong><br />

<strong>complemento</strong> a <strong>uno</strong> con 8 bit. Eseguire la somma algebrica dei <strong>numeri</strong> così<br />

ottenuti e commentare il risultato.<br />

Soluzione<br />

(65)10 = (01000001)2; (−65)10 ↦ (10111110)2. (18)10 ↦ (00010010)2.<br />

10111110 +<br />

00010010 =<br />

(−65)10 + (18)10 ↦<br />

(0) 11010100<br />

00000001<br />

11010101<br />

00<br />

+<br />

=<br />

Non ho overflow perché i riporti sono uguali (non c’è riporto <strong>in</strong> alcuna delle<br />

posizioni n e n − 1). Infatti nella rappresentazione a <strong>complemento</strong> (sia esso<br />

<strong>complemento</strong> a <strong>uno</strong> o <strong>complemento</strong> a due) non è possibile ottenere l’overflow<br />

sommando un numero positivo ed un numero negativo.<br />

Elementi di Informatica e Programmazione, A.A. 2011/12 Federico Cerutti 10

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!