Subject: Network mask contiguity

From: Igor Miroshnik <[email-masked]>
Date: Sun, 14 Dec 2003 15:44:03 -0500

Group,

Is anybody familiar with an efficient algorithm testing network mask
contiguity?

Thank you,
Igor

---

From: Vishwas Manral <[email-masked]>
Date: Mon, 15 Dec 2003 19:08:01 +0200

Hi Igor,

I dont think this question belongs here, however just thinking of it I found
this could be fairly optimal. From the right start till u find the bit 1
(could make this more optimal), compare the value as stored in an array of
index the number of bits. The value could be 2^32 - 2^x at index x.

You could start with looking at the more possible prefix mask lengths, like
24 bits etc.

Thanks,
Vishwas

-----Original Message-----
From: Mailing List [mailto:[email-masked]]On Behalf Of Igor
Miroshnik
Sent: Sunday, December 14, 2003 10:44 PM
To: [email-masked]
Subject: Network mask contiguity

Group,

Thank you,
Igor

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.551 / Virus Database: 343 - Release Date: 12/11/2003

---

From: Fred Baker <[email-masked]>
Date: Mon, 15 Dec 2003 07:43:34 -0800

>Is anybody familiar with an efficient algorithm testing network mask

On a 32 bit two's complement machine, one can isolate the least significant
"1" in a number by

         lso = (mask) & (-mask);

and test 32 bit mask contiguity by

         0 == mask + lso

Extending to a 64 bit machine should be obvious: test for the mask itself
being zero, or the sum being 0x0000000100000000

for a 128 bit mask, the simple approach is to structure the number as four
unsigned 32 bit ints. Some may be equal to zero, and some may be equal to
0xFFFFFFFF. The one in the middle meets the rule above.