The Original CHomP Software
bitcount.h
Go to the documentation of this file.
1
3
14
15// Copyright (C) 1997-2020 by Pawel Pilarczyk.
16//
17// This file is part of my research software package. This is free software:
18// you can redistribute it and/or modify it under the terms of the GNU
19// General Public License as published by the Free Software Foundation,
20// either version 3 of the License, or (at your option) any later version.
21//
22// This software is distributed in the hope that it will be useful,
23// but WITHOUT ANY WARRANTY; without even the implied warranty of
24// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25// GNU General Public License for more details.
26//
27// You should have received a copy of the GNU General Public License
28// along with this software; see the file "license.txt". If not,
29// please, see <https://www.gnu.org/licenses/>.
30
31// Started on November 29, 2005. Last revision: November 29, 2005.
32
33
34#ifndef _CHOMP_STRUCT_BITCOUNT_H_
35#define _CHOMP_STRUCT_BITCOUNT_H_
36
37namespace chomp {
38namespace homology {
39
40
41extern unsigned char bitcounttable [];
42
43inline int bitcountbyte (char n)
44{
45 return bitcounttable [static_cast<unsigned char> (n)];
46} /* bitcountbyte */
47
48inline int bitcount (int number)
49{
50 if (!number)
51 return 0;
52 unsigned int n = static_cast<unsigned int> (number);
53 if (n < 256)
54 return bitcountbyte (static_cast<unsigned char> (n));
55 int c = 0;
56 while (n > 255)
57 {
58 if (n & 1)
59 ++ c;
60 n >>= 1;
61 }
62 return c + bitcountbyte (static_cast<unsigned char> (n));
63} /* bitcount */
64
65
66} // namespace homology
67} // namespace chomp
68
69#endif // _CHOMP_STRUCT_BITCOUNT_H_
70
72
unsigned char bitcounttable[]
int bitcount(int number)
Definition: bitcount.h:48
int bitcountbyte(char n)
Definition: bitcount.h:43
This namespace contains the entire CHomP library interface.
Definition: bitmaps.h:51