Skip to content

Latest commit

 

History

History
230 lines (172 loc) · 10.5 KB

File metadata and controls

230 lines (172 loc) · 10.5 KB

Class 1: Number Bases

Topics

Minute-by-Minute

Elapsed Time Activity
0:00 0:05 Factoid of the day
0:05 0:15 Learning Outcomes/Course Overview
0:20 0:10 Flippy Do Worksheet
0:30 0:25 TT: Base Ten vs Base Two
0:55 0:15 Integers and Bases Worksheet
1:10 0:25 TT: Base 16
1:35 0:15 Integers and Bases Worksheet Pt 2
1:50 0:05 Negative Numbers
1:55 0:05 Wrapup
TOTAL 2:00

Factoid of the day (5 min)

Learning Outcomes/Course Overview (15 min)

Learning Outcomes

By the end of this lesson, students should be able to...

  1. Explain the difference between decimal, binary, and hexadecimal based numbers
  2. Practice converting between decimal, binary, and hexadecimal
  3. Differentiate between signed and unsigned numbers in binary

Course Overview

  • Provide a high level outline of what the course is going to be covering of the next 7 weeks
  • Emphasize how the concepts used here can be used in a technical interviews
  • Plaegerism is not tolerated for any assignment in this class. AT ALL.

Flippy Do Worksheet (10 min)

Each person should fill out the Flippy Do worksheet, following the guide on how to fill it out

TT: Base Ten vs Base Two (25 min)

  • Reminder that slides can be found in course repo

Base Ten

  • Review of what Base 10 (decimal) is, and how we use it.
  • Everyone uses base 10 today
  • There are ten digits.
  • The size of the base are reflective of the amount of numbers in that base (0-9)
  • Go over concept of "carrying" or "rolling over" (i.e. 9 rolls over to 10)

Base Two

  • How computers represent information
  • Two digits: 0 and 1
  • 1 rolls over to 10, 11 rolls over to 100
  • 10 != ten, 10 == two or "one-zero"
  • Like how we go from 99 to 100 in base ten, we go from 11 to 100
  • Ask students to say how numbers one through ten would be represented in base two

Counting in Binary

  • All even numbers end in 0
  • All odd numbers end in 1
  • That extra 1 in the final digit represents exactly that: the number one!
Whiteboard Demo

Go over example of powers of ten in base ten:

  • i.e. 4236 is 4*1000+2*100+3*10+6*1

Now relate to how in base two, we have the same concept, but with powers of two

  • i.e. 1100 is twelve: 1*8+1*4+0*2+0*1

Both base ten and two always start with base^0, which is why they're always equal to 1.

  • 10^0 and 2^0 both equal 1
Binary to Decimal
  • To convert, start from one end of the number and go through it, progressivly adding powers of two depending on if the bit is 0 or 1
  • Brief history of language: digits --> binary digits --> bidgits --> bigits --> bits

Integers and Bases Worksheet (15 min)

Individual Work (10 min)

Each person should fill out the first two sections of integers and bases worksheet

  • Note that students should have 8 bits in your answer
  • Use subscripts to notate what base a number is in
  • Common practice to group binary numbers into 4 bits. Similar to how we use commas in base ten
  • Hold on sign mangnitude section for now
  • Visit Base Convert to check your answers

Peer Review (5 min)

  • Compare/contrast answers with a neighbor, and how you each arrived to your solutions
  • Discuss conversion strategies, and try to find a neighbor that used a different one than you did

TT: Base 16 (25 min)

  • Hexidecimal - base-16, more simply known as "hex"
  • Common to represent a number as hex, even though computers use binary, because:
    • There's a simple conversion betweeb base-2 and base-16
    • They're easier to write out
  • 16 digits: 0-9, A-F
    • 9 rolls over to A, F rolls over to 10
    • 99 rolles over to 9A, FF rolls over to 100
  • A == 10, B == 11 ... F == 15
  • very important for frontend engineers!
    • Used for colors
    • Used to represent symbols
  • Note max value is always one less than the base (i.e. 15 for base-16)

Base 64 interlude

Give brief history of how base 64 is used for the web, commonly used to pass information through URLs. We won't be using it in this class though

Back to Base-16

  • 10 will always be the base number in any base
    • 10 is two in binary, ten in base-10, and sixteen in base-16
  • Hex is often prefixed 0x, makes it easier to differentiate from a variable name in programming languages
  • Subscript with 16 to also help differentiate
  • Counting in Hex: 0-F, 10-1F, 20-2F, ... 90-9F, A0-AF, B0-Bf, etc.
  • If listed in a grid, numberes change by 16 per row

Hex to Binary

  • Every hex digit is 4 binary digits (bits)
    • i.e. 0x05 = 0101
  • Can take two hex numbers, and convert them to two 4-digit binary numbers and then combine them to get a valid binary number
    • i.e. 0x72 = 0111 0010
    • can independently convert hex digits into binary!
  • Can use this same idea to also go from binary to hex!
  • Processors can easily convert chunks of hex into binary because of this!

Integers and Bases Worksheet Pt 2 (15 min)

Each person should fill out the hex section of the previous worksheet

  • Can work with a partner, but you'll need to understand this on your own to do the homework!
  • Tip: write out some basic hex/binary conversions to use for help

Negative Numbers (5 min, only if there is extra time)

Cover if time is available, only needed for stretch challenges.

There are a few different ways to represent negative numbers:

  • Signed Magnitude: leftmost bit indicates sign: 0 is positive, 1 is negative
  • Base-10 to Base-2 equivalents:
    • 72 <--> 0100 1000
    • -72 <--> 1100 1000
  • One's compliment: flip all the bits to switch between positive and negative numbers
  • Base-10 to Base-2 equivalents:
    • 72 <--> 0100 1000
    • -72 <--> 1011 0111
    • One's compliment Addition: Add the two numbers, and if there's a carry, do an end-arround carry (add it back to the sum)
  • Two-s Compliment: most way processors represnt negatives, to negate a two's compliment number, invert/flip all the bits and then add 1
    • other technique: starting from the right, find the first 1, invert all the bits to the left of that 1
  • Base-10 to Base-2 equivalents:
    • 72 <--> 0100 1000
    • -72 <--> 1011 1000

Wrap up (5 min)

  1. Finish the worksheet except for the negative numbers section (that section is a stretch challenge)
  2. Go over resources
  3. Present overview of the coding challenges due next class.

Resources

Challenges

  • Practice binary and hexadecimal conversions on number bases worksheet
  • Implement base conversion functions for unsigned numbers using starter code:
    • Implement decode - decode digits in any base to a number
    • Implement encode - encode a number to digits in any base
    • Implement convert - convert digits in any base to digits in any base
    • Run python bases.py number base1 base2 to test convert on number
      • Example: python bases.py 42 10 2 should give the result 101010
    • Run pytest bases_test.py to run the unit tests and fix any failures
  • Write additional unit tests to ensure your conversion algorithms are robust

Stretch Challenges