Extension: Bitwise Operators, Numeral Systems, and Byte Strings

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 do most of the useful work we want done with it, but it does have features that allow us to work directly with bits if we want to. Since bitwise operators are part of the language tested on the PCEP, we will explore them in this extension.

  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

    • >>> ~42

In order to understand the results you got in this exercise, you need to understand binary numbers and two’s complement. Take time to read through these Wikipedia entries carefully, until you can convince yourself you understand both binary numbers and two’s complement.

Then read the entry on bitwise operations. See if you can determine which symbol Python uses for each of the operations described in the article.

Finally, revisit exercise 1, this time making sure you understand the results.

  1. Now enter the following into the Python shell and record what you get as a response:

    >>> for n in [0b1001, 0b1100, 0o10, 0xA, 0x12]:
    ...     print(n, end=' ')
    ... else:
    ...     print()
    ...
    

Binary, Octal, and Hexidecimal Numbers

Octal and hexadecimal (or hex) are the two other numeral systems commonly used in computer science to represent bits.

Now look at the following table:

Decimal, Binary, Octal and Hexadecimal Numbers

Decimal

Binary

Octal

Hexadecimal

0

0b0000

0o0

0x0

1

0b0001

0o1

0x1

2

0b0010

0o2

0x2

3

0b0011

0o3

0x3

4

0b0100

0o4

0x4

5

0b0101

0o5

0x5

6

0b0110

0o6

0x6

7

0b0111

0o7

0x7

8

0b1000

0o10

0x8

9

0b1001

0o11

0x9

10

0b1010

0o12

0xA

11

0b1011

0o13

0xB

12

0b1100

0o14

0xC

13

0b1101

0o15

0xD

14

0b1110

0o16

0xE

15

0b1111

0o17

0xF

16

0b10000

0o20

0x10

17

0b10001

0o21

0x11

18

0b10010

0o22

0x12

Bytes Objects

A byte is sequence of 8 bits. Python has a data type for working with a sequence of bytes.

Take a look at the following Python shell session:

>>> message = b'A message from GHC'
>>> type(message)
<class 'bytes'>
>>> for byte in message:
...     print(byte)
...
65
32
109
101
115
115
97
103
101
32
102
114
111
109
32
71
72
67

Run this yourself and confirm that you get the same result. Then take time to understand it.

PCEP Practice

  1. What is the output of the following code?

    x = 1
    y = 0
    z = x & y
    w = x | y
    u = x ^ y
    print(z, w,  u)
    
  2. What is the output of the following code?

a, b = 0, 1
print(a ^ a, a ^ b, b ^ a, b ^ b)
  1. What is the output of the following code?

var = 17
var_right = var >> 1
var_left = var << 2
print(var, var_left, var_right)
  1. What is the output of the following code?

x = 1
y = 0
z = ((x == y) and (x == y)) or not(x == y)
print(not(z))
  1. What is the output of the following code?

x = 8
y = 1

a = x & y
b = x | y
c = ~x
d = x ^ 5
e = x >> 2
f = x << 2

print(a, b, c, d, e, f)