[Tutor] operators >> and &
spir
denis.spir at free.fr
Sat Feb 13 23:28:31 CET 2010
On Sat, 13 Feb 2010 23:17:27 +0100
spir <denis.spir at free.fr> wrote:
> On Sat, 13 Feb 2010 13:58:34 -0500
> David Abbott <david at pythontoo.com> wrote:
>
> > I am attempting to understand this little program that converts a
> > network byte order 32-bit integer to a dotted quad ip address.
> >
> > #!/usr/bin/python
> > # Filename : int2ip.py
> >
> > MAX_IP = 0xffffffffL
> > ip = 2130706433
> >
> > def int2ip(l):
> > if MAX_IP < l < 0:
> > raise TypeError, "expected int between 0 and %d inclusive" %
> > MAX_IP
> > return '%d.%d.%d.%d' % (l>>24 & 255, l>>16 & 255, l>>8 & 255, l &
> > 255)
PS: in "l>>24 & 255", the & operation is useless, since all 24 higher bits are already thrown away by the shift:
>>> 0x12345678 >> 24
18 # = 0x12
Denis
________________________________
la vita e estrany
http://spir.wikidot.com/
More information about the Tutor
mailing list