Statistics
| Branch: | Revision:

iof-bird-daemon / lib / bitops.c @ 18c8241a

History | View | Annotate | Download (634 Bytes)

1
/*
2
 *        BIRD Library -- Generic Bit Operations
3
 *
4
 *        (c) 1998 Martin Mares <mj@ucw.cz>
5
 *
6
 *        Can be freely distributed and used under the terms of the GNU GPL.
7
 */
8

    
9
#include "nest/bird.h"
10
#include "bitops.h"
11

    
12
u32
13
u32_mkmask(unsigned n)
14
{
15
  return n ? ~((1 << (32 - n)) - 1) : 0;
16
}
17

    
18
int
19
u32_masklen(u32 x)
20
{
21
  int l = 0;
22
  u32 n = ~x;
23

    
24
  if (n & (n+1)) return -1;
25
  if (x & 0x0000ffff) { x &= 0x0000ffff; l += 16; }
26
  if (x & 0x00ff00ff) { x &= 0x00ff00ff; l += 8; }
27
  if (x & 0x0f0f0f0f) { x &= 0x0f0f0f0f; l += 4; }
28
  if (x & 0x33333333) { x &= 0x33333333; l += 2; }
29
  if (x & 0x55555555) l++;
30
  if (x & 0xaaaaaaaa) l++;
31
  return l;
32
}