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 AND, OR, NOT, 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 = 16
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: log2(n)+1.
- Binary searches also work with a list of STRINGS. We can sort them alphabetically.
- Binary searches can be represented in TREE diagrams.
Binary Conversions Practice ✔
- Convert the decimal number "17" into binary.
17 / 2 = 8 R1
8 / 2 = 4 R0
4 / 2 = 2 R0
2 / 2 = 1 R0
1 / 2 = 0 R1
10001 - Convert the binary number 1010 into decimal.
1 x 0 + 2 x 1 + 4 x 0 + 8 x 1 = 0 + 2 + 0 + 8 = 10
10 - Convert the decimal number "122" into hexadecimal.
122 / 16 = 7 R10, A = 10 in hexadecimal
7 / 16 = 0 R7
7A - Convert the hexadecimal number "09" into binary.
0 = 0000
9 / 2 = 4 R1
4 / 2 = 2 R0
2 / 2 = 1 R0
1 / 2 = 0 R1
9 = 1001
00001001
Binary Search Questions ✔
- Make a binary search tree of different the list [1,2,4,15,25,30,31]
- Put this list of strings in a order that can be used for binary search ["Donut”,"Cake”,"Soda”,"Banana”,"Fruit”]
- Alphabetical order: ["Banana", "Cake", "Donut", "Fruit", "Soda"]
- Reverse alphabetical order: ["Soda", "Fruit", "Donut", "Cake", "Banana"]
- Explain why Binary Search is more efficient than Sequential Search.
In binary search, by dividing the list in half at each iteration, we can eliminate half of the remaining elements each time, resulting in a much faster search process. Binary Search is more efficient than Sequential Search for large lists, as it reduces the number of comparisons required to find the target value, resulting in faster search times.
Extra Credit: ✔
- Translate the binary number, 1001010, into octal (base 8). SHOW YOUR WORK AND EXPLAIN YOUR THINKING.
001010
100 , 101 , 0
100 = 4 , 101 = 5 , 0 = 0
450
Group the binary number in groups of 3
Convert each group of three from binary to an octal number
Put them together
OR
- write the best rap line (determined during the lesson by group)