YaK:: Masking an IPv4 address in Python [Changes]   [Calendar]   [Search]   [Index]   [PhotoTags]   
[mega_changes]
[photos]

Masking an IPv4 address in Python


>>> q = '192.168.8.232'
>>> m = 24

>>> a, b, c, d = [int(e) for e in q.split('.')]
>>> print a, b, c, d
192 168 8 232
>>> addy = a*(1<<24) + b*(1<<16) + c*(1<<8) + d
>>> print addy
3232237800
>>> print hex(addy)
0xc0a808e8

>>> maskedAddy = (0x100000000 - (1<<(32-m))) & addy
>>> print hex(maskedAddy)
0xc0a80800

>>> print '.'.join([str((maskedAddy>>i)&255) for i in [24, 16, 8, 0]])
192.168.8.0
(unless otherwise marked) Copyright 2002-2014 YakPeople. All rights reserved.
(last modified 2017-01-09)       [Login]
(No back references.)