Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Idea 1: Bits Two states in a digital machine: 1.presence of.

Similar presentations


Presentation on theme: "Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Idea 1: Bits Two states in a digital machine: 1.presence of."— Presentation transcript:

1 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Idea 1: Bits Two states in a digital machine: 1.presence of a voltage – we’ll call this state “1” 2.absence of a voltage – we’ll call this state “0” A binary digit, or bit is the abstraction of a voltage reading Use multiple bits to represent information Common grouping of bits:  4 bits – a nybble  8 bits - a byte, the basic unit Word size – the number of bits typically handled as a unit in a machine 2-1

2 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 2-2 Idea 2: Represent Information in Bits A collection of n bits has 2 n possible states Interpret a collection of states as a binary number 2 2121 2020 Decimal # 0000 0011 0102 0113 1004 1015 1106 1117

3 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 2-3 Idea 3: Define Operations Algorithmically Base-2 addition – just like base-10! add from right to left, propagating carry 10010100101111 +1001 +1011+1 110111110110000 10111 +111 carry Except for OVERFLOW Subtraction, multiplication, division,… also like base-10

4 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 2-4 Idea 4: Represent Signed Numbers In a collection of n bits, divide the 2 n possible states between negative and positive numbers 2 2121 2020 Decimal # 0000 0011 0102 0113 100-4 101-3 110-2 111 Positive numbers Negative numbers

5 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 2-5 Two’s Complement Representation If number is positive or zero, normal binary representation, zero in MSB If number is negative, start with positive number flip every bit (i.e., take the one’s complement) then add one 00101 (5) 01001 (9) 11010 (1’s comp)(1’s comp) +1+1 11011 (-5)(-9)

6 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 2-6 Two’s Complement Shortcut To negate a two's complement number: copy bits from right to left until (and including) the first “1” flip remaining bits to the left 011010000 100101111 (1’s comp) +1100110000 (copy)(flip)

7 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 2-7 Operations on Two’s Complement As we’ve discussed, 2C encoded addition uses the same procedure as binary addition assume all integers have the same number of bits ignore carry out 01101000 (104) 11110110 (-10) +11110000 (-16) + (-9) 01011000 (98)(-19) Assuming 8-bit 2C numbers.

8 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 2-8 Subtraction in 2C Negate subtrahend (2nd no.) and add. assume all integers have the same number of bits ignore carry out 01101000 (104) 11110110 (-10) -00010000 (16) - (-9) 01101000 (104) 11110110 (-10) +11110000 (-16) + (9) 01011000 (88)(-1) But what about OVERFLOW

9 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 2-9 Sign Extension To add two 2C encoded numbers, we must represent them with the same number of bits. But what if one is 4-bit word length and the other is 8-bits? If we just pad with zeroes on the left, it doesn’t work!: Instead, replicate the MS bit -- the sign bit: Called “Sign extension” 4-bit8-bit 0100 (4) 00000100 (still 4) 1100 (-4) 00001100 ( 12, not -4) 4-bit8-bit 0100 (4) 00000100 (still 4) 1100 (-4) 11111100 (still -4)

10 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 2-10 Overflow If operands are too big, then sum cannot be represented as an n-bit 2C number. We have overflow if: signs of both operands are the same, and sign of sum is different. In other words, you can’t add two positive numbers and get a negative answer, or two negative numbers and get a positive answer! Another test -- easy for hardware: carry into MS bit does not equal carry out 01000 (8) 11000 (-8) +01001 (9) +10111 (-9) 10001 (-15) 01111 (+15)

11 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 2-11 Hexadecimal Notation fewer digits -- four bits per hex digit less error prone -- easy to corrupt long string of 1’s and 0’s Trivia: ‘hexadecimal’ is a mixture of Greek (hex) and Latin (decimal) because the full-Latin word for 16 makes people blush (‘sexadecimal’) BinaryHexDecimal 000000 000111 001022 001133 010044 010155 011066 011177 BinaryHexDecimal 100088 100199 1010A10 1011B11 1100C12 1101D13 1110E14 1111F15

