QTM 350 - Data Science Computing

Lecture 02 - Computational Literacy

Danilo Freire

Department of Quantitative Theory and Methods
Emory University

Learning objectives

By the end of this lecture, you will be able to:

  1. Understand the historical evolution of computers, from manual calculation to modern electronics
  2. Grasp the fundamental architecture of computers (Von Neumann)
  3. Explain how computers represent various forms of data (numbers, images, colours) using binary and hexadecimal systems
  4. Perform basic conversions between decimal, binary, and hexadecimal numbers
  5. Appreciate the role of abstraction in computing

Let’s get started! 🚀 💻

Brief history of computing

The first computers

  • Historically, a computer was a person who makes calculations, especially with a calculating machine
  • To do calculations we use numbers. How to represent them?

Four-species mechanical calculators

Silicon-based computers

The 1970s marked the transition from mechanical to electronic:

Moore’s law: exponential growth

  • Coined by Gordon Moore (co-founder of Intel) in 1965.
  • Observation: The number of transistors on a microchip doubles approximately every two years, though the pace has slowed a bit.
  • This has driven decades of increasing computing power and decreasing costs.
  • The law is not a physical law but an observation and prediction
  • It has significant implications for the future of technology and computing!
  • Quantum computing may change this trajectory and increase the pace of growth.
  • In the future, maybe only energy and time will limit computing power?
  • Read more about Moore’s Law

Von Neumann Architecture

Von Neumann Architecture

  • Advantages:
    • Efficient memory use, with less need for separate areas
    • Flexibility in data storage and manipulation
    • Simplicity in design and operation
  • Disadvantages:
    • Von Neumann bottleneck: Limits computing performance due to sequential processing of instructions and data through a single bus
    • The CPU often waits for data due to its faster processing speed compared to memory transfer rates
    • Harvard architecture is an alternative that separates data and instruction memory

Harvard Architecture

  • As a contrast to Von Neumann, the Harvard Architecture uses physically separate storage and signal pathways for instructions and data.
  • Key Idea: Can fetch the next instruction while the current instruction is operating on data.
  • This allows for simultaneous access to both instructions and data, improving performance.
  • Helps alleviate the Von Neumann bottleneck in certain contexts.
  • Never achieved the same level of popularity as Von Neumann architecture for general-purpose computing.
  • Read more about Harvard Architecture

Data representation

Computers run on 0s and 1s

  • Computers represent everything by using 0s and 1s
  • Transistors act as switches, with 1 for high voltage level and 0 for low voltage level
  • Computers use binary because transistors are easy to fabricate in silicon and can be densely packed on a chip
  • But how does this work?
  • How can we represent text, images, and videos using only 0s and 1s?
  • This leads us to abstraction: representing ideas at different levels of detail by identifying what is essential
  • We will use abstraction to translate anything into 0s and 1s, and also translate those numbers to other types

Number Systems Introduction

  • We are familiar with the decimal system (base 10), using digits 0-9.
  • Computers use different number systems for internal representation.
  • To understand how computers “think” in 0s and 1s, we’ll explore:
    • An analogy using coins.
    • The binary system (base 2).
    • The hexadecimal system (base 16).

Converting coins to dollars

  • We can convert between number systems by translating a value from one system to the other
  • For example, the coins on the left represent the same value as $0.87
  • Using pictures is clunky. Let’s make a new representation system for coins

Converting coins to dollars

  • To represent coins, we will make a number with four digits
  • The first represents quarters, the second dimes, the third nickels, and the fourth pennies
    • c3102 =
    • 3 x $0.25 + 1 x $0.10 + 0 x $0.05 + 2 x $0.01 =
    • $0.87

Converting dollars to coins

  • How do we convert money from dollars to coins? Assume we want to minimise the number of coins used

  • For example, what is $0.59 in coin representation? Use the same four-digit system: quarters, dimes, nickels, and pennies

  • $0.59 = 2 x $0.25 + 0 x $0.10 + 1 x $0.05 + 4 x $0.01 = c2014

Quick questions!

  • Think-Pair-Share: do the following conversions

  • What is c1112 in dollars?

  • What is $0.61 in coin representation?

Solutions:

  • c1112 = $0.42 = 1 x $0.25 + 1 x $0.10 + 1 x $0.05 + 2 x $0.01

  • $0.61 = c2101 = 2 x $0.25 + 1 x $0.10 + 0 x $0.05 + 1 x $0.01

