P4-M 4/28 Binary Lesson HACKS
Learn the basics of binary including truth tables, boolean expressions, binary conversions, and binary searches.
Lesson Note Taker
Fill in the blanks below during the binary presentation. You can visit our website here!^ Due to last minute deployment issues, may need to run a local server
- git clone https://github.com/TheoH32/Runtime_Terror.git
- run:
- bundle install
- bundle exec jekyll serve
Binary
- Binary is a base 2 number system.
- 0 represents off and 1 represents on.
- A bit is the minimum unit of binary information stored in a computer system.
Boolean Expressions
- A boolean expression is a logical statement that is either TRUE or FALSE and compares data.
Truth Tables
- The logical operations shown in truth tables are not, and, or, and xor.
# AND
5 > 3 and 5 == 3 + 2
5 < 3 and 5 == 5
5 == 5 or 5 != 5
5 < 3 or 5 != 5
age = 18
citizen = True
if age >= 18 and citizen:
print("You are eligible to vote.")
else:
print("You are not eligible to vote.")
Binary Conversions
Binary to Decimal
- We can count in binary by using powers of 2.
- In binary, we read from right to left.
- 0111 has a value of 7.
Binary Search
- For a binary search, the list must be SORTED.
- In a binary search, computers start at the middle(front,middle,end)/
- The number of steps required in a binary search follows the equation: log(n).
- Binary searches also work with a list of strings. We can sort them alphabetically.
- Binary searches can be represented in tree diagrams.
Hacks
You will NOT be awarded any points for sections that are INCOMPLETE
Note Taker
- Fill in all of the blanks above.
Lesson Quiz
- Complete the lesson quiz
- SCREENSHOT SCORE and paste it here (or paste screenshot with submission)
Binary Game
- Complete the Binary game and reach a minimum score of 10!
- SCREENSHOT SCORE and paste it here (or with submission)
Binary Search Questions
-
Make a binary search tree of different the list [1,2,4,15,25,30,31]
15 / \ 2 30 / \ / \ 1 4 25 31
- Put this list of strings in a order that can be used for binary search ["Donutā,"Cakeā,"Sodaā,"Bananaā,"Fruitā]
- ["Banana", "Cake", "Donut", "Fruit", "Soda"] (In alphabetical order)
- Explain why Binary Search is more efficient than Sequential Search.
- Binary search is more efficient than sequential search because it reduces the number of elements that need to be searched by half with each iteration.
- In binary search, we start by comparing the target value with the middle element of the sorted list, and if they are not equal, we can eliminate one half of the list from consideration. We then repeat the process with the remaining half until we either find the target value or determine that it does not exist in the list.
- In contrast, sequential search requires checking every element of the list until the target value is found, which can be inefficient for large lists. Therefore, binary search is generally faster than sequential search for large datasets.
- Binary search is more efficient than sequential search because it reduces the number of elements that need to be searched by half with each iteration.
Extra Credit:
- Translate the binary number, 1001010, into octal (base 8). SHOW YOUR WORK AND EXPLAIN YOUR THINKING.
My extra credit
To convert a binary number to octal, we need to group the binary digits into sets of three starting from the rightmost digit, and then write down the corresponding octal digit for each set of three.
Here are the steps to convert the binary number 1001010 to octal:
Step 1: Group the binary digits into sets of three starting from the rightmost digit:
1 0 0 1 0 1 0 1 0 0 1 0 1 0 Step 2: Add leading zeros to the leftmost set of digits, if necessary, to make it a set of three:
| 0 | 1 | 0 | 0 | 1 | 0 | 1 | 0 |
Step 3: Write down the corresponding octal digit for each set of three, starting from the leftmost set:
0 1 0 0 1 0 1 0
2 4 1 0 5 2 0
Therefore, the binary number 1001010 is equivalent to the octal number 241052 in base 8.
The reason this works is because each octal digit can represent 8 different values (0-7), which is equivalent to 2^3. By grouping the binary digits into sets of three, we can match each set of three to a single octal digit.
001 001 010 = 112
Hacks Scoring
Hack | Comments | Grade |
---|---|---|
Note Taker | fill in the blanks above | 0.1 |
Lesson Quiz | under 100% = 0.1 only | 0.2 |
Binary Game | must score at least 10 points | 0.2 |
Binary Conversions Practice | if incorrect= 0.2 awarded | 0.2 |
Binary Search Questions | if incorrect= 0.2 awarded | 0.2 |
Extra Credit | MUST SHOW WORK | 0.1 |
Total | expected= 0.9/1 | 1/1 |