Wrox Programmer Forums Chapter 1 - bits.py problems
 | 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

December 17th, 2014, 12:54 PM
Registered User
 Points: 8, Level: 1
 Activity: 0%

Join Date: Dec 2014
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Chapter 1 - bits.py problems

I must say, starting off the book with bitwise operations is a bit difficult for someone just past the beginner level. A lot is presumed about knowing binary operations and how to count in binary. Aside from that rant I'm having trouble getting my code to match with the book output. I was trying this on a fresh Ubuntu 14.04 VPS, using python3 interpreter

In my bits.py file:

Code:
```def bit(val, idx):
mask = 1 << idx # all 0 except idx
return bool(val & 1)```
I'm supposed to get:

>>> bin.bits(0b0101, 0)
True
>>> bin.bits(0b0101, 1)
False

But I get True returned for both!

I think the 'bit' function is also messing up my 'listbits' function. Here's my code:

Code:
```def listbits(val):
num = len(bin(val)) - 2
result = []
for n in range(num):
result.append( 1 if bit(val, n) else 0 )
return list( reversed(result) )```
Now, the following operation is supposed to occur:
>>> bits.listbits(0b10111)
[1, 0, 1, 1, 1]

However, I get back:
[1, 1, 1, 1, 1]

I think this is something to do with the 'bit' function call in 'listbits' but I can't figure out what's going on because I don't understand binary too well. Any ideas?

December 17th, 2014, 01:02 PM
Registered User
 Points: 8, Level: 1
 Activity: 0%

Join Date: Dec 2014
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Typo

I've discovered that it is actually a typo in the book's code. I figured this out by downloading the Chapter 1 code and viewing the same function for 'bit'. It should read:

Code:
```def bit(val, idx):
mask = 1 << idx # all 0 except idx
Where '1' in the last line should have been 'mask'. This fixes all the issues I was having. Hope this helps others who were as confused as I was!

February 27th, 2015, 02:27 PM
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
bits.py bug

My apologies, there is indeed a bug in the code in the book.

the actual files I used in testing. The code in the book has been
copy/pasted/reformatted/reviewed and then reformatted again.
There are lots of opportunities for things to go astray.

Its still my fault however, for not spotting it in the final copy so
apologies once more.

Alan G.

February 27th, 2015, 02:34 PM
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

Quote:
 Originally Posted by saltycraig I must say, starting off the book with bitwise operations is a bit difficult for someone just past the beginner level. A lot is presumed about knowing binary operations and how to count in binary.
Sorry if it seems a bit (!) hard but I wanted the module/package to be
genuinely useful and because bitmasks crop up all over the OS stuff in
chapter 2, I thought providing an option that was slightly easier to use
than the standard bitwise operators would be worthwhile.

Alan G.

PS. I'm a little late on the scene as I only just discovered this forum.

October 16th, 2015, 07:44 PM
Registered User
 Points: 46, Level: 1
 Activity: 0%

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

I just started reading the book recently and became curious as to whether other readers had commented on the section about bitwise operators, because it was indeed a bit of a challenge. Like saltycraig I'm just (barely) past beginner's level. (I got an introduction to some Java programming many years ago). Now I am in a situation where I have very specific and relatively simple programming needs for a business, and felt it would be nice to use that need to learn some more programming. I purchased this book for three reasons: I immediately identified with "WHO THIS BOOK IS FOR" in the introduction, I liked the structure, and, most importantly, I was looking for a book with practical exercises (including solutions!).

Fortunately I started reading during a holiday and had plenty of time to google around to learn more about bitwise operators. In particular, the following example in step 4 on page 37 was difficult to understand (despite the How-it-works comments on the next page):
Code:
```>>> bin(bits.NOT(0b0101))
'-0b110'```
But I agree that after I had figured out the details, it was a nice example that immediately gives the reader some pretty fundamental insights into programming basics.

However, as a reader I would have appreciated very much if there had been a short explanation, only a few sentences, to explain 1) how byte size is related to the bit representation of numbers, 2) the concept of two's complement for representing negative numbers, and 3) that there is a difference between the computer's physical bit representation of the number and Python's binary representation of that number. Once this is figured out, it's easy to see why 0b0101 in the example is represented (in 8 bits for simplicity) as 00000101. Performing bitwise NOT (switching the ones and zeros) on this trivially gives 11111010, which is (by definition) the two's complement representation of the number -6. And in Python the binary representation of 6 is '0b110', so -6 is simply '-0b110'.
Code:
```>>> 0b110
6
>>> -0b110
-6```

In the next step of the example it is necessary to understand the conversion between binary and hexadecimal numbers, which is pretty straightforward, but some readers might appreciated a little nudge in the right direction. Likewise, the concept of a bitmask deserves an extra sentence to explain it to the newcomers.

I learned a lot from studying this example, but I spent several hours over the course of a couple of days before it made sense.

October 17th, 2015, 12:39 PM
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

Your points are all valid and well made, however the purpose of the example was primarily to produce a module/package and I decided I wanted it to be useful rather than just a toy. I considered giving more theoretical background but decided it would distract from the actual focus of packaging the code.

As I stated in an earlier reply, I chose this functionality because I knew it could be useful for dealing with the bitmasks encountered in the OS section of Chapter 2. Perhaps, coming from an electronics background, I underestimated just how foreign a concept bitmasks and binary logic are to programming beginners. If that's the case I apologize in advance to all future readers! Hopefully Wikipedia can compensate... :-)

Alan g.
 The Following User Says Thank You to Alan G For This Useful Post: Egalth (October 19th, 2015)

 Similar Threads Thread Thread Starter Forum Replies Last Post creating an array of size 2^16 bits scoobie C++ Programming 2 November 9th, 2005 08:59 PM Turning strings into bits...help patpicos BOOK: Beginning Java 2 2 March 29th, 2005 06:09 AM inverting bits sdilucca Visual C++ 0 July 2nd, 2004 02:14 PM