P3-M 4/21 Binary Overview
A series of binary lessons focusssed on math and conversions.
- How to contact us
- Learning Objectives
- Hacks & Grading (Due SUNDAY NIGHT 4/23)
- Coding Hacks and Bonus
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 bits
- 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, analog is manual and digital is computing
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?
56-16 = 40
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 thatn 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, 70000)
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?
0, 5, 25, 125, etc.
- What base is Hexadecimal? What range of values can each digit of Hexadecimal represent?
Base 6, 0, 6, 36, 1296, 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?
For base 10, the representation would be M, as 23 is 13 more than the Base 10 allows. In the base 50 system the number should be 23, without letters as there are enough characters to represent 23.
I am still not in full understanding of this question, and will be following up on Monday to better understand.
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 = 2^3 + 2 = 10
- 11100010 = 2 + 2^5 + 2^6 + 2^7 = 226
- 10 = 2
-
Fill in the blanks (convert to binary)
- 12 = 1100
12/2 = 0 6/2 = 0 3/2 = 1 1/2 = 1
- 35 = 100011
35/2 = 1 17/2 = 1 8/2 = 0 4/2 = 0 2/2 = 0 1/2 = 1
- 256 = 10000000
256/2 = 0 128/2 = 0 64/2 = 0 32/2 = 0 16/2 = 0 8/2 = 0 4/2 = 0 2/2 = 0 1/2 = 1
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]
Coding Hacks and Bonus
This code uses functions bin() which converts an integer to its binary representation, while int() converts a binary representation to an integer with a specific base. The base in this case being 2, because we are using binary. Because the program converts the input to an integer, than manipulates them, we can use simple syntax like +, -, //, and * to add/subtract/divide/multiply the numbers.
a = input("Enter first binary number:") # example number 111 (dec 7), 3 bit input
b = input("Enter second binary number:") # example number 11 (dec 3), 2 bit input
print("Input a:", a)
print("Input b:", b)
def addnums(): # define function for add
add = bin(int(a, 2) + int(b, 2)) # converts inputs to integers, adds them, then converts back to binary
print("Sum:", str(int(add[2:], 2))) # converts back to integers
addnums()
# displays output
def subnums(): # define function for subtract
sub = bin(int(a, 2) - int(b, 2)) # converts inputs to integers, subtracts them, then converts back to binary
print("Difference:", str(int(sub[2:], 2)))
subnums()
def multinums(): # define function for multiply
multi = bin(int(a, 2) * int(b, 2)) # converts inputs to integers, multiplies them, then converts back to binary
print("Product:", str(int(multi[2:], 2)))
multinums()
def divnums(): # define function for divide
div = bin(int(a, 2) // int(b, 2)) # converts inputs to integers, divides them, then converts back to binary
print("Quotient:", str(int(div[2:], 2)))
divnums()