outline
play

Outline Integer representa+on and opera+ons Bit opera+ons - PowerPoint PPT Presentation

Outline Integer representa+on and opera+ons Bit opera+ons Floa+ng point numbers Reading Assignment: Chapter 2 Integer & Float Number Representa+on


  1. Outline ¡ • Integer ¡representa+on ¡and ¡opera+ons ¡ • Bit ¡opera+ons ¡ • Floa+ng ¡point ¡numbers ¡ Reading ¡Assignment: ¡ – Chapter ¡2 ¡Integer ¡& ¡Float ¡Number ¡Representa+on ¡ related ¡content ¡(Sec+on ¡2.2, ¡2.3, ¡2.4) ¡ 1 ¡

  2. Why ¡we ¡need ¡to ¡study ¡the ¡low ¡level ¡ representa4on? ¡ 1 ¡and ¡3 ¡ è ¡exclusive ¡OR ¡(^) ¡ 2 ¡and ¡4 ¡ è ¡and ¡(&) ¡ 5 ¡ è ¡or ¡(|) ¡ 01100 ¡ ¡carry* ¡ * ¡Always ¡start ¡ ¡ ¡0110 ¡ ¡a ¡ with ¡a ¡carry-­‑in ¡ of ¡0 ¡ ¡ ¡0111 ¡ ¡b ¡ 01101 ¡ ¡a+b ¡ Did ¡it ¡work? ¡ What ¡is ¡a? ¡ ¡ What ¡is ¡b? ¡ What ¡is ¡a+b? ¡ What ¡if ¡8 ¡bits ¡instead ¡of ¡4? ¡ 2 ¡

  3. Integer ¡Representa4on ¡ • Different ¡encoding ¡scheme ¡than ¡float ¡ • *Total ¡number ¡of ¡values: ¡2 w ¡ – where ¡w ¡is ¡the ¡bit ¡width ¡of ¡the ¡data ¡type ¡ • The ¡le[-­‑most ¡bit ¡is ¡the ¡sign ¡bit ¡if ¡using ¡a ¡signed ¡data ¡type ¡ (typically… ¡B2T). ¡ • Unsigned ¡ à ¡non-­‑neg ¡numbers ¡(>=0) ¡ – Minimum ¡value: ¡0 ¡ – *Maximum ¡value: ¡2 w -­‑1 ¡ • Signed ¡ à ¡neg, ¡zero, ¡and ¡pos ¡numbers ¡ – *Minimum ¡value: ¡-­‑2 w-­‑1 ¡ – *Maximum ¡value: ¡2 w-­‑1 -­‑1 ¡ ¡ ¡ * ¡Where ¡w ¡is ¡the ¡bit ¡width ¡of ¡the ¡data ¡type ¡ 3 ¡

  4. Integer ¡Decoding ¡ • Binary ¡to ¡Decimal ¡(mult ¡of ¡powers) ¡ CODE B2U B2T 0000 0 0 • Unsigned ¡= ¡simple ¡binary ¡= ¡B2U ¡ 0001 1 1 0010 2 2 – You ¡already ¡know ¡how ¡to ¡do ¡this ¡:o) ¡ 0011 3 3 • 0101 ¡= ¡5, ¡1111 ¡= ¡F, ¡1110 ¡= ¡E, ¡1001 ¡= ¡9 ¡ 0100 4 4 • Signed ¡= ¡two’s ¡complement ¡= ¡B2T* ¡ 0101 5 5 0110 6 6 – 0 ¡101 ¡= ¡unsigned ¡= ¡5 ¡ 0111 7 7 – 1 ¡111 ¡= ¡-­‑1*2 3 ¡+ ¡7 ¡= ¡-­‑8 ¡+ ¡7 ¡= ¡-­‑1 ¡ 1000 8 -8 1001 9 -7 – 1 ¡110 ¡= ¡-­‑1*2 3 ¡+ ¡6 ¡= ¡-­‑8 ¡+ ¡6 ¡= ¡-­‑2 ¡ 1010 10 -6 – 1 ¡001 ¡= ¡-­‑1*2 3 ¡+ ¡1 ¡= ¡-­‑8 ¡+ ¡1 ¡= ¡-­‑7 ¡ 1011 11 -5 1100 12 -4 – Another ¡way, ¡if ¡sign ¡bit ¡= ¡1 ¡ 1101 13 -3 • invert ¡bits ¡and ¡add ¡1 ¡ 1110 14 -2 1111 15 -1 * ¡reminder: ¡le[ ¡most ¡bit ¡is ¡sign ¡bit ¡ 4 ¡

  5. B2O ¡& ¡B2S ¡ • One’s ¡complement ¡= ¡bit ¡ CODE B2U B2T B2O B2S complement ¡of ¡B2U ¡ 0000 0 0 0 0 0001 1 1 1 1 • Signed ¡Magnitude ¡= ¡le[ ¡ 0010 2 2 2 2 0011 3 3 3 3 most ¡bit ¡set ¡to ¡1 ¡with ¡B2U ¡ 0100 4 4 4 4 for ¡the ¡remaining ¡bits ¡ 0101 5 5 5 5 0110 6 6 6 6 • Both ¡include ¡neg ¡values ¡ 0111 7 7 7 7 • Min/max ¡= ¡-­‑(2 w-­‑1 -­‑1) ¡ ¡to ¡ ¡ 1000 8 -8 -7 -0 1001 9 -7 -6 -1 2 w-­‑1 -­‑1 ¡ 1010 10 -6 -5 -2 • Pos ¡and ¡neg ¡zero ¡ 1011 11 -5 -4 -3 1100 12 -4 -3 -4 • Difficul+es ¡with ¡arithme+c ¡ 1101 13 -3 -2 -5 op+ons ¡ 1110 14 -2 -1 -6 1111 15 -1 -0 -7 5 ¡

  6. Signed ¡vs ¡Unsigned ¡ • Cas+ng… ¡ ¡ CODE B2U B2T 0000 0 0 • Signed ¡to ¡unsigned… ¡ ¡ 0001 1 1 • Unsigned ¡to ¡signed… ¡ 0010 2 2 0011 3 3 ¡ 0100 4 4 *** ¡Changes ¡the ¡meaning ¡of ¡the ¡value, ¡but ¡ 0101 5 5 0110 6 6 not ¡it’s ¡bit ¡representa+on ¡ 0111 7 7 1000 8 -8 1001 9 -7 • No+ce, ¡the ¡difference ¡of ¡16 ¡i.e. ¡le[ ¡most ¡ 1010 10 -6 bit ¡ ¡ à ¡ 1011 11 -5 1100 12 -4 – Unsigned ¡= ¡2 3 ¡= ¡8 ¡ 1101 13 -3 – Signed ¡= ¡-­‑2 3 ¡ = ¡-­‑8 ¡ 1110 14 -2 1111 15 -1 6 ¡

  7. Signed ¡vs ¡Unsigned ¡(cont) ¡ • When ¡an ¡opera+on ¡is ¡performed ¡where ¡one ¡operand ¡is ¡ signed ¡and ¡the ¡other ¡is ¡unsigned, ¡ C ¡implicitly ¡casts ¡the ¡ signed ¡argument ¡to ¡unsigned ¡ and ¡performs ¡the ¡opera+ons ¡ assuming ¡the ¡numbers ¡are ¡nonnega+ve. ¡ – Since ¡bit ¡representa+on ¡does ¡not ¡change, ¡it ¡really ¡doesn’t ¡ maker ¡arithme+cally ¡ – However… ¡rela+onal ¡operators ¡have ¡issues ¡ ß ¡1 ¡= ¡TRUE ¡and ¡0 ¡= ¡FALSE ¡ EXPRESSION ¡ TYPE ¡ EVALUATION ¡ 0 ¡= ¡= ¡0u ¡ unsigned ¡ 1 ¡ -­‑1 ¡< ¡0 ¡ signed ¡ 1 ¡ -­‑1 ¡< ¡0u ¡ unsigned ¡ 0* ¡ ß #define ¡ ¡INT_MIN ¡ ¡ ¡(-­‑INT_MAX ¡– ¡1) ¡ 2147483647 ¡> ¡-­‑2147483647-­‑1 ¡ signed ¡ 1 ¡ ex. ¡w=4 ¡INT_MIN ¡= ¡-­‑8 ¡ ¡INT_MAX=7 ¡ 2147483647u ¡> ¡-­‑2147483647-­‑1 ¡ unsigned ¡ 0* ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡*** ¡how ¡is ¡-­‑8 ¡represented ¡in ¡T2U? ¡ ¡ 2147483647 ¡> ¡(int) ¡2147483647u ¡ signed ¡ 1* ¡ -­‑1 ¡> ¡-­‑2 ¡ signed ¡ 1 ¡ (unsigned) ¡-­‑1 ¡> ¡-­‑2 ¡ unsigned ¡ 1 ¡ 7 ¡

  8. Sign ¡Extend ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Trunca4on ¡ ¡ Drops ¡the ¡high ¡order ¡w-­‑k ¡ • Already ¡have ¡an ¡ bytes ¡when ¡trunca+ng ¡a ¡ intui+ve ¡sense ¡of ¡this ¡ w-­‑bit ¡number ¡to ¡a ¡k-­‑bit ¡ number ¡ ¡ w ¡= ¡8 ¡ 4-­‑bit ¡to ¡3-­‑bit ¡trunca+on ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡for ¡+27 ¡= ¡ ¡ ¡00011011 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡=> ¡invert ¡+ ¡1 ¡for ¡-­‑27 ¡= ¡ ¡ ¡11100101 ¡ ¡ ¡ UNSIGNED ¡– ¡ TWO'S ¡COMP ¡– ¡ w=16 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡00000000 ¡00011011 ¡ HEX ¡ B2U ¡ B2T ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡11111111 ¡11100101 ¡ orig ¡ trunc ¡ orig ¡ trunc ¡ orig ¡ trunc ¡ 0 ¡ 0 ¡ 0 ¡ 0 ¡ 0 ¡ 0 ¡ For ¡unsigned ¡ 2 ¡ 2 ¡ 2 ¡ 2 ¡ 2 ¡ 2 ¡ Fill ¡to ¡le[ ¡with ¡zero ¡ 9 ¡ 1 ¡ 9 ¡ 1 ¡ -­‑7 ¡ 1 ¡ For ¡signed ¡ B ¡ 3 ¡ 11 ¡ 3 ¡ -­‑5 ¡ 3 ¡ Repeat ¡sign ¡bit ¡ F ¡ 7 ¡ 15 ¡ 7 ¡ -­‑1 ¡ -­‑1 ¡ 8 ¡

  9. Integer ¡Addi4on ¡ ¡ • Signed ¡or ¡unsigned… ¡that ¡is ¡the ¡ques+on! ¡ Unsigned ¡4-­‑bit ¡BTU ¡ Signed ¡4-­‑bit ¡B2T ¡ 0 ¡to ¡16 ¡are ¡valid ¡values ¡ -­‑8 ¡to ¡7 ¡are ¡valid ¡values ¡ • • Only ¡checking ¡carry-­‑out ¡ Checking ¡carry-­‑in ¡AND ¡carry-­‑out ¡ • • x ¡ y ¡ x+y ¡ result ¡ x ¡ y ¡ x+y ¡ result ¡ -­‑8 ¡ -­‑5 ¡ -­‑13 ¡ 3 ¡ 8 ¡ 5 ¡ 13 ¡ 13 ¡ 1000 ¡ 1011 ¡ 10011 ¡ negOF ¡ 1000 ¡ 0101 ¡ 1101 ¡ ok ¡ -­‑8 ¡ -­‑8 ¡ -­‑16 ¡ 0 ¡ 8 ¡ 7 ¡ 15 ¡ 15 ¡ 1000 ¡ 1000 ¡ 10000 ¡ negOF ¡ 1000 ¡ 0111 ¡ 1111 ¡ ok ¡ -­‑8 ¡ 5 ¡ -­‑3 ¡ -­‑3 ¡ 12 ¡ 5 ¡ 17 ¡ 1 ¡ 1000 ¡ 0101 ¡ 01101 ¡ ok ¡ 1100 ¡ 0101 ¡ 10001 ¡ OF ¡ 2 ¡ 5 ¡ 7 ¡ 7 ¡ 0010 ¡ 0101 ¡ 00111 ¡ ok ¡ Nega+ve ¡overflow ¡when ¡x+y ¡< ¡-­‑2 w-­‑1 ¡ 5 ¡ 5 ¡ 10 ¡ -­‑6 ¡ Pos+ve ¡overflow ¡when ¡x+y ¡>= ¡2 w-­‑1 ¡ 0101 ¡ 0101 ¡ 01010 ¡ posOF ¡ 9 ¡

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend