librfn
An ad-hoc utility library
bitopstest.c
Go to the documentation of this file.
1 /*
2  * bitopstest.c
3  *
4  * Part of librfn (a general utility library from redfelineninja.org.uk)
5  *
6  * Copyright (C) 2012 Daniel Thompson <daniel@redfelineninja.org.uk>
7  * Copyright (C) 2014 Daniel Thompson <daniel@redfelineninja.org.uk>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU Lesser General Public License as published
11  * by the Free Software Foundation; either version 3 of the License, or
12  * (at your option) any later version.
13  */
14 
15 #undef NDEBUG
16 
17 #include <assert.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 
21 #include <librfn.h>
22 
23 int main()
24 {
25  assert(8 == bitcnt(0x11111111));
26  assert(32 == bitcnt(0xffffffff));
27  assert(16 == bitcnt(0xff00ff00));
28  assert(16 == bitcnt(0xcccccccc));
29  assert(0 == bitcnt(0));
30 
31  assert(32 == clz(0));
32  assert(31 == clz(1));
33  assert(1 == clz(0x7fffffff));
34  assert(16 == clz(0xffff));
35  assert(0 == clz(0xffffffff));
36  assert(14 == clz(0x3ffff));
37  assert(14 == clz(0x20000));
38 
39  assert(32 == ctz(0));
40  assert(31 == ctz(0x80000000));
41  assert( 1 == ctz(0x00000002));
42  assert(16 == ctz(0xffff0000));
43  assert( 0 == ctz(0xffffffff));
44  assert(14 == ctz(0xffffc000));
45  assert(14 == ctz(0x00004000));
46 
47  assert(0 == ilog2(1));
48  assert(1 == ilog2(2));
49  assert(1 == ilog2(3));
50  assert(2 == ilog2(4));
51  assert(19 == ilog2(1 << 19));
52 
53  return 0;
54 }
int bitcnt(uint32_t x)
Definition: bitops.c:20
int ctz(uint32_t x)
Definition: bitops.c:46
int main()
Definition: bitopstest.c:23
int ilog2(uint32_t x)
Definition: bitops.c:51
int clz(uint32_t x)
Definition: bitops.c:36