How to contact us

Join the "coding" channel on slack! That is the only place where we will be answering questions or sending announcements about lessons. If you have a question please contact us there.

How to join

  • Click on "add channels" below the list of channels
  • Click on "browse channels"
  • Search for "coding"
  • Click the green "Join" button on the right

Learning Objectives

CollegeBoard Requirements for Binary

DAT-1.A: Representing Data with Bits

Basic Information

  • Bit is short for binary__ digit, and represents a value of either 0 or 1.
    • A byte is 8 bits.
  • Sequences of bits are used to represent different things.
    • Representing data with sequences of bits is called _abstraction__.

Practice Questions:

  1. How many bits are in 3 bytes?: 24 bits

  2. What digital information can be represented by bits?: booleans

  3. Are bits an analog or digital form of storing data? What is the difference between the two?: Digital form of storing data, analog is physical, digital is virtual or digital.

Examples

  • Boolean variables (true or false) are the easiest way to visualize binary.
    • 0 = False
    • 1 = True
import random

def example(runs):
    # Repeat code for the amount of runs given
    while runs > 0:
        # Assigns variable boolean to either True or False based on random binary number 0 or 1.
        boolean = False if random.randint(0, 1) == 0 else True 

        # If the number was 1 (True), it prints "awesome."
        if boolean:
            print("binary is awesome")
            
        # If the number was 2 (False), it prints "cool."
        else:
            print("binary is cool")
            
        runs -= 1
     
# Change the parameter to how many times to run the function.   
example(100)
binary is awesome
binary is cool
binary is awesome
binary is awesome
binary is cool
binary is awesome
binary is awesome
binary is awesome
binary is awesome
binary is awesome
binary is awesome
binary is cool
binary is awesome
binary is cool
binary is cool
binary is awesome
binary is awesome
binary is cool
binary is awesome
binary is awesome
binary is cool
binary is cool
binary is cool
binary is cool
binary is cool
binary is awesome
binary is cool
binary is cool
binary is cool
binary is awesome
binary is cool
binary is awesome
binary is awesome
binary is cool
binary is cool
binary is cool
binary is awesome
binary is awesome
binary is awesome
binary is cool
binary is cool
binary is awesome
binary is awesome
binary is awesome
binary is awesome
binary is cool
binary is awesome
binary is cool
binary is cool
binary is cool
binary is cool
binary is cool
binary is awesome
binary is cool
binary is cool
binary is awesome
binary is cool
binary is cool
binary is cool
binary is awesome
binary is cool
binary is awesome
binary is awesome
binary is cool
binary is awesome
binary is awesome
binary is awesome
binary is cool
binary is awesome
binary is cool
binary is awesome
binary is awesome
binary is awesome
binary is cool
binary is cool
binary is awesome
binary is cool
binary is cool
binary is cool
binary is awesome
binary is cool
binary is cool
binary is cool
binary is cool
binary is cool
binary is cool
binary is awesome
binary is awesome
binary is awesome
binary is cool
binary is awesome
binary is awesome
binary is cool
binary is cool
binary is awesome
binary is awesome
binary is cool
binary is awesome
binary is cool
binary is awesome

DAT-1.B: The Consequences of Using Bits to Represent Data

Basic Information

  • Integers are represented by a fixed number of bits, this limits the range of integer values. This limitation can result in overflow or other errors.
  • Other programming languages allow for abstraction only limited by the computers memory.
  • Fixed number of bits are used to represent real numbers/limits

Practice Questions:

  1. What is the largest number can be represented by 5 bits?: 31

  2. One programing language can only use 16 bits to represent non-negative numbers, while a second language uses 56 bits to represent numbers. How many times as many unique numbers can be represented by the second language?: 2^40

  3. 5 bits are used to represent both positive and negative numbers, what is the largest number that can be represented by these bits? (hint: different thatn question 1): 15

Examples

import math

def exponent(base, power):
    # Print the operation performed, turning the parameters into strings to properly concatenate with the symbols "^" and "=".
    print(str(base) + "^" + str(power) + " = " + str(math.pow(base, power)))

