0

The title pretty much summarizes it.

In gcc, you can use the .Find_first() method on a bitset to get the position of the first significant bit. Is there any equivalent in Visual Studio?

I know you can use _BitScanForward64 in Visual Studio to get the first significant bit, but I work with bitsets of the size larger than 64, and this method only works with 64-bit integers.

I also am not interested in the De Bruijn bit twiddling method.

SMir
  • 650
  • 1
  • 7
  • 19

1 Answers1

1
template<size_t N>
size_t highbit( bitset<N> const& bs ) {
  auto str = bs.template to_string< char, char_traits<char>, allocator<char> >();
  size_t retval = 0;
  for(auto it = str.begin(); it != str.end(); ++it, ++retval)
  {
    if (*it == '1')
      return retval;
  }
  return retval;
}

No bit twiddling, as requested. Returns N if the bits are all zeros.

Personally, I would recommend bit twiddling instead.

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
  • Thank you. This seems to check the value of the bits individually, which is not what I had in mind when I asked the question. I am/was mainly interested in some compiler-dependent instructions like `fbs`. – SMir Dec 10 '12 at 19:46