Number systems – binary

  • Now let us go back to computers! 💻

  • We can represent numbers using only 0s and 1s with the binary number system

  • Instead of counting the number of 1s, 5s, 10s, and 25s coins you need, count the number of 1s, 2s, 4s, 8s, etc

  • Why these numbers? They are powers of 2. This is a number in base 2

  • A single binary digit is a bit, e.g., 101 has three bits

  • An 8-bit group is called a byte, e.g., 10101010

  • Binary numbers grow as follows:

    • 0 represents zero
    • 1 represents one
    • 10 represents two
    • 100 represents four
    • 1000 represents eight, and so on…

Quick question!

  • Think-Pair-Share: what is the binary representation of the decimal number 3?
  1. 101
  1. 11
  1. 111
  1. 010

Your turn!

Practice Exercise 01:

  1. What binary number represents 5?

  2. What binary number represents 7?

Solutions:

Convert binary to decimal

To convert a binary number to decimal, just add each power of 2 that is represented by a 1.

  • For example, 00011000 = \(2^4 + 2^3\) = 16 + 8 = 24
\(2^7\) (128) \(2^6\) (64) \(2^5\) (32) \(2^4\) (16) \(2^3\) (8) \(2^2\) (4) \(2^1\) (2) \(2^0\) (1)
0 0 0 1 1 0 0 0


  • Another example: 10010001 = \(2^7 + 2^4 + 2^0\) = 128 + 16 + 1 = 145
128 64 32 16 8 4 2 1
1 0 0 1 0 0 0 1

So far, so good? 😃

Binary and abstraction

Binary and abstraction

(Representing Images)


  • Now that we can represent numbers using binary, we can represent everything computers store using binary
  • We just need to use abstraction to interpret bits or numbers in particular ways
  • Let us consider colours, images, and text

Images as collections of colours

  • What if we want to represent an image? How can we convert that to numbers?
  • First, break the image down into a grid of colours, where each dot of color has a distinct hue
  • A dot of color in this context is called a pixel
  • Now we just need to represent a single color (a pixel) as a number!

Images as collections of colours

RGB colour model

  • The RGB colour model is widely used in digital displays
  • Each pixel is represented by three numbers, each ranging from 0 to 255 (for 8-bit colour)
  • The first number represents the amount of red, the second the amount of green, and the third the amount of blue
  • (0,0,0) is black (no r/g/b) and (255,255,255) is white (full r/g/b)!
  • You can try different colours here

Interactive RGB Exploration

Let’s explore the RGB model interactively!

  • Visit an online RGB Colour Picker: W3Schools RGB Calculator
  • Try inputting different values (0-255) for Red, Green, and Blue.
  • Observe how the colour changes.
    • What happens if Red is 255 and Green/Blue are 0?
    • What if all three are equal (e.g., 128, 128, 128)?
    • Can you create your favourite colour? What are its RGB values?

Number systems – Hexadecimal

What is hexadecimal?

  • When we represent values with multiple bytes, it can be hard to distinguish where numbers begin and end
  • Hexadecimal is a number system with 16 digits: 0123456789ABCDEF (Base 16)
  • It is used to represent binary numbers in a more compact way
  • Each hex digit corresponds to 4 binary bits, making it a shorthand for binary:
    • 0000\(_2\) = 0\(_{16}\)
    • 0001\(_2\) = 1\(_{16}\)
    • 0010\(_2\) = 2\(_{16}\)
    • 1010\(_2\) = A\(_{16}\) (10 in decimal)
    • 1110\(_2\) = E\(_{16}\) (14 in decimal)
    • 1111\(_2\) = F\(_{16}\) (15 in decimal)

Binary to hex conversion

  • Convert binary to hex by grouping into blocks of four bits (starting from the right).
  • Example: Binary 1001 1110 0000 1010 converts to Hex 9E0A.
    • 1001\(_2\) = 9\(_{16}\)
    • 1110\(_2\) = E\(_{16}\)
    • 0000\(_2\) = 0\(_{16}\)
    • 1010\(_2\) = A\(_{16}\)


Practice Exercise 02:

  1. Convert the decimal number 13 to binary.

  2. Convert that binary number to hexadecimal.

  • Solutions:
    • Decimal 13 to binary: 1101\(_2\)
    • Binary 1101\(_2\) to hexadecimal: D\(_{16}\)

Hexadecimal and HTML