# How can function become a problem? (Hint: what happens if you set both base and power equal to high numbers?): when the number for the power is set too high, the program overflows
exponent(5, 10000)
---------------------------------------------------------------------------
OverflowError                             Traceback (most recent call last)
/c/Users/navan/vscode/fastpages/_notebooks/2023-04-21-P3M-BinaryLesson.ipynb Cell 5 in <cell line: 8>()
      <a href='vscode-notebook-cell://wsl%2Bubuntu/c/Users/navan/vscode/fastpages/_notebooks/2023-04-21-P3M-BinaryLesson.ipynb#W4sdnNjb2RlLXJlbW90ZQ%3D%3D?line=4'>5</a>     print(str(base) + "^" + str(power) + " = " + str(math.pow(base, power)))
      <a href='vscode-notebook-cell://wsl%2Bubuntu/c/Users/navan/vscode/fastpages/_notebooks/2023-04-21-P3M-BinaryLesson.ipynb#W4sdnNjb2RlLXJlbW90ZQ%3D%3D?line=6'>7</a> # How can function become a problem? (Hint: what happens if you set both base and power equal to high numbers?): when the number for the power is set too high, the program overflows
----> <a href='vscode-notebook-cell://wsl%2Bubuntu/c/Users/navan/vscode/fastpages/_notebooks/2023-04-21-P3M-BinaryLesson.ipynb#W4sdnNjb2RlLXJlbW90ZQ%3D%3D?line=7'>8</a> exponent(5, 10000)

/c/Users/navan/vscode/fastpages/_notebooks/2023-04-21-P3M-BinaryLesson.ipynb Cell 5 in exponent(base, power)
      <a href='vscode-notebook-cell://wsl%2Bubuntu/c/Users/navan/vscode/fastpages/_notebooks/2023-04-21-P3M-BinaryLesson.ipynb#W4sdnNjb2RlLXJlbW90ZQ%3D%3D?line=2'>3</a> def exponent(base, power):
      <a href='vscode-notebook-cell://wsl%2Bubuntu/c/Users/navan/vscode/fastpages/_notebooks/2023-04-21-P3M-BinaryLesson.ipynb#W4sdnNjb2RlLXJlbW90ZQ%3D%3D?line=3'>4</a>     # Print the operation performed, turning the parameters into strings to properly concatenate with the symbols "^" and "=".
----> <a href='vscode-notebook-cell://wsl%2Bubuntu/c/Users/navan/vscode/fastpages/_notebooks/2023-04-21-P3M-BinaryLesson.ipynb#W4sdnNjb2RlLXJlbW90ZQ%3D%3D?line=4'>5</a>     print(str(base) + "^" + str(power) + " = " + str(math.pow(base, power)))

OverflowError: math range error

DAT-1.C: Binary Math

Basic Information

  • Binary is Base 2, meaning each digit can only represent values of 0 and 1.
  • Decimal is Base 10, meaning eacht digit can represent values from 0 to 9.
  • Conversion between sequences of binary to decimal depend on how many binary numbers there are, their values and their positions.

Practice Questions:

  1. What values can each digit of a Base 5 system represent?: 0,1,2,3,4

  2. What base is Hexadecimal? What range of values can each digit of Hexadecimal represent?: Base 16: 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F

  3. When using a base above 10, letters can be used to represent numbers past 9. These letters start from A and continue onwards. For example, the decimal number 10 is represented by the letter A in Hexadecimal. What letter would be used to represent the Base 10 number 23 in a Base 30 system? What about in a Base 50 system?: Base 30 --> n, also Base 50 --> n

