data representation bits, bytes, binary, hexadecimal

Download Data Representation Bits, Bytes, Binary, Hexadecimal

If you can't read please download the document

Upload: miranda-norris

Post on 13-Dec-2015

226 views

Category:

Documents


5 download

TRANSCRIPT

  • Slide 1

Data Representation Bits, Bytes, Binary, Hexadecimal Slide 2 Computer Science Joke There are 10 types of people in the world: Those who understand binary And those who dont. Get it? If not, you will by the end of todays lecture.. 1 Slide 3 Outline Bits and Bytes Binary Hexadecimal 2 Slide 4 Bits and Bytes Recall input / output of Logic Gates 0 => FALSE 1 => TRUE 3 A C B ABC = A & B 000 010 100 111 Slide 5 Bits and Bytes In reality, 0 and 1 represent voltage states (e.g., LOW / HIGH) These voltage states give the electrical circuit life 4 A C B ABC = A & B 000 010 100 111 Slide 6 Bits and Bytes Think of each input / output as a light switch Can be either on (TRUE, 1) or off (FALSE, 0) 5 Slide 7 Bits and Bytes Each light switch represents a bit bit stands for Binary digIT A bit can be either 0 or 1 A bit is the most basic element in computing Basis for logic gate circuits (logic gate circuits => more complex circuits) 6 Slide 8 Bits and Bytes When you put 8 bits together, you get a byte A byte is used to encode integers from 0 to 255 Or integers from -128 to 127 (more on this later) 7 Slide 9 Bits and Bytes In computers, a byte is the smallest addressable memory unit Memory units are defined in terms of bytes (not bits) E.g., Kilobytes (KB), Megabytes (MB), Gigabytes (GB), etc. Kilobyte: 1,000 bytes (thousand) Megabyte: 1,000,000 bytes (million) Gigabyte: 1,000,000,000 bytes (billion) 8 Slide 10 Bits and Bytes How many bits in 10 GB of storage? 9 Slide 11 Bits and Bytes How many bits in 10 GB of storage? 80,000,000,000 bits 10 Slide 12 Outline Bits and Bytes Binary Hexadecimal 11 Slide 13 Binary Numbers A computers machine language only uses ONES and ZEROS How does a computer represent a number using only bits? 12 Slide 14 Binary Numbers A byte can represent any number between 0 and 255 A byte is a sequence of eight bits, b 7 through b 0 13 b 7 b 6 b 5 b 4 b 3 b 2 b 1 b 0 Slide 15 Binary Numbers 14 b7b6b5b4b3b2b1b0b7b6b5b4b3b2b1b0 b 7 (2 7 ) b 6 (2 6 ) b 5 (2 5 ) b 4 (2 4 ) b 3 (2 3 ) b 2 (2 2 ) b 1 (2 1 ) b 0 (2 0 ) NUMBER Slide 16 Binary Numbers 15 b7b6b5b4b3b2b1b0b7b6b5b4b3b2b1b0 b 7 (2 7 ) b 6 (2 6 ) b 5 (2 5 ) b 4 (2 4 ) b 3 (2 3 ) b 2 (2 2 ) b 1 (2 1 ) b 0 (2 0 ) NUMBER b 7 through b 0 can only be 0 or 1 Slide 17 Binary Numbers 16 b7b6b5b4b3b2b1b0b7b6b5b4b3b2b1b0 b 7 (2 7 ) b 6 (2 6 ) b 5 (2 5 ) b 4 (2 4 ) b 3 (2 3 ) b 2 (2 2 ) b 1 (2 1 ) b 0 (2 0 ) NUMBER Note how b 7 gets multiplied by 2 7 Slide 18 Binary Numbers 17 b7b6b5b4b3b2b1b0b7b6b5b4b3b2b1b0 b 7 (2 7 ) b 6 (2 6 ) b 5 (2 5 ) b 4 (2 4 ) b 3 (2 3 ) b 2 (2 2 ) b 1 (2 1 ) b 0 (2 0 ) NUMBER b 6 gets multiplied by 2 6 Slide 19 Binary Numbers 18 b7b6b5b4b3b2b1b0b7b6b5b4b3b2b1b0 b 7 (2 7 ) b 6 (2 6 ) b 5 (2 5 ) b 4 (2 4 ) b 3 (2 3 ) b 2 (2 2 ) b 1 (2 1 ) b 0 (2 0 ) NUMBER b 5 gets multiplied by 2 5 Slide 20 Binary Numbers 19 b7b6b5b4b3b2b1b0b7b6b5b4b3b2b1b0 b 7 (2 7 ) b 6 (2 6 ) b 5 (2 5 ) b 4 (2 4 ) b 3 (2 3 ) b 2 (2 2 ) b 1 (2 1 ) b 0 (2 0 ) NUMBER Et. cetera Slide 21 Binary Numbers 20 b7b6b5b4b3b2b1b0b7b6b5b4b3b2b1b0 b 7 (2 7 ) b 6 (2 6 ) b 5 (2 5 ) b 4 (2 4 ) b 3 (2 3 ) b 2 (2 2 ) b 1 (2 1 ) b 0 (2 0 ) NUMBER All the multiplications get added together into the integer number Slide 22 Binary Numbers 21 b7b6b5b4b3b2b1b0b7b6b5b4b3b2b1b0 b 7 (128) b 6 (64) b 5 (32) b 4 (16) b 3 (8) b 2 (4) b 1 (2) b 0 (1) NUMBER Slide 23 Binary Numbers: Example 22 0010 1010 Represents what number? Slide 24 Binary Numbers: Example 23 0010 1010 0 (128) 0 (64) 1 (32) 0 (16) 1 (8) 0 (4) 1 (2) 0 (1) Slide 25 Binary Numbers: Example 24 0010 1010 0 (128) 0 (64) 1 (32) 0 (16) 1 (8) 0 (4) 1 (2) 0 (1) 32 + 8 + 2 = 42 Slide 26 Binary Numbers: Example 25 0010 1010 Represents what number? 42 Slide 27 Binary Numbers: Example 26 1001 0111 Represents what number? Please find a partner to work with Check each others work Slide 28 Binary Numbers: Example 27 1001 0111 Represents what number? 151 Slide 29 Binary Numbers Same technique applies to represent larger numbers 2 bytes (16 bits) 4 bytes (32 bits) 8 bytes (64 bits) Whats the largest unsigned (positive only) number that can be represented using 16 bits? Hint: the largest number for 8-bits is 255 Well cover signed (positive and negative) numbers next time 28 Slide 30 Binary Numbers Example of 16-bit number: 29 1000 0000 0000 0001 Slide 31 Binary Numbers Example of 16-bit number: 30 1000 0000 0000 0001 = 2 15 + 2 0 = 32,768 + 1 = 32,769 Slide 32 Binary Numbers To convert decimal to binary Reverse the process 1.Find largest power of 2 that fits within the decimal value 2.Record 1 in the associated log 2 placeholder (in binary sequence) 3.Subtract power of 2 value from original decimal number 4.Repeat until nothing remains 31 Slide 33 Binary Numbers Example: Decimal to Binary What is the binary sequence for decimal number 250? 1.Find largest power of two that fits in 250 128 2.Record 1 in associated placeholder (128 = 2 7, 7 th position) => 1000 0000 3.Subtract power of two value from original decimal (250 128 = 122) 4.Repeat until theres nothing left. 32 Slide 34 Binary Numbers Example: Decimal to Binary 1.Find largest power of two that fits in 122 64 2.Record 1 in associated placeholder (64 = 2 6, 6 th position) => 1100 0000 3.Subtract power of two value from original decimal ( 122 64 = 58 ) 4.Repeat until theres nothing left. 33 Slide 35 Binary Numbers Example: Decimal to Binary 1.Find largest power of two that fits in 58 32 2.Record 1 in associated placeholder (32 = 2 5, 5 th position) => 1110 0000 3.Subtract power of two value from original decimal ( 58 - 32 = 26) 4.Repeat until theres nothing left. 34 Slide 36 Binary Numbers Example: Decimal to Binary 1.Find largest power of two that fits in 26 16 2.Record 1 in associated placeholder (16 = 2 4, 4 th position) => 1111 0000 3.Subtract power of two value from original decimal ( 26 16 = 10) 4.Repeat until theres nothing left. 35 Slide 37 Binary Numbers Example: Decimal to Binary 1.Find largest power of two that fits in 10 8 2.Record 1 in associated placeholder (8 = 2 3, 3 th position) => 1111 1000 3.Subtract power of two value from original decimal ( 10 8 = 2 ) 4.Repeat until theres nothing left. 36 Slide 38 Binary Numbers Example: Decimal to Binary 1.Find largest power of two that fits in 2 2 2.Record 1 in associated placeholder (2 = 2 1, 1 st position) => 1111 1010 3.Subtract power of two value from original decimal ( 2 2 = 0 ) 4.Repeat until theres nothing left. DONE!! 37 Slide 39 Binary Numbers What is the binary sequence for decimal number 250? 38 250 = 128 + 64 + 32 + 16 + 8 + 2 = 2 7 + 2 6 + 2 5 + 2 4 + 2 3 + 2 1 = 1111 1010 Slide 40 Break Time 39 Slide 41 Outline Bits and Bytes Binary Hexadecimal 40 Slide 42 Hexadecimal Writing and representing binary numbers can be cumbersome Hard to read / write large sequences of 0s and 1s Hexadecimal offers succinct way to represent binary numbers E.g., 41 1011 0101 = 0xB5 Slide 43 Hexadecimal Decimal numbers are base 10 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 Hexadecimal numbers are base 16 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F Note: we prefix hexadecimal numbers with 0x Why use hex? Easier to represent binary numbers 42 Slide 44 Hexadecimal 4-bit numbers go from 0 to 15 0000 => 0 1111 => 15 Hexadecimal is base 16 Can easily represent numbers between 0 and 15 0 => 0x0 15 => 0xF Thus, we can represent 0000 as 0x0 1111 as 0xF 43 Slide 45 Hexadecimal This can be abstracted to 8-bit numbers One hex number represents the first 4-bits One hex number represents the second 4-bits 44 1011 0101 1011 = 11 = 0xB 0xB5 0101 = 5 = 0x5 Slide 46 Hexadecimal This works with 16, 32, and 64-bit numbers One hex number per 4-bits 45 1111 0110 1100 0001 0x6 0xF6C1 0xC0xF0x1 Slide 47 Hexadecimal 46 1111 0110 1100 0001 (binary) = 0xF6C1 (hex) = 63,169 (decimal) Slide 48 Hexadecimal (practice) Find a partner to work with Convert decimal number 77 A) to binary B) to hexadecimal 47 Slide 49 Hexadecimal (practice) Find a partner to work with Convert decimal number 77 A) to binary 01001101 B) to hexadecimal 0x4D 48 Slide 50 Hexadecimal (practice) Convert binary number 1110 0111 A) to decimal B) to hexadecimal 49 Slide 51 Hexadecimal (practice) Convert binary number 1110 0111 A) to decimal 231 B) to hexadecimal 0xE7 50 Slide 52 Hexadecimal (practice) Convert hexadecimal number 0xDF A) to binary B) to decimal 51 Slide 53 Hexadecimal (practice) Convert hexadecimal number 0xDF A) to binary 11011111 B) to decimal 223 52 Slide 54 Next steps Next lecture: Binary Arithmetic (addition / subtraction) 2s complement (signed numbers) Simple logic gate circuits to do addition 53