class: big, middle # Engineering 1020: Introduction to Programming .title[ .lecture[Lecture \24\:] .title[Integer representation] ] .footer[[/lecture/24/](/lecture/24/)] --- # Storing values in memory ### Thus far: ??? In this course up to now, we have mostly talked about variables (including arrays) as opaque **boxes** that have been set aside to store information. We haven't gone into much detail about what's actually **stored** at those locations. Now, we'll start to talk more about how a computer's memory actually represents information. -- * values can be stored in _variables_ -- * variables are places in memory that store values -- * `int`, `bool`: integers -- * `float`: more detail next time, but using integers! -- * `str`: collections of characters (integer _ordinals_) --- # Storing integers <img src="../images/memory/variables.png" height="450" align="right"/> ### So how do we store integers? -- * variables have _names_ -- * variables have _addresses_ ??? Python provides a function called `id()` that can be used to uniquely identify a value at a moment in time. In most implementations of Python that you're likely to use, that `id()` function returns the _address_ of the value (though it's not required by the Python standard). -- * integers have _values_ -- ### But how does a computer represent the integer 42? --- # Computer memory <img src="../images/memory/dimms.png" height="325"/> ??? While these memory sticks may look familiar to anyone who's opened the case of a modern computer, it's not actually a very illuminating image. Inside each of that figure's black chips are billions of microscopic features; understanding them may seem just a bit overwhelming. -- <img src="../images/memory/8B-core-vs-8GB-sd.jpeg" height="325"/> .footnote[ Source: <a href="https://en.wikipedia.org/wiki/File:8_bytes_vs._8Gbytes.jpg"> Daniel Sancho via Wikipedia via Flikr</a> ] ??? The second image juxtaposes two memory technologies separated by about 50 years. On the top of the image we see a modern SD card, just like you might use in a phone or digital camera. This card is laid on top of a [_magnetic core memory_](https://en.wikipedia.org/wiki/Magnetic-core_memory), a kind of memory that computers used from the 1950s through the 1970s. In magnetic core memory, each of the little iron rings could be magnetized in one direction or the other, which was indicative of storing a `1` or a `0`. In the picture above, the SD card is laid on a square of 64 iron rings, which could store 64 bits. Eight bits forms a _byte_, so this pictures shows an 8 GB SD card (eight billion bytes) that's physically smaller than how computers used to store eight bytes! Today's memory works on different operating principles (using various [arrangements of microscopic _transistors_](https://en.wikipedia.org/wiki/Memory_cell_(computing)), but it still works by storing individual ones and zeros, which can be accessed as bytes (i.e., eight bits at a time). With these bits, we can represent all of the different types of values that we've been talking about in this course. We'll take a detailed look at how a computer represents integers, wave our hands in a not-super-specific way at how to represent floating-point numbers and then we'll talk about arrays. We will leave a discussion of how more complicated types like strings are represented for later course (e.g., ECE 3400: Foundations of Programming). --- # What's in memory? .floatright[ <img src="../images/memory/dimms.png" width="250"/> <img src="../images/memory/8B-core-vs-8GB-sd.jpeg" width="250"/> ] -- ### Each _bit_ (b) is on/off, true/false, 0/1 -- * portmanteau of _binary digit_ -- ### Eight bits is a _byte_ (B) -- or _octet_ (o) -- * My hard drive holds 1,000 GB -- * My network port can run at up to 1 Gb/s --- # Binary digit? -- ## What is the number 10? -- ### It depends! -- * base-10 (_decimal_) representation: $10\_{10}$ is ten -- * base-20 (_vigisimial_) representation: $10\_{20}$ = $20\_{10}$ -- * base-2 (_binary_) representation: $10\_2$ = $2\_{10}$ -- * base-16 (_hexadecimal_) representation: $10\_{16}$ = $16\_{10}$ --- # Numbers and place values ## Let's go back to school! -- ### Place value of numbers: -- * 1s place, 10s place, 100s place, 1000s place, ... -- * $b^0$ place, $b^1$ place, $b^2$ place, $b^3$ place, ... -- ### Alternative systems: $b=2$, $b=16$, $b=20$... --- # Decimal example ### What is 1234? -- * 1 $\times$ 1,000 -- $\left(10^3\right)$ -- plus * 2 $\times$ 100 -- $\left(10^2\right)$ -- plus * 3 $\times$ 10 -- $\left(10^1\right)$ -- plus * 4 $\times$ 1 -- $\left(10^0\right)$ --- # Binary example ### What is 1010? -- * 1 $\times 2^3$ -- (8) -- plus * 0 $\times 2^2$ -- (4) -- plus * 1 $\times 2^1$ -- (2) -- plus * 0 $\times 2^0$ -- (1) --- # Binary conversions in Python * binary literals: `0b10110111` -- * the `int` function has a `radix` parameter: ```python int(str, radix=10) ``` -- * the `bin` function converts a number to a binary string -- #### Exercise: Write a function to convert any integer to a list of 1s and 0s --- # Summary ### Computer memory stores _bits_ ### Can use bits for _binary_ integer representation ### Can use other representations (e.g., hexadecimal) too! ### Yet to come: * floating-point representation --- class: big, middle (here endeth the lesson)