Wrox Programmer Forums Bitwise operators: integer size and bitmask terminology
 | Search | Today's Posts | Mark Forums Read
BOOK: Python Projects
 This is the forum to discuss the Wrox book Python Projects Laura Cassell, Alan Gauld; ISBN: 978-1-118-90866-2 Read more about Python Projects or buy the book from your favorite retailerDownload the code for Python Projects
 Welcome to the p2p.wrox.com Forums. You are currently viewing the BOOK: Python Projects section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com

August 4th, 2018, 11:10 AM
Registered User
 Points: 46, Level: 1
 Activity: 0%

Join Date: Oct 2015
Posts: 5
Thanks: 2
Thanked 2 Times in 2 Posts
Bitwise operators: integer size and bitmask terminology

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?

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):
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".

Last edited by Egalth; August 4th, 2018 at 02:43 PM..

August 5th, 2018, 04:11 AM
Wrox Author
 Points: 119, Level: 2
 Activity: 0%

Join Date: Feb 2015
Location: Stirling, Scotland
Posts: 25
Thanks: 0
Thanked 3 Times in 3 Posts

Re Bit size:
I have no idea why I wrote that the return type was 16 bits! You are quite right the integers in Python are much bigger than that. Sorry, I can't offer any explanation other than brain malfunction!

The idea is that the returned objects are bitmasks and therefore have all the bitmask methods. You can therefore chain method calls if required. For example:

result = bm1.OR(bm2).NOT()

RE Parameter to AND.
The intent of the name bm was simply to indicate the purpose of the parameter rather than its type (as should always be the case). It should be a bit mask of some kind, either a bit mask integer or a BitMask object. But I agree there could be confusion here because the class and the concept have the same name. In the real world I'd probably have a doc string per method explaining the difference along with some doctest examples showing both forms of usage.

However, the real purpose of this example was not to demonstrate bitwise operations but to demonstrate use of modules and packages, so my concentration was focused on that rather than the details of the class definition. At least, that's my excuse! :-)

I hope that clarifies things a little.

August 7th, 2018, 11:27 AM
Registered User
 Points: 46, Level: 1
 Activity: 0%

Join Date: Oct 2015
Posts: 5
Thanks: 2
Thanked 2 Times in 2 Posts

Thanks for this swift reply even though it's been some time since the thread was active!

It makes sense to me now. The purpose of illustrating how packages and modules work was clear. But sometimes valuable insights can be hidden in the small nuances, and I find stubbornness is often just as important as curiosity when it comes to learning :)

 Similar Threads Thread Thread Starter Forum Replies Last Post BitMask Try It Out pg40 error xynzee BOOK: Python Projects 4 April 5th, 2017 11:32 AM Terminology Prof Alvaro Hernandez BOOK: Excel 2007 VBA Programmer's Reference ISBN: 978-0-470-04643-2 3 May 6th, 2012 10:42 AM Question regarding bitwise operators on page 66 Bahr BOOK: Beginning Visual C# 2010 1 March 30th, 2011 03:07 PM Integer Size Miquella C++ Programming 7 November 6th, 2005 10:31 PM Terminology? jacob C# 3 July 28th, 2005 03:33 PM