Sync usage with man page.
[netbsd-mini2440.git] / dist / ipf / lib / count4bits.c
blob2ab8f21292331e874f422862fd4dc24b654e4ea0
1 /* $NetBSD$ */
3 /*
4 * Copyright (C) 2002 by Darren Reed.
6 * See the IPFILTER.LICENCE file for details on licencing.
8 * Id: count4bits.c,v 1.1.4.1 2006/06/16 17:20:57 darrenr Exp
9 */
11 #include "ipf.h"
15 * count consecutive 1's in bit mask. If the mask generated by counting
16 * consecutive 1's is different to that passed, return -1, else return #
17 * of bits.
19 int count4bits(ip)
20 u_int ip;
22 int cnt = 0, i, j;
23 u_int ipn;
25 ip = ipn = ntohl(ip);
26 for (i = 32; i; i--, ipn *= 2)
27 if (ipn & 0x80000000)
28 cnt++;
29 else
30 break;
31 ipn = 0;
32 for (i = 32, j = cnt; i; i--, j--) {
33 ipn *= 2;
34 if (j > 0)
35 ipn++;
37 if (ipn == ip)
38 return cnt;
39 return -1;