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 binary sequence.

Practice Questions:

  1. How many bits are in 3 bytes?
    • 8*3 = 24 bits
  2. What digital information can be represented by bits?
    • All digital information can be represented by bits
  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 the difference between the two is that digital is represented in bits as a 1 or 0 however with analog data it has the possibility of having other 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 cool
binary is cool
binary is awesome
binary is cool
binary is awesome
binary is awesome
binary is awesome
binary is awesome
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 is the largest number that can be represented by 5 bits.
  2. One programming 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^16 - 1 = 65,535
    • 2^56 -1 = 72,057,594,037,927,935
    • The second language can store about 1,099,528,405,248 times more numbers than 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 than question 1)

    • Signed 5 Bit integer: 31
    • Unsigned 5 bit integer: 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 each 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 value a 5 digit based system can represent is 5^n depending on where it correlates to then the prior values can be added to each other to be able to get the total value of the number as for instance 001001 can be represent as (0 x 2^6) + (0 x 2^6) + (1 x 2^5) ... and how a similar concept can be used in a base 10 system we could do the exact same thing with a base 5 system.
  2. What base is Hexadecimal? What range of values can each digit of Hexadecimal represent?

    • Hexadecimal is base 16 and can represent 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?

  • In a base system above 10, letters are used to represent numbers past 9. To find the letter that represents a particular number in a given base system, we perform integer division and modulus operations. For example, to represent the number 23 in a base 30 system, we divide 23 by 30 and take the remainder, which is 23. The corresponding letter for the number 23 in base 30 is H, so 23 in base 30 is represented as the single digit H. Similarly, to represent the number 23 in a base 50 system, we divide 23 by 50 and take the remainder, which is 23. The corresponding letter for the number 23 in base 50 is X, so 23 in base 50 is represented as the single digit X.

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 = 010011
    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]

Code Demo With Binary Math operations

def binary_addition(x, y):
    # Convert binary sequences x and y to integers, add them, and convert back to binary sequence
    # ,2 tells the function that the number is a binary number in base 2 and the slice [2:] removes the "0b" from the beginning of the string this is the same for all the other functions
    return bin(int(x, 2) + int(y, 2))[2:]

def binary_subtraction(x, y):
    # Convert binary sequences x and y to integers, subtract them, and convert back to binary sequence
    return bin(int(x, 2) - int(y, 2))[2:]

def binary_multiplication(x, y):
    # Convert binary sequences x and y to integers, multiply them, and convert back to binary sequence
    return bin(int(x, 2) * int(y, 2))[2:]

def binary_division(x, y):
    # Convert binary sequences x and y to integers, divide them using integer division, and convert back to binary sequence
    quotient = bin(int(x, 2) // int(y, 2))[2:]
    # Calculate the remainder and convert it to a binary string
    remainder = bin(int(x, 2) % int(y, 2))[2:]
    # Return a string with the quotient and the remainder tag
    return quotient + " remainder: " + remainder


def binary_operations(x, y, operator):
    if operator == '+':
        # Perform binary addition
        return binary_addition(x, y)
    elif operator == '-':
        # Perform binary subtraction
        return binary_subtraction(x, y)
    elif operator == '*':
        # Perform binary multiplication
        return binary_multiplication(x, y)
    elif operator == '/':
        # Perform binary division
        return binary_division(x, y)
    else:
        # Invalid operator
        return "Invalid operator!"

# Must be a string as it is a binary sequence or it will be treated as a decimal number


addition = binary_operations('11010101', '1010', '+')

print(f'This is the result of binary addition of the values 1101 and 1010: {addition}')

subtraction = binary_operations('110110101', '101', '-')
print(f'This is the result of binary subtraction of the values 1101 and 1010: {subtraction}')

multiplication = binary_operations('0101101', '11', '*')
print(f'This is the result of binary multiplication of the values 1101 and 1010: {multiplication}')

division = binary_operations('1', '100101', '/')
print(f'This is the result of binary division of the values 1101 and 1010: {division}')
This is the result of binary addition of the values 1101 and 1010: 11011111
This is the result of binary subtraction of the values 1101 and 1010: 110110000
This is the result of binary multiplication of the values 1101 and 1010: 10000111
This is the result of binary division of the values 1101 and 1010: 0 remainder: 1
def binary_addition(x, y):
    return int(x, 2) + int(y, 2)

def binary_subtraction(x, y):
    return int(x, 2) - int(y, 2)

def binary_multiplication(x, y):
    return int(x, 2) * int(y, 2)

def binary_division(x, y):
    quotient = int(x, 2) // int(y, 2)
    remainder = int(x, 2) % int(y, 2)
    return quotient, remainder

def binary_operations(x, y, operator):
    if operator == '+':
        return binary_addition(x, y)
    elif operator == '-':
        return binary_subtraction(x, y)
    elif operator == '*':
        return binary_multiplication(x, y)
    elif operator == '/':
        return binary_division(x, y)
    else:
        return "Invalid operator!"

addition = binary_operations('111', '1010', '+')
print(f'This is the result of binary addition of the values 111 and 1010: {addition}')

subtraction = binary_operations('10101011', '1010', '-')
print(f'This is the result of binary subtraction of the values 10101011 and 1010: {subtraction}')

multiplication = binary_operations('1', '1010', '*')
print(f'This is the result of binary multiplication of the values 1 and 1010: {multiplication}')

division = binary_operations('0101011', '1010', '/')
print(f'This is the result of binary division of the values 0101011 and 1010: {division[0]} and the remainder is {division[1]}')
This is the result of binary addition of the values 1101 and 1010: 17
This is the result of binary subtraction of the values 1101 and 1010: 161
This is the result of binary multiplication of the values 1101 and 1010: 10
This is the result of binary division of the values 1101 and 1010: 4 and the remainder is 3