]>
git.ipfire.org Git - thirdparty/bird.git/blob - lib/bitops.c
2 * BIRD Library -- Generic Bit Operations
4 * (c) 1998 Martin Mares <mj@ucw.cz>
6 * Can be freely distributed and used under the terms of the GNU GPL.
13 * u32_mkmask - create a bit mask
16 * u32_mkmask() returns an unsigned 32-bit integer which binary
17 * representation consists of @n ones followed by zeroes.
20 u32_mkmask(unsigned n
)
22 return n
? ~((1 << (32 - n
)) - 1) : 0;
26 * u32_masklen - calculate length of a bit mask
29 * This function checks whether the given integer @x represents
30 * a valid bit mask (binary representation contains first ones, then
31 * zeroes) and returns the number of ones or -1 if the mask is invalid.
39 if (n
& (n
+1)) return -1;
40 if (x
& 0x0000ffff) { x
&= 0x0000ffff; l
+= 16; }
41 if (x
& 0x00ff00ff) { x
&= 0x00ff00ff; l
+= 8; }
42 if (x
& 0x0f0f0f0f) { x
&= 0x0f0f0f0f; l
+= 4; }
43 if (x
& 0x33333333) { x
&= 0x33333333; l
+= 2; }
44 if (x
& 0x55555555) l
++;
45 if (x
& 0xaaaaaaaa) l
++;