Dangling Conversations

Colour commentary on the world we live in

Netmasks and Snow

Posted 06 Jan, 2005 at 09:35 by brent in /Math | Permanent link

I came up with what I think is a simple way to determine if a netmask is valid. If the netmask is represented as an unsigned 4 byte integer, let's call it nm, then if ((~nm) + 1) & (~nm) == 0 then nm is a valid netmask. Just to go in a bit of detail, a valid netmask is a set of 1's followed by a set of zeroes. so if a bit sequence of 01 appears in the netmask, the netmask is invalid.

So ~ is the bitwise complement, + is the standard integer add, and & is bitwise AND. So if the netmask is valid, ~ will flip the bits so it's a sequence of zeroes followed by a sequence of ones. Add one to that and you flip the bits again for all the ones to zeroes and the first zero to a one (or overflow the 4 bytes resulting in a zero value). If the netmask is valid, if you bitwise and that value now with the bitwise complement, you should now get zero.

If the netmask is invalid on the otherhand, then there is a 01 sequence somewhere. When you bit flip you get 10. When you add 1, that zero ensures the 1 is still there (no carry will get it to zero). So when you bitwise AND it with the bitwise complement, you will get 10 in that location which is certainly non-zero.

And it is currently snowing outside, so it looks like I'll have to wear shoes today (snow in sandals just doesn't work).

Comments (0 comments so far)
Join the conversation:
 
Name:
URL/Email:
[http://... or mailto:you@wherever] (optional)
Title: (optional)
Comments:
Save my Name and URL/Email for next time
How many copies of the letter T are in the word STRESSFUL ?

Comments are not automatically formatted, so please bring your own HTML. Allowed tags are: P BR EM STRONG A I B UL OL LI.

Hows & Whys
Who we are

Blue has been known to toss a disc around a field from time to time, and thinks that you should as well. He lives either on the Internet or in Toronto, depending on your perspective. Ask him no questions and there's a good chance he'll tell you no lies. [Site]

Brent hosts the box from which we dangle our conversations, for which we are all eternally grateful. Gratitude is most easily expressed in small bills. Formerly a pawn of the Evil Empire (or maybe a Knight), he has gone over the wall and now toils at a small computer game company in Alberta that no one except for ten million gamers has ever heard of. [Site]

Kael occasionally gets called "Mike"; mostly by people who don't know him. He cooks, he cleans, he maintains Unix servers... what else could you ask for? Currently a slave to the Man, by which we mean retail sales. He has secret plans, but we can't tell you about them. In fact, we've already said too much. [Site]

Lisa is a recreational therapist without a cause. She entertains dreams of ruling over an empire of scrapbooking. Has a well-deserved reputation for enthusiasm, common sense, and tiredness. Ask her about her teapots, but don't touch them.

Matt is just this guy, you know? A mathematician by training and a layabout by inclination; he currently has an Urban Commuter Campus in the American Midwest convinced that they should pay him for plying these trades. The designer and administrator of this site, which means in a sense this is all his fault. [Site | E-mail]

Sky is a salesman during the day. At night he doesn't bother: his words are like unto those of a god, and you can agree or you can be wrong. Lives in the World of Warcraft, with a sattelite office in Toronto. Known to play games on occasion.

Wendy has never run away to join the circus, but pursuing graduate work in medical imaging is perhaps just as good. She didn't choose her current abode on the basis of proximity to a Toronto Public Library branch, but we wouldn't put it past her. Married to one of the other authors here, but you'll have to read the archives to find out which one. [Site]

Giving us what-for

Posts that have attracted comments in the last week:

What we talk about
Remembering when
Where's it say that?

Advanced Search