Answering the Questions in the lesson

Bitwise Opperations

Operator Name Action
& AND
When both values are True it outputs True. If one is False and the other is True it will output False. If both are False it will output False.

OR
If at least one of the values is True then the output will be True, otherwise the output will be False

^ XOR
If both are the same value then it would output True. So if both values are True then it will output True. If both values are False, it will also output True. But if there is one True and one False, then the output will be False.

~ NOT
If it is NOT True, then the output is False. If it is NOT False, then the output is True.

<< Left Shift
This shifts the binary sequence to the left and adds a zero in the empty space. for example, 1011 becomes 10110.

>> Right Shift
Shifts the binary sequence to the right. For example, 1011 becomes 101.

>>> Zero-Fill Right Shift
Means that when you shift it right and add a zero at the end.

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.

  • 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

Binary search operates a lot like a "guess the number" game. Try the game and explain how the two are similar.

Calculator Hack:

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)
3 ^ 4
Result: 81