[Tutor] Finding even and odd numbers

Michael Langford mlangford.cs03 at gtalumni.org
Wed Sep 19 22:52:14 CEST 2007

Premature optimization is the devil. Care not which method is faster. Only
optimize when the whole program is too slow and then find which area is the
biggest culprit and optimize that.

Computers are very very fast these days. Even embedded systems are so fast
you don't understand how fast they are. Your brain just doesn't comprehend
periods of time that short. It can't understand really understand *Really*
how many instructions happen in a second. It just can't.

That being said, I'd say the bitwise and (&) is unclear unless you're
working around embedded programmers or other low level engineers or C
programmers or are using it for a lot of other bit masking.

I'd also go for modulus (%) because its more generalizable to divisibility
tests other than powers of 2. There is no bitwise and test to see if a
number is divisible by 5 for instance, at least not one you'd like to see.
You're going to confusing everyone if you implement isDivBy8 with bitwise


On 9/19/07, Terry Carroll <carroll at tjc.com> wrote:
> On Wed, 19 Sep 2007, Boykie Mackay wrote:
> > if not n&1:
> >     return false
> >
> > The above should return false for all even numbers,numbers being
> > represented by n.I have tried to wrap my head around the 'not n&1' but
> > I'm failing to understand what's going on.Could someone please explain
> > the statement.
> Others have explained why this works, but let me rail against this code
> for a moment.  IMHO, a bitwise operation is appropriate only when the
> field being operated on is a set of bits with bitwise information; for
> example, if the right-most bit was a flag bit of some sort, N&1 is a
> perfectly appropriate operation.
> But evenness/oddness is a property of numbers, and the code would be much
> easier to understand if the parameter was treated as a number, rather than
> as a field of bits, i.e.:
> >>> def isodd(n):
> ...  return bool(n%2)
> ...
> >>> isodd(4)
> False
> >>> isodd(7)
> True
> >>>
> Mind you, I'll bet that the bit-checking method is slightly faster than
> the modulus operation in my little piece of code above.  I'll also bet
> that, if you added up the amount of time that was saved by using the
> bitwise approach over the modulus approach, over all executions of the
> program everywhere, it probably would be a smaller amount of time than it
> would take for a programmmer maintaining the program to find out what that
> line of code is supposed to do.
> </soapbox>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor

Michael Langford
Phone: 404-386-0495
Consulting: http://www.TierOneDesign.com/
Entertaining: http://www.ThisIsYourCruiseDirectorSpeaking.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20070919/08090243/attachment.htm 

More information about the Tutor mailing list