12 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 2-12 Converting from Binary to Hexadecimal Every four bits is a hex digit. start grouping from right-hand side! 011101010001111010011010111 7D4F8A3 NOTE! This is not a new machine code, just a convenient way for humans to write a long binary number!!

13 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Converting From Base 10 to Base 2 Recall the previous approach of identifying the largest powers of 2 that contribute to the base number: 2-13 A better algorithm

14 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 2-14 Fractions: Fixed-Point Code How can we represent fractions? Use a “binary point” to separate positive from negative powers of two -- just like “decimal point.” 2’s comp addition and subtraction still work.  if binary points are aligned 00101000.101 (40.625) +11111110.110 (-1.25) 00100111.011 (39.375) 2 -1 = 0.5 2 -2 = 0.25 2 -3 = 0.125 No new operations needed -- same procedure as integer arithmetic.

15 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 2-15 Very Large and Very Small: Floating-Point Code Large values: 6.023 x 10 23 -- requires 79 bits Small values: 6.626 x 10 -34 -- requires >110 bits Use equivalent of “scientific notation”: F x 2 E Need to represent F (fraction), E (exponent), and sign. IEEE 754 Floating-Point Standard (32-bits): S Encoded Exponent Fraction 1b8b23b

16 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 2-16 Floating Point Example Single-precision IEEE floating point number: 10111111010000000000000000000000 Sign is 1 – number is negative. Encoded exponent field is 01111110 = 126 (decimal). Fraction is 0.100000000000… = 0.5 (decimal). Value = -1.5 x 2 (126-127) = -1.5 x 2 -1 = -0.75. sign encoded exponent fraction

17 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Another Floating Point Example 2-17 http://en.wikipedia.org/w/index.php?title=IEEE_754-1985 Sign bit is 1, because number is negative. 118.625 (decimal) is 1110110.101 (binary). This is 1.110110101×2 6. Exponent is 6: encoded as 6+127 = 133 = 10000101 (binary). Fraction is 11011010100000000000000: first 1 is hidden, padded with zeroes on the right

18 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 2-18 Text: ASCII Characters ASCII: Maps 128 characters to 7-bit code. both printable and non-printable (ESC, DEL, …) characters 00nul10dle20sp30040@50P60`70p 01soh11dc121!31141A51Q61a71q 02stx12dc222"32242B52R62b72r 03etx13dc323#33343C53S63c73s 04eot14dc424$34444D54T64d74t 05enq15nak25%35545E55U65e75u 06ack16syn26&36646F56V66f76v 07bel17etb27'37747G57W67g77w 08bs18can28(38848H58X68h78x 09ht19em29)39949I59Y69i79y 0anl1asub2a*3a:4aJ5aZ6aj7az 0bvt1besc2b+3b;4bK5b[6bk7b{ 0cnp1cfs2c,3c<4cL5c\6cl7c| 0dcr1dgs2d-3d=4dM5d]6dm7d} 0eso1ers2e.3e>4eN5e^6en7e~ 0fsi1fus2f/3f?4fO5f_6fo7fdel

19 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 2-19 Fun tricks with ASCII Code What is relationship between a decimal digit ('0', '1', …) and its ASCII code What is the difference between an upper-case letter ('A', 'B', …) and its lower-case equivalent ('a', 'b', …)? Given two ASCII characters, how do we tell which comes first in alphabetical order? Are 128 characters enough? (http://www.unicode.org/) No new operations -- integer arithmetic and logic. Copyright © Thomas M Conte

20 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 2-20 Other Data Types (codes)… millions of them! Text strings sequence of characters, terminated with NULL (0) typically, no hardware support Image array of pixels  monochrome: one bit (1/0 = black/white)  color: red, green, blue (RGB) components (e.g., 8 bits each)  other properties: transparency hardware support:  typically none, in general-purpose processors  MMX -- multiple 8-bit operations on 32-bit word Sound sequence of fixed-point numbers


Download ppt "Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Idea 1: Bits Two states in a digital machine: 1.presence of."

Similar presentations


Ads by Google