Skip to main content

Notes on Architecture

Section 3.2 Problems for Thursday, January 11

Work on these problems in groups of 2–3 while the instructor catches up on grading projects 00 and 01. For Python coding problems, you can save more files on your Pico (make sure not to overwrite code.py). Use the Serial console in Mu editor to see the output of your programs.

Exercises Exercises

1.

Write a function is_even(x) that takes a single integer argument x and returns True if the argument is even and False otherwise. You may assume that the argument is a nonnegative integer (do not validate within your function). You may not use the % operator. Only use bitwise operators and equality comparison.

2.

Write a function bit_is_set(x, n) that takes two integer arguments x and n and returns True if the nth bit of x is set and False otherwise. You may assume that the arguments are nonnegative integers (do not validate within your function). You may not use the % operator or any loops or conditionals. Only use bitwise operators and equality comparison.

3.

Write a function set_bit(x, n) that takes two integer arguments x and n and returns the value of x with the nth bit set. You may assume that the arguments are nonnegative integers (do not validate within your function). You may not use the % operator or any loops or conditionals. Only use bitwise operators and equality comparison.

4.

Write a function clear_bit(x, n) that takes two integer arguments x and n and returns the value of x with the nth bit cleared. You may assume that the arguments are nonnegative integers (do not validate within your function). You may not use the % operator or any loops or conditionals. Only use bitwise operators and equality comparison.

5.

Write a function toggle_bit(x, n) that takes two integer arguments x and n and returns the value of x with the nth bit toggled. You may assume that the arguments are nonnegative integers (do not validate within your function). You may not use the % operator or any loops or conditionals. Only use bitwise operators and equality comparison.