Solution to Exam 2 Question A
In Python on our CMC 306 lab machines, it seems that 2**30 fits into a small integer but 2**31 requires a large integer. To get more detailed information, I wrote this code:
total = 0
for i in range(31):
total += 2**i
This adds up all of the powers of 2, from 2**0 to 2**30. The result should be 2**31 - 1, which equals 2147483647. As expected, Python says that total equals 2147483647. However, if I then add 1 to total, then Python reports that the new total is 2147483648L. So it appears that the greatest number that can be stored in small integers is 2**31 - 1.
In an unsigned n-bit integer, the greatest number that can be stored is 2**n - 1. In a two's complement n-bit integer, the greatest number that can be stored is 2**(n - 1) - 1. Based on my experiments, I suspect that Python is using two's complement 32-bit integers.