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 one 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? There are 24 bits in 3 bites
  2. What digital information can be represented by bits? Digtical information that can be represented by bits are on and off or yes and no.
  3. Are bits an analog or digital form of storing data? What is the difference between the two? Bits are a digital form of storing data. Digital is automatically done in the computer and analog has to be done manually. #### 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(10)

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 __ 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? The largest number that can be reprsented by 5 bits is 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? The second languege can 2^(56/16) represent times more unique numbers because 56-16= 2^40 times more numbers.
  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) The largest number that can be represented by 5 bits would be 31 bits because the positive value would still be the same. #### 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?)
exponent(100000000000, 99999)

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? The values that each digit of base 5 system can represnet 0, 1, 2, 3, and 4
  2. What base is Hexadecimal? What range of values can each digit of Hexadecimal represent? The base value of hexadecimal is 16. The range of values that each digit of hexadecmial can represent is 0-15
  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? The letter that would be used to represent the base 10 number 23 in a base 30 system would be M for both #### 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 = _
    2. 11100010 = _
    3. 10 = _
  • Fill in the blanks (convert to binary)

    1. 12 = _
    2. 35 = _
    3. 256 = _

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]
def binary_addition(a, b):
    carry = 0
    result = ""
    # Pad the binary sequences with zeros if they have different lengths
    a = a.zfill(len(b))
    b = b.zfill(len(a))
    # Perform binary addition digit by digit
    for i in range(len(a)-1, -1, -1):
        if a[i] == '1' and b[i] == '1':
            if carry == 1:
                result = '1' + result
            else:
                result = '0' + result
            carry = 1
        elif a[i] == '0' and b[i] == '0':
            if carry == 1:
                result = '1' + result
            else:
                result = '0' + result
            carry = 0
        else:
            if carry == 1:
                result = '0' + result
                carry = 1
            else:
                result = '1' + result
    if carry == 1:
        result = '1' + result
    return result

# Function to subtract two binary values
def binary_subtraction(a, b):
    carry = 0
    result = ""
    # Pad the binary sequences with zeros if they have different lengths
    a = a.zfill(len(b))
    b = b.zfill(len(a))
    # Perform binary subtraction digit by digit
    for i in range(len(a)-1, -1, -1):
        if a[i] == '1' and b[i] == '0':
            if carry == 1:
                result = '0' + result
            else:
                result = '1' + result
            carry = 0
        elif a[i] == '0' and b[i] == '1':
            if carry == 1:
                result = '1' + result
            else:
                result = '0' + result
            carry = 1
        else:
            if carry == 1:
                result = '1' + result
            else:
                result = '0' + result
    if carry == 1:
        result = '1' + result
    return result

# Function to convert a binary value to a decimal number
def binary_to_decimal(binary):
    decimal = 0
    for i in range(len(binary)):
        if binary[i] == '1':
            decimal += 2**(len(binary)-i-1)
    return decimal

# Ask the user to input two binary values
a = input("Enter the first binary value: ")
b = input("Enter the second binary value: ")

# Ask the user to choose between addition and subtraction
operation = input("Choose the operation (+ for addition, - for subtraction): ")

# Perform the selected operation and print the result
if operation == '+':
    result_binary = binary_addition(a, b)
    result_decimal = binary_to_decimal(result_binary)
    print("The sum of", a, "and", b, "is:", result_binary, "in binary and", result_decimal, "in decimal.")
elif operation == '-':
    result_binary = binary_subtraction(a, b)
    result_decimal = binary_to_decimal(result_binary)
    print("The difference of", a, "and", b, "is:", result_binary, "in binary and", result_decimal, "in decimal.")
else:
    print("Invalid operation. Please choose either + or -.")
The sum of 1001 and 10001 is: 11010 in binary and 26 in decimal.