CS107 Lecture 2 Bits and Bytes; Integer Representations
Total Page:16
File Type:pdf, Size:1020Kb
CS107 Lecture 2 Bits and Bytes; Integer Representations reading: Bryant & O’Hallaron, Ch. 2.2-2.3 This document is copyright (C) Stanford Computer Science and Nick Troccoli, licensed under Creative Commons Attribution 2.5 License. All rights reserved. Based on slides created by Marty Stepp, Cynthia Lee, Chris Gregg, Lisa Yan, Jerry Cain and others. 1 CS107 Topic 1: How can a computer represent integer numbers? 2 Demo: Unexpected Behavior cp -r /afs/ir/class/cs107/lecture-code/lect2 . 3 Lecture Plan • Bits and Bytes • Hexadecimal • Integer Representations • Unsigned Integers • Signed Integers • Overflow • Casting and Combining Types 4 Lecture Plan • Bits and Bytes • Hexadecimal • Integer Representations • Unsigned Integers • Signed Integers • Overflow • Casting and Combining Types 5 0 6 1 7 Bits • Computers are built around the idea of two states: “on” and “off”. Transistors represent this in hardware, and bits represent this in software! 8 One Bit At A Time • We can combine bits, like with base-10 numbers, to represent more data. 8 bits = 1 byte. • Computer memory is just a large array of bytes! It is byte-addressable; you can’t address (store location of) a bit; only a byte. • Computers still fundamentally operate on bits; we have just gotten more creative about how to represent different data as bits! • Images • Audio • Video • Text • And more… 9 Base 10 5 9 3 4 Digits 0-9 (0 to base-1) 10 Base 10 5 9 3 4 tens ones thousandshundreds = 5*1000 + 9*100 + 3*10 + 4*1 11 Base 10 5 9 3 4 103 102 101 100 12 Base 10 5 9 3 4 10X: 3 2 1 0 13 Base 2 1 0 1 1 2X: 3 2 1 0 Digits 0-1 (0 to base-1) 14 Base 2 1 0 1 1 23 22 21 20 15 Base 2 Most significant bit (MSB) Least significant bit (LSB) 1 0 1 1 eights fours twos ones = 1*8 + 0*4 + 1*2 + 1*1 = 1110 16 Base 10 to Base 2 Question: What is 6 in base 2? • Strategy: • What is the largest power of 2 ≤ 6? 17 Base 10 to Base 2 Question: What is 6 in base 2? • Strategy: • What is the largest power of 2 ≤ 6? 22=4 _0 1_ _ _ 23 22 21 20 18 Base 10 to Base 2 Question: What is 6 in base 2? • Strategy: • What is the largest power of 2 ≤ 6? 22=4 • Now, what is the largest power of 2 ≤ 6 – 22? _0 1_ _ _ 23 22 21 20 19 Base 10 to Base 2 Question: What is 6 in base 2? • Strategy: • What is the largest power of 2 ≤ 6? 22=4 • Now, what is the largest power of 2 ≤ 6 – 22? 21=2 _0 1_ 1_ _ 23 22 21 20 20 Base 10 to Base 2 Question: What is 6 in base 2? • Strategy: • What is the largest power of 2 ≤ 6? 22=4 • Now, what is the largest power of 2 ≤ 6 – 22? 21=2 • 6 – 22 – 21 = 0! _0 1_ 1_ _ 23 22 21 20 21 Base 10 to Base 2 Question: What is 6 in base 2? • Strategy: • What is the largest power of 2 ≤ 6? 22=4 • Now, what is the largest power of 2 ≤ 6 – 22? 21=2 • 6 – 22 – 21 = 0! _0 1_ 1_ 0_ 23 22 21 20 22 Base 10 to Base 2 Question: What is 6 in base 2? • Strategy: • What is the largest power of 2 ≤ 6? 22=4 • Now, what is the largest power of 2 ≤ 6 – 22? 21=2 • 6 – 22 – 21 = 0! _0 1_ 1_ 0_ 23 22 21 20 = 0*8 + 1*4 + 1*2 + 0*1 = 6 23 Practice: Base 2 to Base 10 What is the base-2 value 1010 in base-10? a) 20 b) 101 c) 10 d) 5 e) Other 24 Practice: Base 10 to Base 2 What is the base-10 value 14 in base 2? a) 1111 b) 1110 c) 1010 d) Other 25 Byte Values • What is the minimum and maximum base-10 value a single byte (8 bits) can store? 26 Byte Values • What is the minimum and maximum base-10 value a single byte (8 bits) can store? minimum = 0 maximum = ? 27 Byte Values • What is the minimum and maximum base-10 value a single byte (8 bits) can store? minimum = 0 maximum = ? 11111111 2x: 7 6 5 4 3 2 1 0 28 Byte Values • What is the minimum and maximum base-10 value a single byte (8 bits) can store? minimum = 0 maximum = ? 11111111 2x: 7 6 5 4 3 2 1 0 • Strategy 1: 1*27 + 1*26 + 1*25 + 1*24 + 1*23+ 1*22 + 1*21 + 1*20 = 255 29 Byte Values • What is the minimum and maximum base-10 value a single byte (8 bits) can store? minimum = 0 maximum = 255 11111111 2x: 7 6 5 4 3 2 1 0 • Strategy 1: 1*27 + 1*26 + 1*25 + 1*24 + 1*23+ 1*22 + 1*21 + 1*20 = 255 • Strategy 2: 28 – 1 = 255 30 Multiplying by Base 1450 x 10 = 14500 11002 x 2 = 11000 Key Idea: inserting 0 at the end multiplies by the base! 31 Dividing by Base 1450 / 10 = 145 11002 / 2 = 110 Key Idea: removing 0 at the end divides by the base! 32 Lecture Plan • Bits and Bytes • Hexadecimal • Integer Representations • Unsigned Integers • Signed Integers • Overflow • Casting and Combining Types 33 Hexadecimal • When working with bits, oftentimes we have large numbers with 32 or 64 bits. • Instead, we’ll represent bits in base-16 instead; this is called hexadecimal. 0110 1010 0011 0-15 0-15 0-15 34 Hexadecimal • When working with bits, oftentimes we have large numbers with 32 or 64 bits. • Instead, we’ll represent bits in base-16 instead; this is called hexadecimal. 0-15 0-15 0-15 Each is a base-16 digit! 35 Hexadecimal • Hexadecimal is base-16, so we need digits for 1-15. How do we do this? 0 1 2 3 4 5 6 7 8 9 a b c d e f 10 11 12 13 14 15 36 Hexadecimal Hex digit 0 1 2 3 4 5 6 7 Decimal value 0 1 2 3 4 5 6 7 Binary value 0000 0001 0010 0011 0100 0101 0110 0111 Hex digit 8 9 A B C D E F Decimal value 8 9 10 11 12 13 14 15 Binary value 1000 1001 1010 1011 1100 1101 1110 1111 37 Hexadecimal • We distinguish hexadecimal numbers by prefixing them with 0x, and binary numbers with 0b. • E.g. 0xf5 is 0b11110101 0x f 5 1111 0101 38 Practice: Hexadecimal to Binary What is 0x173A in binary? Hexadecimal 1 7 3 A Binary 0001 0111 0011 1010 39 Practice: Hexadecimal to Binary What is 0b1111001010 in hexadecimal? (Hint: start from the right) Binary 11 1100 1010 Hexadecimal 3 C A 40 Lecture Plan • Bits and Bytes • Hexadecimal • Integer Representations • Unsigned Integers • Signed Integers • Overflow • Casting and Combining Types 41 Number Representations • Unsigned Integers: positive and 0 integers. (e.g. 0, 1, 2, … 99999… • Signed Integers: negative, positive and 0 integers. (e.g. …-2, -1, 0, 1,… 9999…) • Floating Point Numbers: real numbers. (e,g. 0.1, -12.2, 1.5x1012) 42 Number Representations • Unsigned Integers: positive and 0 integers. (e.g. 0, 1, 2, … 99999… • Signed Integers: negative, positive and 0 integers. (e.g. …-2, -1, 0, 1,… 9999…) • Floating Point Numbers: real numbers. (e,g. 0.1, -12.2, 1.5x1012) Look up IEEE floating point if you’re interested! 43 Number Representations C Declaration Size (Bytes) int 4 double 8 float 4 char 1 char * 8 short 2 long 8 44 In The Days Of Yore… C Declaration Size (Bytes) int 4 double 8 float 4 char 1 char * 4 short 2 long 4 45 Transitioning To Larger Datatypes • Early 2000s: most computers were 32-bit. This means that pointers were 4 bytes (32 bits). • 32-bit pointers store a memory address from 0 to 232-1, equaling 232 bytes of addressable memory. This equals 4 Gigabytes, meaning that 32-bit computers could have at most 4GB of memory (RAM)! • Because of this, computers transitioned to 64-bit. This means that datatypes were enlarged; pointers in programs were now 64 bits. • 64-bit pointers store a memory address from 0 to 264-1, equaling 264 bytes of addressable memory. This equals 16 Exabytes, meaning that 64-bit computers could have at most 1024*1024*1024 GB of memory (RAM)! 46 Lecture Plan • Bits and Bytes • Hexadecimal • Integer Representations • Unsigned Integers • Signed Integers • Overflow • Casting and Combining Types 47 Unsigned Integers • An unsigned integer is 0 or a positive integer (no negatives). • We have already discussed converting between decimal and binary, which is a nice 1:1 relationship. Examples: 0b0001 = 1 0b0101 = 5 0b1011 = 11 0b1111 = 15 • The range of an unsigned number is 0 → 2w - 1, where w is the number of bits. E.g. a 32-bit integer can represent 0 to 232 – 1 (4,294,967,295). 48 Unsigned Integers 49 Let’s Take A Break To ponder during the break: A signed integer is a negative, 0, or positive integer. How can we represent both negative and positive numbers in binary? 50 Lecture Plan • Bits and Bytes • Hexadecimal • Integer Representations • Unsigned Integers • Signed Integers • Overflow • Casting and Combining Types 51 Signed Integers • A signed integer is a negative integer, 0, or a positive integer. • Problem: How can we represent negative and positive numbers in binary? 52 Signed Integers • A signed integer is a negative integer, 0, or a positive integer.