[Tutor] operators >> and &

David Abbott david at pythontoo.com
Sat Feb 13 19:58:34 CET 2010


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)

result = int2ip(ip)
print result

I don't understand the l>>24 & 255. 

from the docs;
Right Shift a >> b rshift(a, b)
Bitwise And a & b and_(a, b)

thanks

-- 
David Abbott <david at pythontoo.com>



More information about the Tutor mailing list