Binary Math Hacks
Hacks!!
In this program, the binary operators & (AND), | (OR), ^ (XOR), and ~ (NOT) are used to perform the binary operations on the input numbers. The results are stored in separate variables as decimal numbers. Then, the bin () function is used to convert each decimal result to binary, and the binary strings are stored in separate variables. Finally, the program returns a tuple of tuples, with each inner tuple containing both the decimal and binary result for each operation. The outer tuple is unpacked into separate variables for printing, and the results are displayed as both decimal and binary.
Bitwise operations are used in a variety of applications, particularly in low-level programming and computer science. Some common used of bitwise operations include:> - Flag Management: Flags are used to keep track of the state of a system or a program. Bitwise operations can be used to set, clear, and toggle flags.> - Bit Manipulation:Bitwise operations can be used to individual bits in a binary number. This is often used to extract specific bits from a number, set specific bits to a particular value, or flip the value of specific bits.> - Masking:Masking is used to extract a specific subset from a binary number. Bitwise operations are commonly used for masking, particularly in low-level programming.> - Encryption:Bitwise operations can be used in cryptographic applications to scramble and unscramble data. One common application of bitwise operations in encryption is the XOR operation.> - Graphics:Bitwise operations can be used in computer graphics to manipulate individual pixels on a screen. This can be used to draw shapes, change colors, and create special effects.> - Networking:Bitwise operations are used extensively in networking applications, particularly in the handling of IP addresses and port numbers.
Binary Search
- An algorithm made to find an item from a list of items
- Works by dividing the list repeatedly to narrow down which half (the low or high half) that contains the item
- Lists of integers are often used with binary search
- Binary search makes searching more efficient, as it ensures the program won't have to search through an entire list of items one by one
- List must be sorted
What are some situations in which binary search could be used?
- To find if n is a square of an integer
- Find the first value greater than or equal to x in a given array of sorted integers
- Find the frequency of a given target value in an array of integers
- Find the peak of an array which increases and then decreases
Real Example of Binary Search
Binary search operates a lot like a "guess the number" game. Try the game and explain how the two are similar.
def calculate(num1, num2, operator):
# Write your calculator function here
if operator == '+':
result == num1 + num2
elif operator == '-':
result = num1 - num2
elif operator == '*':
result = num1 * num2
elif operator == '/':
result = num1/num2
elif operator == '^':
result = num1**num2
return result
# Call the binary_math function and print the result
# Ask the user for input
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
operator = input("Enter the operator (+, -, *, /): ")
print(num1, operator, num2)
result = calculate(num1, num2, operator)
print("Result:", result)