18.08.2013 Views

Dalla A alla Z passando per C - Robotica

Dalla A alla Z passando per C - Robotica

Dalla A alla Z passando per C - Robotica

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.

6.24 AND bit-a-bit<br />

o<strong>per</strong>atore &<br />

“e” commerciale<br />

sintassi espr1 & espr2<br />

n. o<strong>per</strong>andi 2<br />

utilizzo il bit i-esimo del risultato corrisponde all’AND tra il bit i-esimo<br />

di espr1 e quello di espr2<br />

associativita’ =⇒<br />

commutativita’ SI’<br />

L’AND tra due valori binari vale 1 soltanto se entrambi i valori sono 1.<br />

Esempio:<br />

int lowbyte = val & 0xff;<br />

int highbyte = val & 0xff00;<br />

L’o<strong>per</strong>azione di AND bit-a-bit si dice talvolta mascheratura, e viene detta maschera la sequenza<br />

di bit che viene “applicata” al dato.<br />

6.25 XOR bit-a-bit<br />

o<strong>per</strong>atore<br />

∧<br />

accento circonflesso<br />

sintassi espr1 ∧ espr2<br />

n. o<strong>per</strong>andi 2<br />

utilizzo il bit i-esimo del risultato corrisponde allo XOR tra il bit iesimo<br />

di espr1 e quello di espr2<br />

associativita’ =⇒<br />

commutativita’ SI’<br />

Il risultato dello XOR tra due valori binari vale 1 se entrambi i valori sono diversi.<br />

while ( /* condizione */ ) {<br />

/* calcolo */<br />

led = led ^ 1; /* inversione del bit più basso */<br />

}<br />

Se led vale 11010001, dopo l’o<strong>per</strong>azione diventa 11010000 e viceversa.<br />

69

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

Saved successfully!

Ooh no, something went wrong!