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 abstractions.

Practice Questions:

  1. How many bits are in 3 bytes?

    • 8*3 = 24 bits
  2. What digital information can be represented by bits?

    • Bits, or binary digits with values 0 or 1, are the basic units of digital information. They can represent various types of data, such as text, numbers, images, audio, and video, by encoding them in binary sequences.
  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, using discrete binary values (0 and 1). The main difference between digital and analog data storage is that analog storage uses continuous signals, such as varying voltages or waveforms, to represent information, while digital storage relies on discrete values.

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)
binary is awesome
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 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 overflows 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?

    • 11111 in binary, or 31 in decimal
  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 first programming language can represent 2^16 unique non-negative numbers, which equals 65,536. The second language can represent 2^56 unique numbers, which equals 72,057,594,037,927,936. To find out how many times as many unique numbers the second language can represent, divide the number of unique values in the second language by the number in the first language:

    • 72,057,594,037,927,936 / 65,536 ≈ 1,099,511,627,776

    • The second language can represent approximately 1,099,511,627,776 times as many unique numbers as the first language.

  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)

    • When using 5 bits to represent both positive and negative numbers, one common method is to use the two's complement representation. In this case, the most significant bit (MSB) serves as the sign bit, where 0 indicates a positive number and 1 indicates a negative number. The remaining 4 bits are used to represent the magnitude of the number. So, the largest number that can be represented by these 5 bits is 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?)
exponent(5, 2)
5^2 = 25.0

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?

    • In base 5, the place values are 1, 5, 25, 125, 625, 3125, and so on. Remember, 324 (base 5) is eighty-nine, not three hundred twenty-four.
  2. What base is Hexadecimal? What range of values can each digit of Hexadecimal represent?

    • In hex, four digits of a binary number can be represented by a single hex digit. Dividing a binary number into 4-bit sets means that each set can have a possible value of between 0000 and 1111, allowing 16 number combinations from 0 to 15. With the base value as 16, the maximum value of a digit is 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?

    • In a base-30 system, the first 10 digits are represented by the numbers 0-9, and the next 20 digits are represented by the letters A-T, where A corresponds to 10, B to 11, and so on. The base-10 number 23 would be represented by the letter N (10 + 13 = 23) in a base-30 system.

    • In a base-50 system, the first 10 digits are represented by the numbers 0-9, and the next 40 digits are represented by the letters A-Y and a-y, where A corresponds to 10, B to 11, and so on. The base-10 number 23 would be represented by the letter N (10 + 13 = 23) in a base-50 system as well.

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 = 1100
    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]
def binary_to_int(binary_str):
    return int(binary_str, 2)

def int_to_binary(integer):
    return bin(integer)[2:]

def binary_addition(a, b):
    a_int = binary_to_int(a)
    b_int = binary_to_int(b)
    return int_to_binary(a_int + b_int)

def binary_subtraction(a, b):
    a_int = binary_to_int(a)
    b_int = binary_to_int(b)
    return int_to_binary(a_int - b_int)

def binary_multiplication(a, b):
    a_int = binary_to_int(a)
    b_int = binary_to_int(b)
    return int_to_binary(a_int * b_int)

def binary_division(a, b):
    a_int = binary_to_int(a)
    b_int = binary_to_int(b)
    if b_int == 0:
        raise ValueError("Division by zero is not allowed.")
    quotient = a_int // b_int
    remainder = a_int % b_int
    return int_to_binary(quotient), int_to_binary(remainder)

def main():
    a = input("Enter first binary number: ")
    b = input("Enter second binary number: ")

    print(f"Addition: {binary_addition(a, b)}")
    print(f"Subtraction: {binary_subtraction(a, b)}")
    print(f"Multiplication: {binary_multiplication(a, b)}")
    quotient, remainder = binary_division(a, b)
    print(f"Division: Quotient: {quotient}, Remainder: {remainder}")

if __name__ == "__main__":
    main()
Addition: 10
Subtraction: 0
Multiplication: 1
Division: Quotient: 1, Remainder: 0