Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Open Source > BOOK: Python Projects
Password Reminder
| FAQ | Members List | Calendar | 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
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old August 4th, 2018, 11:10 AM
Registered User
Points: 46, Level: 1
Points: 46, Level: 1 Points: 46, Level: 1 Points: 46, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Oct 2015
Posts: 5
Thanks: 2
Thanked 2 Times in 2 Posts
Question 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:

>>>import sys
>>> sys.getsizeof(0)
>>> sys.getsizeof(10**9)
>>> sys.getsizeof(10**10)
>>> sys.getsizeof(1&1)
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:

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:

>>> bin(bm.AND(0b1110))
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

Last edited by Egalth; August 4th, 2018 at 02:43 PM..
Reply With Quote
  #2 (permalink)  
Old August 5th, 2018, 04:11 AM
Wrox Author
Points: 119, Level: 2
Points: 119, Level: 2 Points: 119, Level: 2 Points: 119, Level: 2
Activity: 0%
Activity: 0% Activity: 0% 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!

Re Return type of BitMask.
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.
Reply With Quote
  #3 (permalink)  
Old August 7th, 2018, 11:27 AM
Registered User
Points: 46, Level: 1
Points: 46, Level: 1 Points: 46, Level: 1 Points: 46, Level: 1
Activity: 0%
Activity: 0% Activity: 0% 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 :)
Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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

All times are GMT -4. The time now is 08:32 AM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.