Merge remote-tracking branch 'origin/master'
[unleashed/lotheac.git] / usr / src / cmd / ipf / lib / common / count4bits.c
blobe3857fad67ddb6157695191772f2334b53e5e55e
1 /*
2 * Copyright (C) 1993-2001 by Darren Reed.
4 * See the IPFILTER.LICENCE file for details on licencing.
6 * $Id: count4bits.c,v 1.1 2002/06/15 04:46:39 darrenr Exp $
7 */
9 #include "ipf.h"
13 * count consecutive 1's in bit mask. If the mask generated by counting
14 * consecutive 1's is different to that passed, return -1, else return #
15 * of bits.
17 int count4bits(ip)
18 u_int ip;
20 int cnt = 0, i, j;
21 u_int ipn;
23 ip = ipn = ntohl(ip);
24 for (i = 32; i; i--, ipn *= 2)
25 if (ipn & 0x80000000)
26 cnt++;
27 else
28 break;
29 ipn = 0;
30 for (i = 32, j = cnt; i; i--, j--) {
31 ipn *= 2;
32 if (j > 0)
33 ipn++;
35 if (ipn == ip)
36 return cnt;
37 return -1;