Apache Mesos
bits.hpp
Go to the documentation of this file.
1 // Licensed under the Apache License, Version 2.0 (the "License");
2 // you may not use this file except in compliance with the License.
3 // You may obtain a copy of the License at
4 //
5 // http://www.apache.org/licenses/LICENSE-2.0
6 //
7 // Unless required by applicable law or agreed to in writing, software
8 // distributed under the License is distributed on an "AS IS" BASIS,
9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 // See the License for the specific language governing permissions and
11 // limitations under the License.
12 
13 #ifndef __STOUT_BITS_HPP__
14 #define __STOUT_BITS_HPP__
15 
16 #include <stdint.h>
17 
18 // Provides efficient bit operations.
19 // More details can be found at:
20 // http://graphics.stanford.edu/~seander/bithacks.html
21 namespace bits {
22 
23 // Counts set bits from a 32 bit unsigned integer using Hamming weight.
24 inline int countSetBits(uint32_t value)
25 {
26  int count = 0;
27  value = value - ((value >> 1) & 0x55555555);
28  value = (value & 0x33333333) + ((value >> 2) & 0x33333333);
29  count = (((value + (value >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24;
30 
31  return count;
32 }
33 
34 } // namespace bits {
35 
36 #endif // __STOUT_BITS_HPP__
int countSetBits(uint32_t value)
Definition: bits.hpp:24
Definition: bits.hpp:21