sl@0
|
1 |
// -------------------------------------
|
sl@0
|
2 |
// lowest_bit.hpp
|
sl@0
|
3 |
//
|
sl@0
|
4 |
// Position of the lowest bit 'on'
|
sl@0
|
5 |
//
|
sl@0
|
6 |
// (C) Copyright Gennaro Prota 2003 - 2004.
|
sl@0
|
7 |
//
|
sl@0
|
8 |
// Distributed under the Boost Software License, Version 1.0.
|
sl@0
|
9 |
// (See accompanying file LICENSE_1_0.txt or copy at
|
sl@0
|
10 |
// http://www.boost.org/LICENSE_1_0.txt)
|
sl@0
|
11 |
//
|
sl@0
|
12 |
// ------------------------------------------------------
|
sl@0
|
13 |
|
sl@0
|
14 |
#ifndef BOOST_LOWEST_BIT_HPP_GP_20030301
|
sl@0
|
15 |
#define BOOST_LOWEST_BIT_HPP_GP_20030301
|
sl@0
|
16 |
|
sl@0
|
17 |
#include <cassert>
|
sl@0
|
18 |
#include "boost/pending/integer_log2.hpp"
|
sl@0
|
19 |
|
sl@0
|
20 |
|
sl@0
|
21 |
namespace boost {
|
sl@0
|
22 |
|
sl@0
|
23 |
template <typename T>
|
sl@0
|
24 |
int lowest_bit(T x) {
|
sl@0
|
25 |
|
sl@0
|
26 |
assert(x >= 1); // PRE
|
sl@0
|
27 |
|
sl@0
|
28 |
// clear all bits on except the rightmost one,
|
sl@0
|
29 |
// then calculate the logarithm base 2
|
sl@0
|
30 |
//
|
sl@0
|
31 |
return boost::integer_log2<T>( x - ( x & (x-1) ) );
|
sl@0
|
32 |
|
sl@0
|
33 |
}
|
sl@0
|
34 |
|
sl@0
|
35 |
|
sl@0
|
36 |
}
|
sl@0
|
37 |
|
sl@0
|
38 |
|
sl@0
|
39 |
#endif // include guard
|