Examples

  • Using 6 bits, we can represent 64 numbers, from 0 to 63, as 2^6 = 64.
  • The numbers in a sequence of binary go from right to left, increasing by powers of two from 0 to the total amount of bits. The whole number represented is the sum of these bits. For example:
    1. 111111
    2. 2^5 + 2^4 + 2^3 + 2^2 + 2^1 + 2^0
    3. 32 + 16 + 8 + 4 + 2 + 1
    4. 63
  • Fill in the blanks (convert to decimal)

    1. 001010 = 10_
    2. 11100010 = 226_
    3. 10 = 2_
  • Fill in the blanks (convert to binary)

    1. 12 = 001100_
    2. 35 = 100011_
    3. 256 = 100000000_

Hacks & Grading (Due SUNDAY NIGHT 4/23)

  • Complete all of the popcorn hacks (Fill in the blanks + run code cells and interact + Answer ALL questions) [0.3 or nothing]
  • Create a program to conduct basic mathematical operations with binary sequences (addition, subtraction, multiplication, division) [0.6 or nothing]
    • For bonus, program must be able to conduct mathematical operations on binary sequences of varying bits (for example: 101 + 1001 would return decimal 14.) [0.1 or nothing]
digit1 = input("Enter first binary digits: ")
digit2 = input("Enter second binary digits: ")
decimalResult = 0
binaryResult = 0
finish=0 # to track completion

# converts to binary from decimal 
def toBinaryValue(num):
    bin = 0
    # array to store  binary digits
    arr = []
    
    # loop till end of all digits
    while num > 0:
        # mod of 2 will give either 0 or 1
        # append to array
        arr.append(num % 2)
        
        # divide the number by 2 to move to next digit
        num //= 2
    arr.reverse()
    
    # convert to displayable format 
    for i in arr:
        bin = bin * 10 + i
    return bin

# converts to decimal from binary
def toDecimalValue(num):
    pos = []
    index = 0
    decimal = 0
    
    # store the number in a list array
    numArray = list(str(num))
    
    #reverse the number
    numArray.reverse()
    
    # loop till the length of the digit and 
    # check for bit 1 or bit 0
    for x in numArray:
        if (x == "1"):
            
            # if bit 1 then store the index in pos array
            pos.append(index)
            index += 1
        else:
            # if bit 0 then pos array will be zero value
            index +=1
    # loop the pos array and raise it to the power of 2
    for x in pos:
        decimal += 2**x
    return decimal

print("Number 1: Binary: " + digit1 + " Decimal: " + str(toDecimalValue(digit1))) 
print("Number 2: Binary: " + digit2 + " Decimal: " + str(toDecimalValue(digit2))) 

# Do Addition
decimalResult = toDecimalValue(digit1) + toDecimalValue(digit2)
binaryResult = toBinaryValue(int(decimalResult))
print("Addition: " + digit1, "+", digit2, "= Decimal value is:", decimalResult, "and Binary value is:", binaryResult)

# Do Subtraction
decimalResult = toDecimalValue(digit1) - toDecimalValue(digit2)
binaryResult = toBinaryValue(int(decimalResult))
print("Subtraction: " + digit1, "-", digit2, "= Decimal value is:", decimalResult, "and Binary value is:", binaryResult)

# Do Multiplication
decimalResult = toDecimalValue(digit1) * toDecimalValue(digit2)
binaryResult = toBinaryValue(int(decimalResult))
print("Multiplication: " + digit1, "*", digit2, "= Decimal value is:", decimalResult, "and Binary value is:", binaryResult)

# Do Division
decimalResult = toDecimalValue(digit1) / toDecimalValue(digit2)
binaryResult = toBinaryValue(int(decimalResult))
print("Division: " + digit1, "/", digit2, "= Decimal value is:", decimalResult, "and Binary value is:", binaryResult)
Number 1: Binary: 1010 Decimal: 10
Number 2: Binary: 10 Decimal: 2
Addition: 1010 + 10 = Decimal value is: 12 and Binary value is: 1100
Subtraction: 1010 - 10 = Decimal value is: 8 and Binary value is: 1000
Multiplication: 1010 * 10 = Decimal value is: 20 and Binary value is: 10100
Division: 1010 / 10 = Decimal value is: 5.0 and Binary value is: 101