I was reviewing chapter 1 the other day, after initially having read it over a year ago. It was worth the effort, not least because I realized that many concepts I struggled with in the beginning now seem straightforward.
However, I spotted a few peculiarities in the example with the bitwise operators that I can't seem to wrap my head around.
1. Bit size
In the bits.py script on page 36, there is a comment saying that "return types are 16 bit integers or boolean". Why 16 bits? How do we know this? And why is it even mentioned? As far as I can tell integers have an undefined size (potentially infinite) in Python:
https://wiki.python.org/moin/BitwiseOperators
The following code examples show bit sizes of 24 bits for zero, 28 bits for integers between 1 and 10^9 and 32 bits for 10^10:
Code:
>>>import sys
>>> sys.getsizeof(0)
24
>>> sys.getsizeof(10**9)
28
>>> sys.getsizeof(10**10)
32
>>> sys.getsizeof(1&1)
28
Where does 16 come from?
2. Bitmasks
In the bitmask.py script on page 39, the return type of the class methods are defined as instances of BitMask. What's the value in this definition compared to just leaving them as integers? For example:
Code:
class BitMask(int):
def AND(self,bm):
return int(self & bm) '''<-- int() instead of BitMask()'''
And one final question, to see if I am missing something essential. In the BitMask code above, the argument passed to AND is called "bm", supposedly short for "bitmask". But isn't this a little misleading? Isn't that argument rather any value that we compare against the bitmask that is chosen when the object is created? For example, in the test code on page 41:
Code:
>>> bin(bm.AND(0b1110))
'0b1100'
Here, 0b1110 is passed as an argument to AND in order to be tested against the bitmask. But it is distinct from the bitmask, isn't it? If so, I guess it would have been more intuitive to just name it "value".
Thanks in advance