On Sat, 13 Feb 2010 23:17:27 +0100 spir <[email protected]> wrote: > On Sat, 13 Feb 2010 13:58:34 -0500 > David Abbott <[email protected]> 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/ _______________________________________________ Tutor maillist - [email protected] To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