Hex and RGB

  • HTML and CSS use hexadecimal to represent colours.

  • Six-digit hex numbers specify colours: #RRGGBB

    • #FFFFFF = White (Red=FF, Green=FF, Blue=FF)
    • #000000 = Black (Red=00, Green=00, Blue=00)
    • #FF0000 = Red (Red=FF, Green=00, Blue=00)
  • Each pair of hex digits represents a colour component (00 to FF, which is 0 to 255 in decimal).

  • Each color channel (8-bit) has 256 intensity levels.

  • Total possible colours: \(256 \times 256 \times 256 = 256^3 \approx 16.7\) million colours.

Hexadecimal in HTML/CSS Colours

Examples and Interpretation

  • Pure Colours:
    • #FF0000: Full Red (Red=FF, Green=00, Blue=00)
    • #00FF00: Full Green (Red=00, Green=FF, Blue=00)
    • #0000FF: Full Blue (Red=00, Green=00, Blue=00)
  • Black and White:
    • #000000: Black (Red=00, Green=00, Blue=00 – no light)
    • #FFFFFF: White (Red=FF, Green=FF, Blue=FF – all light at full intensity)
  • Mixing Colours (Secondary Colours):
    • #FFFF00: Yellow (Red=FF, Green=FF, Blue=00)
    • #00FFFF: Cyan (Red=00, Green=FF, Blue=FF)
    • #FF00FF: Magenta (Red=FF, Green=00, Blue=FF)

Hexadecimal in HTML/CSS Colours

Shorthand and Transparency (Alpha)

  • 3-Digit Shorthand (#RGB):
    • If each pair of hex digits in a 6-digit code is the same, you can use a 3-digit shorthand
    • The browser expands it by duplicating each digit!
    • Examples:
      • #F00 is equivalent to #FF0000 (Red)
      • #0A3 is equivalent to #00AA33 (a dark green)
      • #FFF is equivalent to #FFFFFF (White)
  • Opacity is represented by an additional two hex digits (8-digit hex)
    • 00 is fully transparent (invisible)
    • FF is fully opaque (solid)
    • Values in between give partial transparency (e.g., 80 is 50% opaque)
    • 4-Digit Shorthand (#RGBA): Similar to the 3-digit shorthand, but for colours with an alpha channel.
    • Example: #F008 would be equivalent to #FF000088 (semi-transparent red, with alpha 88 hex).

Conclusion

Brief Recap

In this lecture, we covered:

  • Evolution of Computing: From human calculators and mechanical devices to silicon-based computers and the Von Neumann architecture.
  • Data Representation Fundamentals: How computers use binary (0s and 1s) as the basic language.
  • Abstraction: The key concept of representing complex information (like numbers and images) at different levels.
  • Number Systems:
    • Binary (Base 2): Bits, bytes, and converting between binary and decimal.
    • Hexadecimal (Base 16): A compact way to represent binary, its conversion, and its use in RGB colours for HTML/CSS.
  • Representing Images: Pixels and the RGB colour model.

Questions? 🤔

Thank you very much and see you next class! 😊 🙏

Solutions to Practice Exercises

Solution - Practice Exercise 01

  1. What binary number represents 5?
  • In binary, the number 5 is represented as 101\(_2\).
  • $ (1 ^2) + (0 ^1) + (1 ^0) = 4 + 0 + 1 = 5 $.
  1. What binary number represents 7?
  • In binary, the number 7 is represented as 111\(_2\).
  • $ (1 ^2) + (1 ^1) + (1 ^0) = 4 + 2 + 1 = 7 $.

Solution - Practice Exercise 02

  • Decimal 13 to binary:

    • \(13 = 8 + 4 + 0 + 1\)
    • \(13 = (1 \times 2^3) + (1 \times 2^2) + (0 \times 2^1) + (1 \times 2^0)\)
    • So, decimal 13 is 1101 in binary (1101\(_2\)).
  • Binary 1101 to hexadecimal:

    • Group the binary into blocks of four (from the right): 1101.
    • Convert the block 1101 to its hexadecimal equivalent:
      • 1101\(_2\) = \((1 \times 8) + (1 \times 4) + (0 \times 2) + (1 \times 1) = 8 + 4 + 0 + 1 = 13\) in decimal.
      • Decimal 13 is D in hexadecimal.
    • So, binary 1101 is D in hexadecimal (D\(_{16}\)).