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? One byte has 8 bits, so 24 bits are in 3 bytes.

  2. What digital information can be represented by bits? Bits can represent many different things, particularly boolean values for true/ false or positions in binary numbers. One practical application of bits is in engineering, where circuits could be on or off represented by bits.

  3. Are bits an analog or digital form of storing data? What is the difference between the two? Analog methods of data storage capture every aspect of the data. They can be time consuming to call upon and store, and they take up a lot of space due to their high level of detail. Digital methods of data storage are more efficient, because they involve representations of the actual data. For example, bits are used to represent the data of interest, which are easier to store.

Examples

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

def example(howmanytimes):
    # Repeat code for the amount of runs given
    while howmanytimes > 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")
            
        howmanytimes -= 1
     
# Change the parameter to how many times to run the function.   
# I changed the variable runs to howmanytimes, and I added an input instead of the example 10
#example(10)
usernum = int(input("enter a number: "))
example(usernum)
binary is awesome
binary is cool
binary is awesome
binary is awesome
binary is awesome
binary is awesome
binary is cool
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 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? The largest number would be 1 for all 5 bits, or 11111. This number is 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? 56 - 16 = 40. Therefore, there are 2^40 more numbers that can be represented by the second language. Hence,2^(56/16) times more numbers can be represented.

  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 would still be 31, because although negative numbers may be represented too, if all the bits can be positive, the binary number would still be equal to 31.

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? Base 5 can include any value from 0-4. Since the name is "base 5," there are 5 possible numbers used.

  2. What base is Hexadecimal? What range of values can each digit of Hexadecimal represent? Hexademical is a base 6 system, where numbers 0-5 (0, 1, 2, 3, 4, 5) are used to

  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 is m, since every time the base goes up by 1, a letter is added once the numbers reach past 9. For base 50, the answer would still be 23 since the letters haven't repeated since 23 is so short.

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 = 0 + 0 + 2^3 + 0 + 2^1 + 0 = 10
    2. 11100010 = 0 + 2^1 + 0 + 0 + 0 + 2^5 + 2^6 + 2^7 = 226
    3. 10 = 2^1 + 0 = 2
  • Fill in the blanks (convert to binary)

    1. 12 = 12/2 ==> 0
       6/2 ==> 0
       3/2 ==> 1
       1/2 ==> 1
       0011
    2. 35 = 35/2 ==> 1
       17/2 ===> 1
       8/2 ==> 0
       4/2 ==> 0
       2/2 ==> 0
       1/2 ==> 1
       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]
# convert converts bin to dec
def convert_bin2dec(binstr):
  # Initialize the decimal value to 0.
  decimal_value = 0
  # starting at index of (length - 1) since index begins at 0. 
  # this way, we iterate the binary string from right to left. 
  # decrementing by 1 each time, since we are converting from right to left
  for i in range(len(binstr) - 1, -1, -1):   # range(first index, end index, increment)
    # Multiply the current digit by 2 raised to the power of its position.
    decimal_value += int(binstr[i]) * (2 ** (len(binstr) - 1 - i))

  return decimal_value

# taking user inputs for 2 binary strings as well as the operation they wish to perform
binstr1 = input("Enter binary string 1: ")
binstr2 = input("Enter binary string 2: ")
dec1 = convert_bin2dec(binstr1)
dec2 = convert_bin2dec(binstr2)

print(binstr1 + " as a decimal is " + str(dec1))
print(binstr2 + " as a decimal is " + str(dec2))

operation = input("Enter a mathematical operation (+, -, *, /): ")

# Performing the operation on the integers:
if operation == "+":
  result = dec1 + dec2
elif operation == "-":
  result = dec1 - dec2
elif operation == "*":
  result = dec1 * dec2
elif operation == "/":
  result = dec1 // dec2
else:
  raise ValueError("Invalid operation.")

print(str(dec1) + operation + str(dec2) + "=" + str(result))

# Convert the result back to a binary string.
result = bin(result)[2:]
print(binstr1 + operation + binstr2 + "=" + result)
11011 as a decimal is 27
100 as a decimal is 4
27+4=31
11011+100=11111