[Tutor] Binary

Sheila King sheila@thinkspot.net
Fri, 11 May 2001 19:49:59 -0700


On Fri, 11 May 2001 22:10:42 -0400 (EDT), "Michael P. Reilly"
<arcege@dsl092-074-184.bos1.dsl.speakeasy.net>  wrote about Re: [Tutor] Binary:

:def bin(n):
:  """Return a bit string representing the number, right-justified."""
:  s = ''
:  while n:
:    if n & 01:
:      s = '1' + s
:    else:
:      s = '0' + s
:    n = n >> 1
:  return s

OK, I tried this out, and it works, apparently.
But I don't understand the code.

What does this line do?

if n & 01:

and what about this line?

n = n >> 1

I thought, for a minute, someone was using C or C++ to "extend" Python, until I
looked more carefully.

--
Sheila King
http://www.thinkspot.net/sheila/
http://www.k12groups.org/