Chris@16: // ----------------------------------------------------------- Chris@16: // lowest_bit.hpp Chris@16: // Chris@16: // Position of the lowest bit 'on' Chris@16: // Chris@16: // Copyright (c) 2003-2004, 2008 Gennaro Prota Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. Chris@16: // (See accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: // ----------------------------------------------------------- Chris@16: Chris@16: #ifndef BOOST_LOWEST_BIT_HPP_GP_20030301 Chris@16: #define BOOST_LOWEST_BIT_HPP_GP_20030301 Chris@16: Chris@16: #include Chris@16: #include "boost/pending/integer_log2.hpp" Chris@16: Chris@16: Chris@16: namespace boost { Chris@16: Chris@16: template Chris@16: int lowest_bit(T x) { Chris@16: Chris@16: assert(x >= 1); // PRE Chris@16: Chris@16: // clear all bits on except the rightmost one, Chris@16: // then calculate the logarithm base 2 Chris@16: // Chris@16: return boost::integer_log2( x - ( x & (x-1) ) ); Chris@16: Chris@16: } Chris@16: Chris@16: Chris@16: } Chris@16: Chris@16: Chris@16: #endif // include guard