Chapter 6 Exercise Set 4: PCEP Topic - Bitwise OperatorsΒΆ

Way back in What is a computer? you were introduced to the idea that all information in a computer is stored as a sequence of bits. Python is a high-level language that thankfully keeps us from having to know such low-level details in order to get useful work done with it, but it does have features that allow us to work with bits if we want to.

Since bitwise operators are part of the language tested on the PCEP, we will explore here in this exercise set.

  1. Enter each of the following expressions into the Python shell and record what you get as a response:

    • >>> 5 | 3

    • >>> 9 & 3

    • >>> 12 ^ 5

    • >>> 1 << 3

  2. Complete the Introduction to number systems and binary lesson on Khan Academy.

  3. What is the output of the following program?

    x = 1
    y = 0
    z = x & y
    w = x | y
    u = x ^ y
    print(z, w,  u)