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
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:
How many bits are in 3 bytes? 24
What digital information can be represented by bits? 0 and 1
Are bits an analog or digital form of storing data? What is the difference between the two? digital, digital is only 0 or 1, but analog can be a range of numbers
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 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:
What is the largest number can be represented by 5 bits? 32
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^56 - 2^16 = 2^40
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) 8
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)
#A function like this can become a problem if both the base and power parameters
# are set to very high numbers.
# math.pow() function used to calculate the exponentiation may result in a very
#
# large number that exceeds the maximum size that can be represented by the data
# type being used (e.g., float or int). In such cases, the result may be inaccurate
# or even cause an error, depending on the language and environment being used. It is
# important to consider the limitations of the data types being used and the potential
# range of values that the function may receive as input.
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:
What values can each digit of a Base 5 system represent? values of 0,5,25,125, so on
What base is Hexadecimal? What range of values can each digit of Hexadecimal represent? base 6, 0,6,36,etc.
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? M, M
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:
- 111111
- 2^5 + 2^4 + 2^3 + 2^2 + 2^1 + 2^0
- 32 + 16 + 8 + 4 + 2 + 1
- 63
Fill in the blanks (convert to decimal)
- 001010 = 18_ 2 + 2^3
- 11100010 = 226_ 2 + 2^5 + 2^6 + 2^7
- 10 = 3_ 1 + 2
Fill in the blanks (convert to binary)
- 12 = _110__ 12/6= 0 3/2 = 1 1/2 = 1
- 35 = 100011_
- 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_addition(binary1, binary2):
max_len = max(len(binary1), len(binary2))
binary1 = binary1.zfill(max_len) # # method so binary numbers will have zeros on the
#left so that they have the same length as the maximum length.
binary2 = binary2.zfill(max_len)
result = ''
track_carry = 0
for i in range(max_len - 1, -1, -1): # goes through each digit in the two binary numbers from the right to the left.
r = track_carry
r += 1 if binary1[i] == '1' else 0 #if the current digit in binary1 is 1, 1 is added to 'r'
r += 1 if binary2[i] == '1' else 0 # If the current digit in binary2 is 1, another 1 is added to 'r'.
result = ('1' if r % 2 == 1 else '0') + result
track_carry = 0 if r < 2 else 1
if track_carry != 0:
result = '1' + result
return result
# takes two binary sequences as input and returns their difference in decimal form.
def binary_subtraction(binary1, binary2):
max_len = max(len(binary1), len(binary2))
binary1 = binary1.zfill(max_len)
binary2 = binary2.zfill(max_len)
result = ''
borrow = 0
for i in range(max_len - 1, -1, -1):
if binary1[i] == '0':
if binary2[i] == '0':
result = ('1' if borrow == 1 else '0') + result
borrow = 1 if borrow == 1 else 0
else:
result = ('0' if borrow == 1 else '1') + result
else:
if binary2[i] == '0':
result = ('0' if borrow == 1 else '1') + result
else:
result = ('1' if borrow == 1 else '0') + result
borrow = 1
return str(int(result, 2))
# takes two binary sequences as input and returns their product in decimal form.
def binary_multiplication(binary1, binary2):
result = '0'
for i, bit1 in enumerate(reversed(binary1)): #iterate through the elements of x in reverse order, while also keeping track of the index of each element.
if bit1 == '1':
temp = bin(int(binary2, 2) << i)[2:]
temp += '0' * i
result = binary_addition(result, temp)
return str(int(result, 2))
#takes two binary sequences as input and returns their quotient and remainder in decimal form.
def binary_division(dividend, divisor):
dividend_int = int(dividend, 2)
divisor_int = int(divisor, 2)
quotient_int = dividend_int // divisor_int
remainder_int = dividend_int % divisor_int
quotient_binary = bin(quotient_int)[2:]
remainder_binary = bin(remainder_int)[2:]
return str(quotient_int), str(remainder_int)
# Taking user input
binary1 = input("Enter binary: ")
binary2 = input("Enter binary: ")
print("binary addition: ", binary_addition(binary1, binary2))
print("binary subtraction: ", binary_subtraction(binary1, binary2))
print("binary multiplication: ", binary_multiplication(binary1, binary2))
quotient, remainder = binary_division(binary1, binary2)
print("binary division - quotient: ", quotient)
print("binary division - remainder: ", remainder)